简体   繁体   中英

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. 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.

Please help me if anyone knows how to get only 1 layout_crash.xml file.

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);

getView() will return the root view of a fragment which is returned by onCreateView() so you cannot get view by getView() inside of onCreateView()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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