Error when trying to set custom Actionbar: java.lang.Nullpointerexception

Asked

Viewed 54 times

1

I’m trying to customize my Actionbar but whenever I try to emulate the application it closes and returns me the following error in Logcat:

01-07 16:21:57.092 1388-1388/br.com.jeiferson.agenda E/AndroidRuntime: FATAL EXCEPTION: main
01-07 16:21:57.092 1388-1388/br.com.jeiferson.agenda E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.jeiferson.agenda/br.com.jeiferson.agenda.MainActivity}: java.lang.NullPointerException
01-07 16:21:57.092 1388-1388/br.com.jeiferson.agenda E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-07 16:21:57.092 1388-1388/br.com.jeiferson.agenda E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-07 16:21:57.092 1388-1388/br.com.jeiferson.agenda E/AndroidRuntime:     at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-07 16:21:57.092 1388-1388/br.com.jeiferson.agenda E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-07 16:21:57.092 1388-1388/br.com.jeiferson.agenda E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99)
01-07 16:21:57.092 1388-1388/br.com.jeiferson.agenda E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:137)
01-07 16:21:57.092 1388-1388/br.com.jeiferson.agenda E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5041)
01-07 16:21:57.092 1388-1388/br.com.jeiferson.agenda E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
01-07 16:21:57.092 1388-1388/br.com.jeiferson.agenda E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:511)
01-07 16:21:57.092 1388-1388/br.com.jeiferson.agenda E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-07 16:21:57.092 1388-1388/br.com.jeiferson.agenda E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-07 16:21:57.092 1388-1388/br.com.jeiferson.agenda E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
01-07 16:21:57.092 1388-1388/br.com.jeiferson.agenda E/AndroidRuntime:  Caused by: java.lang.NullPointerException
01-07 16:21:57.092 1388-1388/br.com.jeiferson.agenda E/AndroidRuntime:     at br.com.jeiferson.agenda.CustomActionBar.setCustomActionBar(CustomActionBar.java:13)
01-07 16:21:57.092 1388-1388/br.com.jeiferson.agenda E/AndroidRuntime:     at br.com.jeiferson.agenda.MainActivity.onCreate(MainActivity.java:22)
01-07 16:21:57.092 1388-1388/br.com.jeiferson.agenda E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5104)
01-07 16:21:57.092 1388-1388/br.com.jeiferson.agenda E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-07 16:21:57.092 1388-1388/br.com.jeiferson.agenda E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-07 16:21:57.092 1388-1388/br.com.jeiferson.agenda E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
01-07 16:21:57.092 1388-1388/br.com.jeiferson.agenda E/AndroidRuntime:     at android.app.ActivityThread.access$600(ActivityThread.java:141) 
01-07 16:21:57.092 1388-1388/br.com.jeiferson.agenda E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
01-07 16:21:57.092 1388-1388/br.com.jeiferson.agenda E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99) 
01-07 16:21:57.092 1388-1388/br.com.jeiferson.agenda E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:137) 
01-07 16:21:57.092 1388-1388/br.com.jeiferson.agenda E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5041) 
01-07 16:21:57.092 1388-1388/br.com.jeiferson.agenda E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
01-07 16:21:57.092 1388-1388/br.com.jeiferson.agenda E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:511) 
01-07 16:21:57.092 1388-1388/br.com.jeiferson.agenda E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
01-07 16:21:57.092 1388-1388/br.com.jeiferson.agenda E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
01-07 16:21:57.092 1388-1388/br.com.jeiferson.agenda E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 

My Mainactivity.java class

public class MainActivity extends Activity {

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

        ActionBar mActionBar = getActionBar();
        LayoutInflater mInflater = LayoutInflater.from(this);

        CustomActionBar.setCustomActionBar(mActionBar, mInflater);
    }
}

My Customactionbar class.java

public class CustomActionBar {

    public static void setCustomActionBar(ActionBar acBar, LayoutInflater ltInflater) {
        acBar.setDisplayShowHomeEnabled(false);
        acBar.setDisplayShowTitleEnabled(false);

        View mCustomView = ltInflater.inflate(R.layout.custom_actionbar, null);

        acBar.setCustomView(mCustomView);
        acBar.setDisplayShowCustomEnabled(true);
    }
}

From what I saw he was receiving a null object when I tried to pass my actionbar to him, someone would know how to solve?

Att. Jeiferson

  • What is the Theme the application is using?

  • try using getSupportActionBar()

  • @ramaral este Aki: android:Theme="@style/Apptheme"

  • @franM can not implement this function, error...

  • See in res/values/Styles.xml what is the Parent of Apptheme

  • @ramaral Parent="Theme.AppCompat.Light.Darkactionbar"

Show 1 more comment

1 answer

4


You are wearing android.support.v7.app therefore its Activity must inherit from AppCompatActivity.

Replace the line

public class MainActivity extends Activity {

for

public class MainActivity extends AppCompatActivity {

and instead of

getActionBar();

use

getSupportActionBar();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.