简体   繁体   English

在空对象引用上使 android.support.v7.widget.Toolbar.setTitle(java.lang.CharSequence)'无效

[英]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: ActivityMainWallet.java 带有错误行:

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主要布局 XML: http : //pastebin.com/4UjqqZUk

Thanks a lot guys!非常感谢伙计们!

EDIT编辑

I've got now in my java file:我现在在我的java文件中:

  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:还有在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.同样在主 xml 布局中没有任何改变。 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.因为否则findViewById()不知道如何找到R.id.toolbarInner或要找到哪个文件。

The error is clear enough :错误很明显:

Void android.support.v7.widget.Toolbar.setTitle(java.lang.CharSequence)' on a null object reference在空对象引用上使android.support.v7.widget.Toolbar.setTitle(java.lang.CharSequence)'无效

The id drawer_toolbar isn't in your provided file : - toolbar.xml: pastebin.com/JrXBFbwy id drawer_toolbar不在您提供的文件中: drawer_toolbarpastebin.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 . java 文件ActivityMainWallet.java的工具栏 id 和 xml 文件toolbar.xml的工具栏 id需要相同

toolbar.xml工具栏.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现在你必须在你的 XML 文件中包含一个布局如下

activity_user_data.xml活动用户数据.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现在,最后,在您的 java 文件中,您只需执行一些简单的步骤

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

I didn't find drawer_toolbar in your layout file.我没有在您的布局文件中找到 drawer_toolbar。 Try to inlude it and after you can set title on it.尝试引入它,然后您可以在其上设置标题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在空对象引用上使 android.widget.TextView.setText(java.lang.CharSequence)&#39; 无效 - Void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference 在Home.onCreate上的空对象引用上无效android.widget.TextView.setText(java.lang.CharSequence)&#39; - void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at Home.onCreate NullPointerException: 'void android.widget.Button.setText(java.lang.CharSequence)' 在 null object 参考 - NullPointerException: 'void android.widget.Button.setText(java.lang.CharSequence)' on a null object reference Recycler view android studioAttempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference - Recycler view android studioAttempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference 尝试在空对象引用上调用虚拟方法&#39;void android.widget.EditText.setText(java.lang.CharSequence)&#39; - Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference Recycler View - 尝试在空对象引用上调用虚拟方法“void android.widget.TextView.setText(java.lang.CharSequence)” - Recycler View - Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference Java错误:尝试在空对象引用上调用虚拟方法&#39;void android.widget.TextView.setText(java.lang.CharSequence)&#39; - Java Error: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference TensorFlow Lite Android 应用程序在空对象引用上因 NullPointerException &#39;void android.widget.TextView.setText(java.lang.CharSequence)&#39; 崩溃 - TensorFlow Lite Android App crashes with NullPointerException 'void android.widget.TextView.setText(java.lang.CharSequence)' on null object reference 尝试在空对象引用上调用虚拟方法&#39;void android.widget.TextView.append(java.lang.CharSequence)&#39; - Attempt to invoke virtual method 'void android.widget.TextView.append(java.lang.CharSequence)' on a null object reference 如何解决对空对象引用调用虚拟方法&#39;void android.widget.TextView.setText(java.lang.CharSequence)&#39;的尝试 - How to resolve Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM