I am currently trying to implement a navigation drawer in my app. I am having an issue with the Toolbar being null. I have done many searches online and tried many different solutions to no avail. I have not finished writing all the code but I can't launch my app to check the navigation drawer. Can anyone see where I went wrong?
If there is anything else I need to post, let me know.
I included this on all my activity screens
<include android:id="@+id/app_bar" layout="@layout/app_bar"/>
app_bar.xml
<?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" app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/ThemeOverlay.AppCompat.Dark" > </android.support.v7.widget.Toolbar>
line 72 of MainActivity
setSupportActionBar(mToolbar);
Main Activity
public class Trans_List extends ActionBarActivity { private Toolbar mToolbar; private CharSequence mTitle; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_trans_list); mToolbar = (Toolbar) findViewById(R.id.app_bar); setSupportActionBar(mToolbar); getSupportActionBar().setDisplayShowHomeEnabled(true); NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.left_drawer); drawerFragment.setUp((DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
Style.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar" > <item name="windowActionBar">false</item> </style> <style name="Theme.split" parent="AppBaseTheme"> </style> <style name="dialog_custom" parent="@style/Theme.AppCompat.Dialog"> <item name="@android:listPreferredItemPaddingLeft">20dip</item> <item name="@android:listPreferredItemPaddingRight">20dip</item> <item name="@android:listPreferredItemPaddingStart">20dip</item> <item name="@android:listPreferredItemPaddingEnd">20dip</item> </style> </resources>
LogCat
01-07 01:14:29.702 4811-4811/com.lee.yuchan.splt E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.lee.yuchan.splt, PID: 4811 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lee.yuchan.splt/com.lee.yuchan.splt.Trans_List}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) at android.app.ActivityThread.access$800(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:95) at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:88) at android.support.v7.internal.app.ToolbarActionBar.<init>(ToolbarActionBar.java:84) at android.support.v7.app.ActionBarActivityDelegateBase.setSupportActionBar(ActionBarActivityDelegateBase.java:175) at android.support.v7.app.ActionBarActivity.setSupportActionBar(ActionBarActivity.java:92) at com.lee.yuchan.splt.Trans_List.onCreate(Trans_List.java:72) at android.app.Activity.performCreate(Activity.java:5933) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) at android.app.ActivityThread.access$800(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Main Activity XML
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- The main content view --> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <include layout="@layout/app_bar"/> <TextView android:id="@+id/selectItems" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/select_instr1" android:textSize="20sp" android:gravity="center" android:paddingTop="4sp" android:paddingBottom="10sp" > </TextView> <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="0dip" android:layout_weight="1" > </ListView> <Button android:id="@+id/buttonChecked" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="0dip" android:background="@drawable/custom_button" android:textColor="#ffffff" android:text="@string/next_button" /> </LinearLayout> <!-- The navigation drawer --> <fragment android:id="@+id/left_drawer" android:layout_width="@dimen/nav_dr" android:layout_height="match_parent" android:layout_gravity="start" app:layout="@layout/fragment_navigation_drawer" android:name="com.lee.yuchan.splt.NavigationDrawerFragment" tools:layout="@layout/fragment_navigation_drawer" /> </android.support.v4.widget.DrawerLayout>
Set the id
attribute in the Toolbar
xml like this:
<?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/app_bar" app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/ThemeOverlay.AppCompat.Dark" > </android.support.v7.widget.Toolbar>
Then remove it from the include
block so it looks like this:
<include layout="@layout/app_bar"/>
And to simplify further you could just remove the include
block completely and simply insert the Toolbar
directly into the xml where the include
was.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With