我在活动栏上点了一个按钮,将您移动到另一个名为“Shop”的页面,但是当我按下按钮时,我的应用程序崩溃了。
这是按钮代码:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.PAUSE:
openPopUP();
case R.id.SHOP:
openSHOP();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
这是openSHOP操作:
private void openSHOP() {
mPlayer.pause();
Intent Intent = new Intent(FirstActivity.this, Shop.class);
startActivity(Intent);
Context context = getApplicationContext();
CharSequence text = "You are at the shop";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
这是商店类:
package com.example.whitetap;
import com.example.whitetap.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class Shop extends Activity implements OnClickListener {
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private Button button5;
private Button button6;
private MediaPlayer mPlayer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shoplayout);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(this);
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(this);
button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(this);
button4 = (Button) findViewById(R.id.button4);
button4.setOnClickListener(this);
button5 = (Button) findViewById(R.id.button5);
button5.setOnClickListener(this);
button6 = (Button) findViewById(R.id.button6);
button6.setOnClickListener(this);
//music
SoundPool sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
int iTmp = sp.load(getBaseContext(), R.raw.windows_8_notify, 1);
sp.play(iTmp, 1, 1, 0, 0, 1);
mPlayer = MediaPlayer.create(getBaseContext(), R.raw.windows_8_notify);
mPlayer.start();
mPlayer.setLooping(true); }
public void onClick(View v) {
int id = v.getId();
if (id == R.id.button6) {
mPlayer.pause();
Intent Intent = new Intent(Shop.this, FirstActivity.class);
startActivity(Intent);
Context context = getApplicationContext();
CharSequence text = "You went back to the game";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
}
logcat的:
06-28 20:18:31.522: I/ViewRootImpl(7191): ViewRoot's Touch Event : ACTION_DOWN
06-28 20:18:31.592: I/ViewRootImpl(7191): ViewRoot's Touch Event : ACTION_UP
06-28 20:18:31.602: V/MediaPlayer[Native](7191): pause
06-28 20:18:31.602: V/MediaPlayer[Native](7191): message received msg=7, ext1=0, ext2=0
06-28 20:18:31.652: D/MediaPlayer[Native](7191): calling notify_player_state: 32
06-28 20:18:31.652: V/MediaPlayer[Native](7191): unrecognized message: (7, 0, 0)
06-28 20:18:31.652: V/MediaPlayer[Native](7191): callback application
06-28 20:18:31.652: V/MediaPlayer[Native](7191): back from callback
06-28 20:18:31.662: I/ActivityManager(7191): Timeline: Activity_launch_request id:com.example.whitetap time:94790790
06-28 20:18:31.662: D/AndroidRuntime(7191): Shutting down VM
06-28 20:18:31.662: W/dalvikvm(7191): threadid=1: thread exiting with uncaught exception (group=0x41d69e48)
06-28 20:18:31.662: E/AndroidRuntime(7191): FATAL EXCEPTION: main
06-28 20:18:31.662: E/AndroidRuntime(7191): Process: com.example.whitetap, PID: 7191
06-28 20:18:31.662: E/AndroidRuntime(7191): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.whitetap/com.example.whitetap.Shop}; have you declared this activity in your AndroidManifest.xml?
06-28 20:18:31.662: E/AndroidRuntime(7191): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
06-28 20:18:31.662: E/AndroidRuntime(7191): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
06-28 20:18:31.662: E/AndroidRuntime(7191): at android.app.Activity.startActivityForResult(Activity.java:3468)
06-28 20:18:31.662: E/AndroidRuntime(7191): at android.app.Activity.startActivityForResult(Activity.java:3429)
06-28 20:18:31.662: E/AndroidRuntime(7191): at android.app.Activity.startActivity(Activity.java:3671)
06-28 20:18:31.662: E/AndroidRuntime(7191): at android.app.Activity.startActivity(Activity.java:3639)
06-28 20:18:31.662: E/AndroidRuntime(7191): at com.example.whitetap.FirstActivity.openSHOP(FirstActivity.java:104)
06-28 20:18:31.662: E/AndroidRuntime(7191): at com.example.whitetap.FirstActivity.onOptionsItemSelected(FirstActivity.java:95)
06-28 20:18:31.662: E/AndroidRuntime(7191): at android.app.Activity.onMenuItemSelected(Activity.java:2644)
06-28 20:18:31.662: E/AndroidRuntime(7191): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1095)
06-28 20:18:31.662: E/AndroidRuntime(7191): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:741)
06-28 20:18:31.662: E/AndroidRuntime(7191): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
06-28 20:18:31.662: E/AndroidRuntime(7191): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:884)
06-28 20:18:31.662: E/AndroidRuntime(7191): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
06-28 20:18:31.662: E/AndroidRuntime(7191): at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:546)
06-28 20:18:31.662: E/AndroidRuntime(7191): at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:118)
06-28 20:18:31.662: E/AndroidRuntime(7191): at android.view.View.performClick(View.java:4461)
06-28 20:18:31.662: E/AndroidRuntime(7191): at android.view.View$PerformClick.run(View.java:18525)
06-28 20:18:31.662: E/AndroidRuntime(7191): at android.os.Handler.handleCallback(Handler.java:733)
06-28 20:18:31.662: E/AndroidRuntime(7191): at android.os.Handler.dispatchMessage(Handler.java:95)
06-28 20:18:31.662: E/AndroidRuntime(7191): at android.os.Looper.loop(Looper.java:136)
06-28 20:18:31.662: E/AndroidRuntime(7191): at android.app.ActivityThread.main(ActivityThread.java:5118)
06-28 20:18:31.662: E/AndroidRuntime(7191): at java.lang.reflect.Method.invokeNative(Native Method)
06-28 20:18:31.662: E/AndroidRuntime(7191): at java.lang.reflect.Method.invoke(Method.java:515)
06-28 20:18:31.662: E/AndroidRuntime(7191): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
06-28 20:18:31.662: E/AndroidRuntime(7191): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
06-28 20:18:31.662: E/AndroidRuntime(7191): at dalvik.system.NativeStart.main(Native Method)
06-28 20:18:41.652: V/MediaPlayer[Native](7191): message received msg=8, ext1=0, ext2=0
06-28 20:18:41.652: V/MediaPlayer[Native](7191): unrecognized message: (8, 0, 0)
06-28 20:18:41.652: V/MediaPlayer[Native](7191): callback application
06-28 20:18:41.652: V/MediaPlayer[Native](7191): back from callback
06-28 20:18:41.662: V/AudioSystem(7191): ioConfigChanged() event 1, ioHandle 2573
06-28 20:18:41.662: V/AudioSystem(7191): ioConfigChanged() output 2573 closed
为什么我的应用会因点击而崩溃?
答案 0 :(得分:2)
如异常所述:
“无法找到显式活动类{com.example.whitetap / com.example.whitetap.Shop};您是否在AndroidManifest.xml中声明了此活动?”
您是否将商店类指定为清单AndroidManifest.xml中的活动?
答案 1 :(得分:0)
因为您必须在AndroidManifest.xml中声明您的活动
<activity
android:name="activityName"
/>
确保路径和包裹正确