OnOptionsMenu导致应用程序崩溃

时间:2014-05-11 21:08:09

标签: android xml oncreateoptionsmenu

当我按下设备中的选项按钮时,某个活动中自动生成的OnCreateOptionsMenu会导致应用崩溃,尽管同样的操作在访问同一应用的其他活动时不会导致应用崩溃

logcat的一个投诉是NPE,我不知道导致此错误的原因。所有的xml文件都是正确的。

Java代码:

     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        requestWindowFeature( Window.FEATURE_NO_TITLE );
        getWindow().setFormat(PixelFormat.TRANSPARENT);         
        getWindow().setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN,
                                WindowManager.LayoutParams.FLAG_FULLSCREEN );
        setContentView(R.layout.add_new_location_activity);

    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

add_new_location_activity.xml

 <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent">

          <LinearLayout 
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="horizontal"
              android:background="@color/titleBackGroundColor">              
              <RelativeLayout 
                  android:id="@+id/RelativeLayout00"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:layout_gravity="top|center_horizontal">
                  <TextView 
                      android:id="@+id/addLocationActivityTitleID"
                      android:layout_width="fill_parent"
                      android:layout_height="wrap_content"
                      android:gravity="center"
                      android:textSize="@dimen/titlesTextSize"
                      android:text="@string/addLocationActivityTitle"
                      android:textColor="@color/titleColor">
                  </TextView>
              </RelativeLayout>
          </LinearLayout>

          <LinearLayout 
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="horizontal">              
              <RelativeLayout 
                  android:id="@+id/RelativeLayout01"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"     
                  android:layout_marginTop="@dimen/marginTop"
                  android:background="@color/bodyBackGroundColor">
                  <TextView 
                      android:id="@+id/editTextLabelID"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_alignParentLeft="true"
                      android:text="@string/editTextLabel"
                      android:gravity="left"
                      android:textSize="@dimen/labelsTextSizes"
                      android:textColor="@color/contentsColor">"
                  </TextView>
                  <EditText 
                      android:id="@+id/editTextID"
                      android:layout_height="wrap_content"
                      android:layout_width="match_parent"
                      android:layout_toRightOf="@+id/editTextLabelID"
                      android:textColor="@color/contentsColor"
                      android:inputType="text">
                  </EditText>
             </RelativeLayout>
         </LinearLayout>         
</FrameLayout>

清单文件:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.meetingpointlocator_03"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.meetingpointlocator_03.Intro"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.meetingpointlocator_03.MainMenuActivity"
            android:label="@string/title_activity_mpl" >
        </activity>
        <activity
            android:name="com.example.meetingpointlocator_03.AddNewLocationActivity"
            android:label="@string/title_activity_add_new_location" >
        </activity>
    </application>

</manifest>

Logcat输出:

    05-11 23:27:28.591: E/InputEventSender(2636): Exception dispatching finished signal.
    05-11 23:27:28.596: E/MessageQueue-JNI(2636): Exception in MessageQueue callback: 
    handleReceiveCallback
    05-11 23:27:28.626: E/MessageQueue-JNI(2636): java.lang.NullPointerException
    05-11 23:27:28.626: E/MessageQueue-JNI(2636):   at  
    android.support.v7.app.ActionBarImplICS.getThemedContext(ActionBarImplICS.java:287)
    05-11 23:27:28.626: E/MessageQueue-JNI(2636):   at 
    android.support.v7.app.ActionBarImplJB.getThemedContext(ActionBarImplJB.java:20)
    05-11 23:27:28.626: E/MessageQueue-JNI(2636):   at android.support.v7.app.ActionBarActivityDelegate.getMenuInflater(ActionBarActivityDelegate.    java:98)
    05-11 23:27:28.626: E/MessageQueue-JNI(2636):   at 
    android.support.v7.app.ActionBarActivity.getMenuInflater(ActionBarActivity.java:71)
    05-11 23:27:28.626: E/MessageQueue-JNI(2636):   at    
com.example.meetingpointlocator_03.AddNewLocationActivity.onCreateOptionsMenu(AddNewLocatio    nActivity.java:35)
    05-11 23:27:28.626: E/MessageQueue-JNI(2636):   at    
    android.app.Activity.onCreatePanelMenu(Activity.java:2578)
    05-11 23:27:28.626: E/MessageQueue-JNI(2636):   at 
    android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:224)
    05-11 23:27:28.626: E/MessageQueue-JNI(2636):   at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:232)
    05-11 23:27:28.626: E/MessageQueue-JNI(2636):   at android.support.v7.app.ActionBarActivityDelegateICS.onCreatePanelMenu(ActionBarActivityDele    gateICS.java:147)
    05-11 23:27:28.626: E/MessageQueue-JNI(2636):   at  android.support.v7.app.ActionBarActivity.onCreatePanelMenu(ActionBarActivity.java:199)
    05-11 23:27:28.626: E/MessageQueue-JNI(2636):   at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onCreatePanelMenu    (ActionBarActivityDelegateICS.java:285)
    05-11 23:27:28.626: E/MessageQueue-JNI(2636):   at   com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:507)
    05-11 23:27:28.626: E/MessageQueue-JNI(2636):   at com.android.internal.policy.impl.PhoneWindow.onKeyDownPanel(PhoneWindow.java:964)
    05-11 23:27:28.626: E/MessageQueue-JNI(2636):   at com.android.internal.policy.impl.PhoneWindow.onKeyDown(PhoneWindow.java:1670)
    05-11 23:27:28.626: E/MessageQueue-JNI(2636):   at  com. android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2227)

1 个答案:

答案 0 :(得分:2)

 causes the app to crash when I press the options button in the device

问题是,由于您已将Window.FEATURE_NO_TITLE设置为ActionBarActivity,因此未为此ActionBar创建Activity。但你试图inflate option menus

由于未为此活动创建ActionBar,因此在尝试调用getMenuInflater时,ActionBar为空

FIX:AddNewLocationActivity更改为Activity而不是ActionBarActivity

public class AddNewLocationActivity extends ActionBarActivity {

public class AddNewLocationActivity extends Activity {

您仍然可以使用设备选项密钥访问Menus