Null pointer exception at action bar

2019-08-10 16:45发布

问题:

I am getting null pointer exception at action bar. I am using getActionBar().Following are the errors..I am extending Activity

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.peoplecloud.guggu/com.peoplecloud.guggu.activity.LandingActivity_}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211)
            at android.app.ActivityThread.access$600(ActivityThread.java:149)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:153)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            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:821)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.peoplecloud.guggu.activity.LandingActivity.onCreate(LandingActivity.java:96)
            at com.peoplecloud.guggu.activity.LandingActivity_.onCreate(LandingActivity_.java:29)
            at android.app.Activity.performCreate(Activity.java:5020)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211)
            at android.app.ActivityThread.access$600(ActivityThread.java:149)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:153)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)

in following lines-

java:96 actionBar.setTitle(mTitle);

回答1:

if you are using Activity then use below code

ActionBar actionBar = getActionBar();
actionBar.hide();

or you are using ActionBarActivity then getSupportActionBar()

make sure that you have not used NoTitlebar Theme.

EDIT

use

    Toolbar mToolbar = (Toolbar)findViewById(R.id.toolbar);
    mToolbar.setTitle(mTitle);

and add in your layout xml file :

create seprate file

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#E3493B"
android:minHeight="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.ActionBar" >

include it in your xml layout file

<include layout="@layout/toolbar" />


回答2:

two things:

if you are using support framework the use "getSupportActionBar()" to get the actionbar. But i see you are using an activity here. Your activity should extend ActionbarActivity to use actionbar eith er directly or using a fragmentactivity.

without that you wont get an actionbar in support packages..



回答3:

The layout of the toolbar should be added to the activity layout

<include layout="@layout/toolbar" />