菜单inflater崩溃了,如何解决?

时间:2013-03-06 16:49:23

标签: java android logcat

我正在编写一个应用程序,我在主菜单类中有一个菜单Inflater,我在这个菜单中有三个选项,分别是Exit,AboutUs和Preferences。退出菜单项有效,但当我触摸另外两个时,应用程序崩溃。

任何修复它的帮助都会非常感激,我已经查看了清单,对我而言,它看起来都是正确的,所有类都是定义的。

MenuInflater代码: -

@Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
// TODO Auto-generated method stub
 super.onCreateOptionsMenu(menu);
 MenuInflater blowUp = getMenuInflater();
 blowUp.inflate(R.menu.main_menu, menu);

 return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case R.id.aboutUs:
    Intent i = new Intent(MainMenu.this, AboutUs.class);
    startActivity(i);
    break;
case R.id.prefs:
    Intent p = new Intent(MainMenu.this, Prefs.class);
    startActivity(p);
    break;
case R.id.exit:
    finish();
    break;
}
return false;
}
}

每个项目的课程: -

public class AboutUs extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.about);
}
}



public class Prefs extends PreferenceActivity{

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.prefs);
}

}

logcat的: -

03-06 16:39:21.040: E/AndroidRuntime(23894): FATAL EXCEPTION: main
03-06 16:39:21.040: E/AndroidRuntime(23894): android.content.ActivityNotFoundException:  Unable to find explicit activity class {coursework.android.travelapp/coursework.android.travelapp.AboutUs}; have you declared this   activity in your AndroidManifest.xml?
03-06 16:39:21.040: E/AndroidRuntime(23894):    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1545)
03-06 16:39:21.040: E/AndroidRuntime(23894):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1420)
03-06 16:39:21.040: E/AndroidRuntime(23894):    at android.app.Activity.startActivityForResult(Activity.java:3446)
03-06 16:39:21.040: E/AndroidRuntime(23894):    at  android.app.Activity.startActivityForResult(Activity.java:3407)
03-06 16:39:21.040: E/AndroidRuntime(23894):    at android.app.Activity.startActivity(Activity.java:3617)
03-06 16:39:21.040: E/AndroidRuntime(23894):    at android.app.Activity.startActivity(Activity.java:3585)
03-06 16:39:21.040: E/AndroidRuntime(23894):    at coursework.android.travelapp.MainMenu.onOptionsItemSelected(MainMenu.java:63)
03-06 16:39:21.040: E/AndroidRuntime(23894):    at android.app.Activity.onMenuItemSelected(Activity.java:2629)
03-06 16:39:21.040: E/AndroidRuntime(23894):    at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1047)
03-06 16:39:21.040: E/AndroidRuntime(23894):    at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
03-06 16:39:21.040: E/AndroidRuntime(23894):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
03-06 16:39:21.040: E/AndroidRuntime(23894):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
03-06 16:39:21.040: E/AndroidRuntime(23894):    at com.android.internal.view.menu.ListMenuPresenter.onItemClick(ListMenuPresenter.java:180)
03-06 16:39:21.040: E/AndroidRuntime(23894):    at android.widget.AdapterView.performItemClick(AdapterView.java:301)
03-06 16:39:21.040: E/AndroidRuntime(23894):    at android.widget.AbsListView.performItemClick(AbsListView.java:1276)
03-06 16:39:21.040: E/AndroidRuntime(23894):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:3067)
03-06 16:39:21.040: E/AndroidRuntime(23894):    at android.widget.AbsListView$1.run(AbsListView.java:3963)
03-06 16:39:21.040: E/AndroidRuntime(23894):    at android.os.Handler.handleCallback(Handler.java:615)
03-06 16:39:21.040: E/AndroidRuntime(23894):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-06 16:39:21.040: E/AndroidRuntime(23894):    at android.os.Looper.loop(Looper.java:137)
03-06 16:39:21.040: E/AndroidRuntime(23894):    at android.app.ActivityThread.main(ActivityThread.java:4898)
03-06 16:39:21.040: E/AndroidRuntime(23894):    at java.lang.reflect.Method.invokeNative(Native Method)
03-06 16:39:21.040: E/AndroidRuntime(23894):    at java.lang.reflect.Method.invoke(Method.java:511)
03-06 16:39:21.040: E/AndroidRuntime(23894):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
03-06 16:39:21.040: E/AndroidRuntime(23894):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
03-06 16:39:21.040: E/AndroidRuntime(23894):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

  

.ActivityNotFoundException:无法找到显式活动类

当您未在androidmanifest.xml

中定义活动时,会发生此异常

将其定义为它。

<activity android:name=".AboutUs" android:label="@string/app_name">

答案 1 :(得分:1)

从您的LogCat:

android.content.ActivityNotFoundException:  Unable to find explicit activity class 
    {coursework.android.travelapp/coursework.android.travelapp.AboutUs}; 
    have you declared this activity in your AndroidManifest.xml?
你有吗?我推荐它。