尝试调用addDrawerListener时出现奇怪的nullpointerexception

时间:2016-05-10 23:42:43

标签: android

根据LogCat,我在尝试使用addDrawerListener方法时收到Null PE,但奇怪的是,所涉及的任何内容都不会显示为null。

drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

toggle = new ActionBarDrawerToggle((Activity)this, drawer, toolbar,R.string.navigation_drawer_open, R.string.navigation_drawer_close );

if (toggle == null){
    Log.d("toggle","");
}
if (drawer == null){
    Log.d("drawer", "");
}
Log.d("Wut", "what's null then?");

drawer.addDrawerListener(toggle);

Exact LogCat:

FATAL EXCEPTION: main
   Process: com.example.jaderedworth.androidmobileapplication, PID: 12293
   java.lang.RuntimeException: Unable to start activity
     ComponentInfo{com.example.jaderedworth.androidmobileapplication/com.example.jaderedworth.androidmobileapplication.baseUITest}: 
     java.lang.NullPointerException: Attempt to invoke virtual method
     'void android.support.v4.widget.DrawerLayout.addDrawerListener(android.support.v4.widget.DrawerLayout$DrawerListener)' 
     on a null object reference
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
       at android.app.ActivityThread.-wrap11(ActivityThread.java)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:148)
       at android.app.ActivityThread.main(ActivityThread.java:5417)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
    Caused by: java.lang.NullPointerException: 
      Attempt to invoke virtual method 'void android.support.v4.widget.DrawerLayout.addDrawerListener(android.support.v4.widget.DrawerLayout$DrawerListener)'
      on a null object reference
       at com.example.jaderedworth.androidmobileapplication.BaseActivity.BaseUIActivity.onCreate(BaseUIActivity.java:59)
       at com.example.jaderedworth.androidmobileapplication.baseUITest.onCreate(baseUITest.java:15)
       at android.app.Activity.performCreate(Activity.java:6237)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
       at android.app.ActivityThread.-wrap11(ActivityThread.java) 
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
       at android.os.Handler.dispatchMessage(Handler.java:102) 
       at android.os.Looper.loop(Looper.java:148) 
       at android.app.ActivityThread.main(ActivityThread.java:5417) 
       at java.lang.reflect.Method.invoke(Native Method) 
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

0 个答案:

没有答案