Error trying to set custom ActionBar: java.lang.NullPointerException

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.java class

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);
    }
}

I'm not sure how to do this,

Att. Jeiferson

    
asked by anonymous 07.01.2016 / 17:31

1 answer

4

You are using android.support.v7.app so your Activity should inherit from AppCompatActivity .

Replace the line

public class MainActivity extends Activity {

by

public class MainActivity extends AppCompatActivity {

and instead of

getActionBar();

use

getSupportActionBar();
    
07.01.2016 / 19:16