Android Studio - 无法选择声明

时间:2015-04-01 21:53:14

标签: android android-fragments android-studio

我正在尝试使用导航抽屉和四个片段创建一个应用。因此,当我从导航抽屉中选择选项时,将显示片段的视图。现在,我为视图创建了某些按钮,但每当我尝试在代码中初始化它们时,我都会收到粘贴在下面的运行时错误。

当我执行Command +点击那些ids R.id.bIndexOutOfBounds按钮时,它向我显示了多个声明。我无法获得如何选择特定声明,以及为什么android studio在只有1个文件时会查找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 个答案:

答案 0 :(得分:1)

您应该在bIndexOutOfBounds = (Button) getView().findViewById(R.id.bIndexOutOfBounds);

中使用“view”而不是“getView()”

getView()将返回onCreateView()返回的片段的根视图,因此您无法通过getView()内的onCreateView()获取视图