11
votes

I've been stuck on this, checked the official guidance, etc. Any tutorials/what are the steps to change from ActionBar to ActionBarCompat (for the Toolbar and support of older versions? I've imported appcompat-v7:21.0.+, tried

getSupportActionBar(); and

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);

setSupportActionBar(toolbar);

changed theme to appcompat theme in styles... Any common errors to note or ideas?

Keep getting this kind of error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.battery.plusfree/com.battery.plusfree.MainCollectionActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5146)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
        at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.battery.plusfree.MainCollectionActivity.onCreate(MainCollectionActivity.java:133)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5146)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
            at dalvik.system.NativeStart.main(Native Method)
3
post ur MainColelctionActivity - Hirak Chhatbar
Post your Activity and layout. The must read about Toolbar is: android-developers.blogspot.it/2014/10/… - Gabriele Mariotti
Can you also post the layout you're inflating? Could it be that it's missing a <android.support.v7.widget.Toolbar> hence the NPE? - IAmKale
What appcompat theme are you using? Your activity need to use the appcompat.light.noactionbar theme (or sonething like that, do not remember the exact name right now) :) - Alioooop

3 Answers

13
votes

I've learned with my application is that you can't use the toolbar and the actionbar at the same time. So that might be where your issue lies, but I'm not sure because you didn't post your code, but using the styles.xml file you have to specify either or like so:

<style name="AppTheme.Base" parent="Theme.AppCompat">
    <item name="colorPrimary">@color/mycolorprimary</item>
    <item name="colorPrimaryDark">@color/mycolorprimarydark</item>
    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
</style>

Activity.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- dont use this if you only want to use the actionbar instead -->

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:elevation="5dp"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary" />

</LinearLayout>

Activity.java:

    public class MyActivity extends ActionBarActivity {

       Toolbar mToolbar;

       public void onCreate(Bundle savedInstanceState) {

          // configure toolbar stuff
          setSupportActionBar(mToolbar);

          // or if you don't want to use the toolbar
          // then change the style values accordingly
          // and then you can run getSupportActionBar() instead

       }

    }

Reference: here .. hope this helps!

4
votes

@ Andrew Butler: good answer, though there is a standard way to not use an actionbar.

<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    <!-- Customize your theme here.>
</style>

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here.>
</style>
1
votes

Set the parent of your theme to one without an action bar. For example

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/mycolorprimary</item>
    <item name="colorPrimaryDark">@color/mycolorprimarydark</item>
    <item name="android:windowNoTitle">true</item>
</style>

There would be no need to set windowActionBar to false anymore.