Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ActionBar not visible

I've updated the API 21, tools and extras up to date, the ActionBar in some projects has gone invisible. I did this following code, but app crashes showing NullPointerException.

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

Logcat Output

02-23 18:03:36.269: E/AndroidRuntime(3327): FATAL EXCEPTION: main 02-23 18:03:36.269: E/AndroidRuntime(3327): Process: com.example.twaapp, PID: 3327 02-23 18:03:36.269: E/AndroidRuntime(3327): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.twaapp/com.example.twaapp.MainActivity}: java.lang.NullPointerException 02-23 18:03:36.269: E/AndroidRuntime(3327): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198) 02-23 18:03:36.269: E/AndroidRuntime(3327): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257) 02-23 18:03:36.269: E/AndroidRuntime(3327): at android.app.ActivityThread.access$800(ActivityThread.java:139) 02-23 18:03:36.269: E/AndroidRuntime(3327): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210) 02-23 18:03:36.269: E/AndroidRuntime(3327): at android.os.Handler.dispatchMessage(Handler.java:102) 02-23 18:03:36.269: E/AndroidRuntime(3327): at android.os.Looper.loop(Looper.java:136) 02-23 18:03:36.269: E/AndroidRuntime(3327): at android.app.ActivityThread.main(ActivityThread.java:5086) 02-23 18:03:36.269: E/AndroidRuntime(3327): at java.lang.reflect.Method.invokeNative(Native Method) 02-23 18:03:36.269: E/AndroidRuntime(3327): at java.lang.reflect.Method.invoke(Method.java:515) 02-23 18:03:36.269: E/AndroidRuntime(3327): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 02-23 18:03:36.269: E/AndroidRuntime(3327): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 02-23 18:03:36.269: E/AndroidRuntime(3327): at dalvik.system.NativeStart.main(Native Method) 02-23 18:03:36.269: E/AndroidRuntime(3327): Caused by: java.lang.NullPointerException 02-23 18:03:36.269: E/AndroidRuntime(3327): at com.example.twaapp.MainActivity.onCreate(MainActivity.java:82) 02-23 18:03:36.269: E/AndroidRuntime(3327): at android.app.Activity.performCreate(Activity.java:5248) 02-23 18:03:36.269: E/AndroidRuntime(3327): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110) 02-23 18:03:36.269: E/AndroidRuntime(3327): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162) 02-23 18:03:36.269: E/AndroidRuntime(3327): ... 11 more

How to make AB visible?

like image 716
ABI Avatar asked Feb 23 '26 23:02

ABI


1 Answers

Your Activity class should extend like this:

public class MainActivity extends ActionBarActivity{

    ....

}

instead of

public class MainActivity extends FragmentActivity{

    ....

}

Also, you need to call

ActionBar = getSupportActionBar();

instead of

ActionBar = getActionBar();

Try this. This will work.

like image 142
Y.S Avatar answered Feb 26 '26 12:02

Y.S



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!