简体   繁体   English

Android Studio-无法选择声明

[英]Android Studio - Unable to choose declaration

I am trying to create an app with a navigation drawer and four fragments. 我正在尝试创建一个带有导航抽屉和四个片段的应用程序。 So as I select the option from navigation drawer the view of the fragment gets displayed. 因此,当我从导航抽屉中选择选项时,将显示片段的视图。 Now, I have certain buttons created for the view, but whenever i am trying to initialize them in the code, i am getting runtime error which is pasted below. 现在,我为视图创建了某些按钮,但是每当我尝试在代码中初始化它们时,我都会收到以下粘贴的运行时错误。

When I did Command + Click on those ids R.id.bIndexOutOfBounds button, it showed me multiple declaration. 当我执行Command +单击这些ID R.id.bIndexOutOfBounds按钮时,它向我显示了多个声明。 I am unable to get how to choose a specific declaration, and why is android studio looking for 4 different fragment_crash.xml file ( layout file for the fragment) when only 1 file is present. 我无法获取如何选择特定声明的信息,为什么只有一个文件时android studio会查找4个不同的fragment_crash.xml文件(片段的布局文件)。

Please help me if anyone knows how to get only 1 layout_crash.xml file. 如果有人知道如何仅获取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)

在此处输入图片说明

You should use "view" instead of "getView()" in bIndexOutOfBounds = (Button) getView().findViewById(R.id.bIndexOutOfBounds); 您应该在bIndexOutOfBounds = (Button) getView().findViewById(R.id.bIndexOutOfBounds);使用“视图”而不是“ getView()”。 bIndexOutOfBounds = (Button) getView().findViewById(R.id.bIndexOutOfBounds);

getView() will return the root view of a fragment which is returned by onCreateView() so you cannot get view by getView() inside of onCreateView() getView()将返回这是由返回的片段的根视图onCreateView()所以你不能得到视图getView()onCreateView()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM