0

When attempting to call a new activity (settings) from the action bar, I get the error java.lang.NullPointerException. I'm not too sure where I'm going wrong, below is the code I am using. Everything compiles fine, when I test the application and press the "Settings" button, the app then crashes.

MainActivity.java

package liam.lbee.com.test1;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;



public class MainActivity extends ActionBarActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, 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) {
            startActivity(new Intent(getApplicationContext(),SettingsActivity.class));
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

Menu_Main.xml

<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=".MainActivity">



    <item android:id="@+id/action_settings"
        android:title="@string/action_settings"
        app:showAsAction="never" />


</menu>

AndroidManifest.xml

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            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=".SettingsActivity"
            android:label="@string/title_activity_settings"
            android:parentActivityName=".MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="liam.lbee.com.test1.MainActivity" />
        </activity>
    </application>

</manifest>

LogCat Files

java.lang.RuntimeException: Unable to start activity ComponentInfo{liam.lbee.com.test1/liam.lbee.com.test1.SettingsActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2350)
            at android.app.ActivityThread.access$800(ActivityThread.java:163)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5335)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at liam.lbee.com.test1.SettingsActivity.setupActionBar(SettingsActivity.java:58)
            at liam.lbee.com.test1.SettingsActivity.onCreate(SettingsActivity.java:48)
            at android.app.Activity.performCreate(Activity.java:5389)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2256)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2350)
            at android.app.ActivityThread.access$800(ActivityThread.java:163)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5335)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)

setupActionBar (As requested)

private void setupActionBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            // Show the Up button in the action bar.
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }
LBPLC
  • 1,570
  • 3
  • 27
  • 51
  • Which line is giving you the NullPointerException? – MFazio23 Feb 09 '15 at 20:59
  • @MFazio23 - **8585-8585/liam.lbee.com.test1 E/ViewRootImpl﹕ sendUserActionEvent() mView == null** is the first error in the log file when testing – LBPLC Feb 09 '15 at 21:03
  • 1
    In your logcat, there should be a stack trace that shows the NullPointerException and the class + method in which it occurred: For example: *java.lang.NullPointerException at [Class Name].[Method Name]([File name].java:74)* – MFazio23 Feb 09 '15 at 21:07
  • @MFazio23 See updated Question, hope that's what your looking for – LBPLC Feb 09 '15 at 21:09
  • Yep, that's it. Can you post the *setupActionBar()* method within your SettingsActivity? The intent to the SettingsActivity works fine, it's the initialization there that fails. – MFazio23 Feb 09 '15 at 21:11
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – njzk2 Feb 09 '15 at 21:13
  • @njzk2 - The error itself is a duplicate, but how to fix it is not. – MFazio23 Feb 09 '15 at 21:17
  • 1
    @SilverShotBee - Try using *getSupportActionBar()* instead of *getActionBar()*. This is assuming your SettingsActivity is extending ActionBarActivity (as your MainActivity is doing) – MFazio23 Feb 09 '15 at 21:19
  • 1
    @MFazio23: the whole point of the linked question is to explain the debug process so that the OP can get the basic troubleshooting done before asking the question. (Typically to avoid the case where the code where the exception is thrown is only posted after you had to *ask* for it) – njzk2 Feb 09 '15 at 21:23
  • 1
    a more exact duplicate would be http://stackoverflow.com/questions/26435231/getactionbar-returns-null-appcompat-v7-21 then – njzk2 Feb 09 '15 at 21:25
  • @njzk2 - The first one was a valid post, I just didn't feel like it was a duplicate. The second one, however, is exactly on. – MFazio23 Feb 09 '15 at 21:28
  • @MFazio23 when I attempt to change it to **getSupportActionBar()** I encounter the error: "Unable to resolve method" – LBPLC Feb 09 '15 at 21:32
  • 1
    @SilverShotBee - Make sure your SettingsActivity is extending ActionBarActivity, since that's the class with this method. – MFazio23 Feb 09 '15 at 21:33
  • @MFazio23 Thank you. Got there in the end! I'd accept an answer if posted. Cheers – LBPLC Feb 09 '15 at 21:43
  • @SilverShotBee - I added a summary solution for future reference. – MFazio23 Feb 09 '15 at 21:46

1 Answers1

1

Final summary: The issue was with the SettingsActivity not using the getSupportActionBar() method from ActionBarActivity. Once the class was updated to extend from ActionBarActivity and the called method was changed, this worked.

Another example of this: getActionBar() returns Null (AppCompat-v7 21)

Community
  • 1
  • 1
MFazio23
  • 1,261
  • 12
  • 21