2015-04-01 52 views
1

我正在嘗試使用導航抽屜和四個片段創建應用程序。因此,當我從導航抽屜中選擇選項時,片段的視圖會顯示出來。現在,我爲視圖創建了一些按鈕,但每當我嘗試在代碼中初始化它們時,我都會收到下面粘貼的運行時錯誤。Android Studio - 無法選擇聲明

當我做了Command +單擊這些id R.id.bIndexOutOfBounds按鈕時,它向我展示了多個聲明。我無法得到如何選擇特定的聲明,以及爲什麼只有1個文件存在時,android studio會尋找4個不同的fragment_crash.xml文件(片段的佈局文件)。

如果有人知道如何獲得1個layout_crash.xml文件,請幫助我。

04-01 14:34:10.256 2067-2067/com.example.android.navigationdrawer E/AndroidRuntime﹕ FATAL EXCEPTION: main 
Process: com.example.android.navigationdrawer, PID: 2067 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.navigationdrawer/com.example.android.navigationdrawer.NavigationDrawerActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
     at android.app.ActivityThread.access$800(ActivityThread.java:144) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:135) 
     at android.app.ActivityThread.main(ActivityThread.java:5221) 
     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:899) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference 
     at com.example.android.navigationdrawer.CrashFragment.onCreateView(CrashFragment.java:56) 
     at android.app.Fragment.performCreateView(Fragment.java:2053) 
     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894) 
     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) 
     at android.app.BackStackRecord.run(BackStackRecord.java:833) 
     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452) 
     at android.app.Activity.performStart(Activity.java:5948) 
     at 

android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
            at android.app.ActivityThread.access$800(ActivityThread.java:144) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:135) 
            at android.app.ActivityThread.main(ActivityThread.java:5221) 
            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:899) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

enter image description here

回答

1

你應該使用bIndexOutOfBounds = (Button) getView().findViewById(R.id.bIndexOutOfBounds);

getView(),而不是「getView()」,「意見」將返回這是由onCreateView()返回,所以你不能得到視圖片段的根視圖通過getView()裏面的onCreateView()

+0

偉大的工作與我! – 2015-04-01 22:37:51