2

My android app is using support v4 library:

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

And it is just created as a new project. When I run it on my phone and press the menu button, it crashes:

12-21 15:12:54.170 31705-31705/com.talkweb.woplus E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.NullPointerException at android.support.v7.app.ActionBarImplICS.getThemedContext(ActionBarImplICS.java:287) at android.support.v7.app.ActionBarImplJB.getThemedContext(ActionBarImplJB.java:20) at android.support.v7.app.ActionBarActivityDelegate.getMenuInflater(ActionBarActivityDelegate.java:98) at android.support.v7.app.ActionBarActivity.getMenuInflater(ActionBarActivity.java:71) at com.talkweb.woplus.HomeActivity.onCreateOptionsMenu(HomeActivity.java:35) at android.app.Activity.onCreatePanelMenu(Activity.java:2652)

code in HomeActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);  // I add this line
    setContentView(R.layout.activity_home);

    if (savedInstanceState == null) {
        webViewFragment = new WebViewFragment();
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, webViewFragment)
                .commit();
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.home, menu);  // line: 35
    return true;
}

styles.xml:

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat">
    </style>
</resources>

manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.talkweb.woplus">

    <uses-permission android:name="android.permission.INTERNET" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:theme="@style/AppTheme"
        android:label="@string/app_name">

        <activity
            android:name="com.talkweb.woplus.HomeActivity"
            android:label="@string/app_name">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

menu:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.talkweb.woplus.HomeActivity" >

    <item android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:orderInCategory="100"
        android:showAsAction="ifRoom|withText"
        app:showAsAction="ifRoom|withText" />
</menu>
JSPDeveloper01
  • 770
  • 2
  • 10
  • 25

2 Answers2

5

Based on analysis in the question "Pressing menu button causes crash in Activity with no ActionBar", this is because the action bar is null. And that's because of this:

requestWindowFeature(Window.FEATURE_NO_TITLE);  // I add this line

which removes the action bar among other things.

Now, your requirements are conflicting: you want a menu but you don't want an action bar. A "menu popup from bottom" is not something you can have, at least not using the standard Android components. You'll have to choose between:

  1. Action bar and menu, removing the FEATURE_NO_TITLE.

  2. No action bar, no menu, keeping the FEATURE_NO_TITLE.

Community
  • 1
  • 1
laalto
  • 150,114
  • 66
  • 286
  • 303
2

Thanks @laato, with your help I finally find out that all I need to do is make my HomeActivity NOT extends ActionBarActivity but normal Activity. Now my options menu shows up.

JSPDeveloper01
  • 770
  • 2
  • 10
  • 25