第二个活动没有打开
Posted
技术标签:
【中文标题】第二个活动没有打开【英文标题】:A second activity dosen't open 【发布时间】:2015-05-02 19:14:37 【问题描述】:我有一个带有几个按钮的应用程序,当用户单击任何按钮时,会发出音乐。问题是当活动打开时程序关闭。
只有这个活动,eclipse错误并不表示发生了什么。
这是代码:
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
public class GranjaActivity extends Activity
Button atras, vaca, cerdo, caballo, oveja, gallina, perro ;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_granja);
atras = (Button) findViewById(R.id.btnatras);
vaca = (Button) findViewById(R.id.btnvaca);
cerdo = (Button) findViewById(R.id.btncerdo);
caballo = (Button) findViewById(R.id.btncaballo);
oveja = (Button) findViewById(R.id.btnoveja);
gallina = (Button) findViewById(R.id.btngallina);
perro = (Button) findViewById(R.id.btnperro);
//boton atras
atras.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
finish();
);
//boton vaca
vaca.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
MediaPlayer vaca = MediaPlayer.create(getApplicationContext(), R.raw.vaca);
vaca.start();
);
//boton cerdo
cerdo.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
MediaPlayer cerdo = MediaPlayer.create(getApplicationContext(), R.raw.cerdo);
cerdo.start();
);
//boton caballo
caballo.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
MediaPlayer caballo = MediaPlayer.create(getApplicationContext(), R.raw.caballo);
caballo.start();
);
//boton oveja
oveja.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
MediaPlayer oveja = MediaPlayer.create(getApplicationContext(), R.raw.oveja);
oveja.start();
);
//boton gallina
gallina.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
MediaPlayer gallina = MediaPlayer.create(getApplicationContext(), R.raw.gallina);
gallina.start();
);
//boton perro
perro.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
MediaPlayer perro = MediaPlayer.create(getApplicationContext(), R.raw.perro);
perro.start();
);
这是de xml代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:background="@drawable/fondogranja"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="es.quovo.learninganimals.GranjaActivity" >
<Button
android:id="@+id/btnatras"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="14dp"
android:layout_marginTop="8dp"
android:background="@drawable/back" />
<Button
android:id="@+id/btnvaca"
android:layout_
android:layout_
android:layout_alignLeft="@+id/button1"
android:layout_alignParentBottom="true"
android:background="@drawable/botonvaca" />
<Button
android:id="@+id/btncerdo"
android:layout_
android:layout_
android:layout_alignTop="@+id/btnvaca"
android:layout_toRightOf="@+id/btnvaca"
android:background="@drawable/botoncerdo" />
<Button
android:id="@+id/btnperro"
android:layout_
android:layout_
android:layout_alignTop="@+id/btncerdo"
android:layout_toRightOf="@+id/btncerdo"
android:background="@drawable/botonperro"/>
<Button
android:id="@+id/btnoveja"
android:layout_
android:layout_
android:layout_alignTop="@+id/btnperro"
android:layout_toRightOf="@+id/btnperro"
android:background="@drawable/botonoveja"/>
<Button
android:id="@+id/btncaballo"
android:layout_
android:layout_
android:layout_alignTop="@+id/btnoveja"
android:layout_toRightOf="@+id/btnoveja"
android:background="@drawable/botonhorse" />
<Button
android:id="@+id/btngallina"
android:layout_
android:layout_
android:layout_alignTop="@+id/btncaballo"
android:layout_toRightOf="@+id/btncaballo"
android:background="@drawable/botongallina" />
</RelativeLayout>
Android 清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="es.quovo.learninganimals"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".GranjaActivity"
android:label="@string/title_activity_granja"
android:screenOrientation="landscape" >
</activity>
<activity
android:name=".SelvaActivity"
android:label="@string/title_activity_selva"
android:screenOrientation="landscape" >
</activity>
<activity
android:name=".OceanoActivity"
android:label="@string/title_activity_oceano"
android:screenOrientation="landscape" >
</activity>
<activity
android:name=".CieloActivity"
android:label="@string/title_activity_cielo"
android:screenOrientation="landscape" >
</activity>
<activity
android:name=".SabanaActivity"
android:label="@string/title_activity_sabana"
android:screenOrientation="landscape" >
</activity>
<activity
android:name=".CasaActivity"
android:label="@string/title_activity_casa"
android:screenOrientation="landscape" >
</activity>
</application>
</manifest>
【问题讨论】:
发布您的 LogCat 输出 尝试调试而不是运行,这样您就可以看到错误,然后发布 logCat 输出以便我们为您提供帮助 尝试在super.oncreate之前设置请求标题 【参考方案1】:requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
在 super.oncreate 之前设置这些行
【讨论】:
【参考方案2】:在活动顶部初始化您的媒体播放器,然后在整个项目中使用该媒体播放器
MediaPlayer mpa=new MediaPlayer();
mpa = MediaPlayer.create(getApplicationContext(), R.raw.perro);
【讨论】:
希望现在你的例外有所不同,现在你必须更新你的 Logcat以上是关于第二个活动没有打开的主要内容,如果未能解决你的问题,请参考以下文章
带有 2 个按钮的 Android 主要活动可在 webview 中打开第二个活动