简体   繁体   中英

Void android.support.v7.widget.Toolbar.setTitle(java.lang.CharSequence)' on a null object reference

On the beginning: i tried all solutions here.

FATAL EXCEPTION: main
                                                 Process: com.justfashion, PID: 5068
                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.justfashion/com.justfashion.ActivityMainWallet}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.Toolbar.setTitle(java.lang.CharSequence)' on a null object reference
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2521)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2595)
                                                     at android.app.ActivityThread.access$800(ActivityThread.java:178)
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
                                                     at android.os.Handler.dispatchMessage(Handler.java:111)
                                                     at android.os.Looper.loop(Looper.java:194)
                                                     at android.app.ActivityThread.main(ActivityThread.java:5631)
                                                     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:959)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)

ActivityMainWallet.java with error lines:

drawerToolbar = (Toolbar)findViewById(R.id.drawer_toolbar);
    drawerToolbar.setTitle(getString(R.string.app_name));
    drawerToolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
    drawerToolbar.setNavigationOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

Main layout XML: http://pastebin.com/4UjqqZUk

Thanks a lot guys!

EDIT

I've got now in my java file:

  drawerToolbar = (Toolbar)findViewById(R.id.toolbarInner);
    drawerToolbar.setTitle(getString(R.string.app_name));
    drawerToolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
    drawerToolbar.setNavigationOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            openAndCloseDrawer();
        }
    });

And also in toolbar.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/toolbar" >

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbarInner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            app:titleTextAppearance="@style/AppTheme.Widget.Toolbar.Title"
            android:theme="@style/AppTheme.Widget.Toolbar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            android:elevation="8dp"
            android:paddingBottom="0dp"
            android:layout_margin="0dp"/>


</RelativeLayout>

Also in main xml layout nothing changed. The same error. Help me!

drawerToolbar = (Toolbar)findViewById(R.id.toolbarInner);

should under the setContentView(R.layout.your_loayout);

because otherwise findViewById() don't know how to find R.id.toolbarInner or which file to find.

The error is clear enough :

Void android.support.v7.widget.Toolbar.setTitle(java.lang.CharSequence)' on a null object reference

The id drawer_toolbar isn't in your provided file : - toolbar.xml: pastebin.com/JrXBFbwy

The toolbar id in your java file ActivityMainWallet.java and the toolbar id in your xml file toolbar.xml need to be the same .

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#344955"
        >
</androidx.appcompat.widget.Toolbar>

Now you have to include a layout in your XML file as follow

activity_user_data.xml

<RelativeLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#356859"
    tools:context=".UserDataActivity">

    <include
        layout="@layout/toolbar"/>

</RelativeLayout>

Now, finally, in your java file, you have to simply follow some simple steps

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("User's Data");
        setSupportActionBar(toolbar);

I didn't find drawer_toolbar in your layout file. Try to inlude it and after you can set title on it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM