onOptionsItemSelected的致命异常

时间:2013-12-08 22:47:55

标签: java android

使用Android Studio我为我的应用创建settings(这是我第一次这样做)。我对onOptionsItemSelected有疑问,我不知道如何继续。 这是我的LogCat

FATAL EXCEPTION: main
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.ultimate.app.test/com.ultimate.app.SettingsActivity}; have you declared this activity in your AndroidManifest.xml? <!-- I have declareted it -->
        at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1618)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
        at android.app.Activity.startActivityFromChild(Activity.java:3843)
        at android.app.Activity.startActivityFromChild(Activity.java:3819)
        at android.app.Activity.startActivityForResult(Activity.java:3395)
        at android.app.Activity.startActivityForResult(Activity.java:3331)
        at android.app.Activity.startActivity(Activity.java:3566)
        at android.app.Activity.startActivity(Activity.java:3534)
        at com.ultimate.app.Tab1Activity.onOptionsItemSelected(Tab1Activity.java:106)
        at android.app.Activity.onMenuItemSelected(Activity.java:2548)
        at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:980)
        at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
        at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
        at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
        at com.android.internal.view.menu.ListMenuPresenter.onItemClick(ListMenuPresenter.java:166)
        at android.widget.AdapterView.performItemClick(AdapterView.java:298)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
        at android.widget.AbsListView$1.run(AbsListView.java:3423)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5191)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
        at dalvik.system.NativeStart.main(Native Method)

manifest

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.NoTitleBar">
    <activity
        android:name=".MainActivity"
        android:screenOrientation="portrait"
        >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    ...
    <activity android:name=".SettingsActivity"
        android:screenOrientation="portrait"/>
</application>

这就是我java.class中的内容:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int i = item.getItemId();
    if (i == R.id.actiorn) {
        Intent intent = new Intent(Tab1Activity.this,SettingsActivity.class);
        Tab1Activity.this.startActivity(intent);
        finish();
    }
    return true;

    }

1 个答案:

答案 0 :(得分:1)

试试这个:

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar">
<activity
    android:name=".MainActivity"
    android:screenOrientation="portrait"
    >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
...
<activity android:name="com.ultimate.app.SettingsActivity"
    android:screenOrientation="portrait"/>