Reputation:
The auto-generated OnCreateOptionsMenu
in a certain activity, causes the app to crash when I press the options button in the device, despite, the same action does not crash the app while accessing others activities of the same app.
One of the logcat
's complains is NPE
, and I do not know what causes this error. All the xml files are correct.
Java Code:
@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>
Manifest file:
<?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 output:
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)
Upvotes: 1
Views: 761
Reputation: 17085
causes the app to crash when I press the options button in the device
Issue is that since you have set the Window.FEATURE_NO_TITLE
to the ActionBarActivity
, the ActionBar
is not created for this Activity
. But your trying to inflate option menus
.
Since ActionBar
is not created for this Activity, when trying to call getMenuInflater
, the ActionBar
is null
FIX: Change the AddNewLocationActivity
to extend Activity
instead of ActionBarActivity
public class AddNewLocationActivity extends ActionBarActivity {
to
public class AddNewLocationActivity extends Activity {
You can still access the Menus
using device option key.
Upvotes: 2