导航抽屉错误

时间:2017-06-16 10:08:16

标签: android

我尝试在Android工作室运行一个应用程序,但我不断收到这个奇怪的错误。

我几乎无法理解这个错误是什么以及如何解决这个问题,我们非常感谢您的帮助。

这里是logcat

06-16 15:26:02.141 5270-5270/com.sensiblewallet E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sensiblewallet, PID: 5270
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sensiblewallet/com.sensiblewallet.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:2452)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
  at android.app.ActivityThread.-wrap11(ActivityThread.java)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1376)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:148)
  at android.app.ActivityThread.main(ActivityThread.java:5481)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
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.sensiblewallet.ActivityMainWallet.onCreate(ActivityMainWallet.java:212)
  at android.app.Activity.performCreate(Activity.java:6251)
  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2405)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) 
  at android.app.ActivityThread.-wrap11(ActivityThread.java) 
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1376) 
  at android.os.Handler.dispatchMessage(Handler.java:102) 
  at android.os.Looper.loop(Looper.java:148) 
  at android.app.ActivityThread.main(ActivityThread.java:5481) 
  at java.lang.reflect.Method.invoke(Native Method) 
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 

这是它指示的代码段。

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, ""));
// MyUtils.setFontAllView((RelativeLayout)findViewById(R.id.laymenu));

1 个答案:

答案 0 :(得分:0)

试试这段代码:

    View v = mDrawerLay.getHeaderView(0);
    TextView navigationTotalCreditTextView = (TextView)v.findViewById(R.id.nav_drawer_total_credits);
    TextView navigationWalletIDText = (TextView)v.findViewById(R.id.nav_drawer_wallet_id);
navigationWalletIDText.setText("Wallet ID: " + PreferenceConnector.readString(aiContext,
        PreferenceConnector.WALLETID, ""));
    TextView navigationUsernameText = (TextView)v .findViewById(R.id.nav_drawer_username);
navigationUsernameText.setText(PreferenceConnector.readString(aiContext,
        PreferenceConnector.USERNAME, ""));