0

我收到這個錯誤,我不知道爲什麼。這個相同的代碼以前工作;該問題在某個時間點更新後開始出現。Activity上的NullPointerException onCreate

堆棧跟蹤:

E/AndroidRuntime: FATAL EXCEPTION: main 
Process: com.domain.appname, PID: 17964 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.domain.appname/com.domain.appname.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.domain.appname.NavigationDrawerFragment.setUp(int, android.support.v4.widget.DrawerLayout)' on a null object reference 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2441) 
    at android.app.ActivityThread.access$800(ActivityThread.java:162) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5431) 
    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:914) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.domain.appname.NavigationDrawerFragment.setUp(int, android.support.v4.widget.DrawerLayout)' on a null object reference 
    at com.domain.appname.MainActivity.onCreate(MainActivity.java:40) 
    at android.app.Activity.performCreate(Activity.java:6056) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2441)  
    at android.app.ActivityThread.access$800(ActivityThread.java:162)  
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)  
    at android.os.Handler.dispatchMessage(Handler.java:102)  
    at android.os.Looper.loop(Looper.java:135)  
    at android.app.ActivityThread.main(ActivityThread.java:5431)  
    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:914)  
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707) 

下面是MainActivity.java違規代碼:

... 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    if (BuildConfig.DEBUG){ Log.d(TAG, "Created in debug mode"); } 

    NavigationDrawerFragment mNavigationDrawerFragment = (NavigationDrawerFragment) 
     getSupportFragmentManager().findFragmentById(R.id.navigation_drawer); 

    // Set up the drawer. 
    Log.d(TAG, "Setting up drawer"); 
    mNavigationDrawerFragment.setUp(// Line 40 
     R.id.navigation_drawer, 
     (DrawerLayout) findViewById(R.id.drawer_layout) 
    ); 

... 

我發現了兩個,似乎有類似的問題的問題。 (這裏的the firstthe second)第一個看起來不相關,第二個沒有答案。

我很困惑。 drawer_layoutnavigation_drawer不是扔「符號未解決」的錯誤。就像我說的,這一切都用於工作...

回答

3

你永遠不會打電話給Activity.setContentView(int)。這應該是這樣的:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.layout_containing_drawer); 

    // Rest. 
} 
+0

是的,看起來我添加了'在調試模式下創建',我放棄了重要的行。我希望這可以幫助別人(所以我可以感覺不那麼愚蠢)。 – Keith

相關問題