S
S
Sergey Semenko2015-04-15 19:45:31
Java
Sergey Semenko, 2015-04-15 19:45:31

Why is the java.lang.NullPointerException thrown in this case?

log cat

04-15 19:44:50.534  24717-24717/ru.redozote.spaces E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{ru.redozote.spaces/ru.redozote.spaces.SettingsActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2463)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
            at android.app.ActivityThread.access$600(ActivityThread.java:162)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1366)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:158)
            at android.app.ActivityThread.main(ActivityThread.java:5751)
            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:1083)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at ru.redozote.spaces.SettingsActivity.setupActionBar(SettingsActivity.java:58)
            at ru.redozote.spaces.SettingsActivity.onCreate(SettingsActivity.java:48)
            at android.app.Activity.performCreate(Activity.java:5165)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1103)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2419)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
            at android.app.ActivityThread.access$600(ActivityThread.java:162)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1366)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:158)
            at android.app.ActivityThread.main(ActivityThread.java:5751)
            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:1083)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
            at dalvik.system.NativeStart.main(Native Method)

MainActivity
@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_settings:
                Intent intent = new Intent(this, SettingsActivity.class);
                startActivity(intent);
            break;
        }

        return super.onOptionsItemSelected(item);
    }

AndroidMainfest
<activity
            android:name=".SettingsActivity"
            android:label="@string/title_activity_settings"
            android:parentActivityName=".MainActivity" >
        </activity>

SettingsActivty was created through AndroidStudio, I did not change anything there, if you need something from there - write.
UPD#1
I found an error, it is in this method
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void setupActionBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            // Show the Up button in the action bar.
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

Namely, because of this line, an error occurs
getActionBar().setDisplayHomeAsUpEnabled(true);
Why and how to solve the problem, but leave the "back" button?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2015-04-15
@abler98

Probably getActionBar() == Null
I did this

// Set up the action bar.
        ActionBar mActionBar = getSupportActionBar();
        mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        mActionBar.setDisplayShowCustomEnabled(true);
        mActionBar.setDisplayShowHomeEnabled(true);
        mActionBar.setDisplayHomeAsUpEnabled(true);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question