Void android.support.v7.widget.Toolbar.setTitle(java.lang.CharSequence)'在null对象引用上

时间:2016-10-26 21:09:35

标签: java android android-studio

一开始:我在这里尝试了所有解决方案。

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:

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) {

主要布局XML: http://pastebin.com/4UjqqZUk

非常感谢你们!

修改

我现在已经进入了我的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();
        }
    });

还有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>

同样在主xml布局中没有任何改变。 同样的错误。帮助我!

3 个答案:

答案 0 :(得分:1)

错误很明显:

  

空   android.support.v7.widget.Toolbar.setTitle(java.lang.CharSequence)' on   空对象引用

标识drawer_toolbar不在您提供的文件中:   - toolbar.xml:pastebin.com/JrXBFbwy

java文件ActivityMainWallet.java中的工具栏ID和xml文件toolbar.xml 中的工具栏ID必须相同

答案 1 :(得分:1)

findViewById()

因为否则R.id.toolbarInner不知道如何找到import datetime def first_day_of_month(any_day): first_month = any_day.replace(day=1) return first_month def last_day_of_month(any_day): next_month = any_day.replace(day=28) + datetime.timedelta(days=4) return next_month - datetime.timedelta(days=next_month.day) for month in range(1, 13): print first_day_of_month(datetime.date(2017, month, 1)) print last_day_of_month(datetime.date(2017, month, 1)) 或要查找的文件。

答案 2 :(得分:-1)

我在布局文件中找不到drawer_toolbar。尝试加入它并在你可以设置标题之后。