访问NavigationView中的TextView时出现NullPointerException

时间:2016-10-23 23:57:39

标签: java android android-studio nullpointerexception textview

我正在调试我的应用程序,我收到此错误:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.fashionwallet, PID: 15785
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fashionwallet/com.fashionwallet.ActivityMainWallet}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(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)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                  at com.fashionwallet.ActivityMainWallet.onCreate(ActivityMainWallet.java:212)
                  at android.app.Activity.performCreate(Activity.java:6092)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2468)
                  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) 

以下是文件: 的 XML:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout android:id="@+id/content_frame_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ffffff"
            android:layout_below="@+id/toolbar_app_bar_layout">

            <FrameLayout
                android:id="@+id/content_frame"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            </FrameLayout>

        </RelativeLayout>

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/toolbar_app_bar_layout" />

        <android.support.design.widget.AppBarLayout
            android:id="@+id/toolbar_app_bar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <include android:id="@+id/toolbar"
                layout="@layout/toolbar" />

            <android.support.design.widget.TabLayout
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/sliding_tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/toolbar"
                app:tabMode="scrollable"
                app:paddingStart="16dp"
                app:tabPaddingStart="16dp"
                app:tabPaddingEnd="16dp"
                app:tabMinWidth="96dp"
                app:tabGravity="center" />

        </android.support.design.widget.AppBarLayout>

    </RelativeLayout>

    <android.support.design.widget.NavigationView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/navigation_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="start"
        android:background="@color/md_white_1000"
        app:headerLayout="@layout/nav_drawer_header"
        app:menu="@menu/menu_drawer"
        app:itemTextColor="@color/md_grey_600"
        app:itemTextAppearance="@style/TextAppearance.AppCompat.Menu"
        android:clipToPadding="false">

        <!--<FrameLayout-->
            <!--android:layout_width="match_parent"-->
            <!--android:layout_height="wrap_content"-->
            <!--android:layout_gravity="bottom"-->
            <!--android:background="@color/md_white_1000"-->
            <!--android:elevation="4dp"-->
            <!--android:layout_marginBottom="-96dp">-->

            <!--<Button android:id="@+id/navigation_button_footer"-->
                <!--android:layout_width="match_parent"-->
                <!--android:layout_height="match_parent"-->
                <!--android:text=""-->
                <!--android:textSize="13sp"-->
                <!--android:textColor="@color/md_grey_800"-->
                <!--android:lines="3"-->
                <!--android:gravity="center"-->
                <!--style="@style/Widget.AppCompat.ActionButton"-->
                <!--android:paddingTop="20dp"-->
                <!--android:paddingLeft="20dp"-->
                <!--android:paddingRight="20dp"-->
                <!--android:paddingBottom="20dp"/>-->

        <!--</FrameLayout>-->

    </android.support.design.widget.NavigationView>

    <RelativeLayout
        android:id="@+id/lay_connection"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="@dimen/actmain_margintop"
        android:background="#3c3c3c"
        android:visibility="gone" >

        <TextView
            android:id="@+id/text_error"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:gravity="center"
            android:text="@string/error_no_internet"
            android:textColor="@color/md_white_1000"
            android:textSize="@dimen/twentyfive" />

        <RelativeLayout
            android:id="@+id/lay_dialog"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone" >

            <ProgressBar
                android:id="@+id/progressBar1"
                style="?android:attr/progressBarStyleLarge"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/progressBar1"
                android:layout_centerHorizontal="true"
                android:text="@string/error_no_internet"
                android:textColor="@color/md_white_1000"
                android:textSize="@dimen/twentyfive" />
        </RelativeLayout>
    </RelativeLayout>

</android.support.v4.widget.DrawerLayout>

ActivityMainWallet.java(212行的一部分):

    mDrawerLayout   = (DrawerLayout)findViewById(R.id.drawer_layout);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.END);
mDrawerLay      = (NavigationView)findViewById(R.id.navigation_view);
mDrawerLay.setNavigationItemSelectedListener(this);
TextView navigationTotalCreditTextView = (TextView)mDrawerLay.findViewById(R.id.nav_drawer_total_credits);
navigationTotalCreditTextView.setText("Total Credits: " + PreferenceConnector.readInteger(aiContext,
        PreferenceConnector.WALLETPOINTS, 0));
TextView navigationWalletIDText = (TextView)mDrawerLay.findViewById(R.id.nav_drawer_wallet_id);
navigationWalletIDText.setText("Wallet ID: " + PreferenceConnector.readString(aiContext,
        PreferenceConnector.WALLETID, ""));
TextView navigationUsernameText = (TextView)mDrawerLay.findViewById(R.id.nav_drawer_username);
navigationUsernameText.setText(PreferenceConnector.readString(aiContext,
        PreferenceConnector.USERNAME, ""));

第212行:

navigationTotalCreditTextView.setText("Total Credits: " + PreferenceConnector.readInteger(aiContext,
    PreferenceConnector.WALLETPOINTS, 0));

请帮帮我们!怎么了?怎么解决?这是我的应用程序中的最后一个错误。当我想通过Google Plus登录时,我在调试器中收到此错误并被迫关闭应用程序。 谢谢!

1 个答案:

答案 0 :(得分:1)

xml文件中不存在带有id nav_drawer_wallet_id的TextView。检查java中的id和xml中的id是否相同。

相关问题