Null pointer exception when loading fragment

限于喜欢 提交于 2020-01-02 23:18:11

问题


I am working on my final project in android-programming course (so I'm still pretty new). I'm building my own media player. I implemented a Drawer that contains the different media types (video, audio, stream etc.) and what I want is to have each clicked item load a fragment that will tackle the task. The first one I'm working on is the audio fragment. It is th esecond on the list but i think it'll e easier than the video. Anyway. when I click on "audio" (picture link) I get a null pointer exception. I've been trying to figure it out for a couple of days but now I'm really wasting precious time. What am I doing wrong?

Because there is quite a lot of code involved I'll use snippets (if people prefer I actually paste all the code here, let me know and I'll change it).

MainActivity: http://pastebin.com/zsXuZHkN

FragmentAudio: http://pastebin.com/77XsBbu9

ListAdapterAudio: http://pastebin.com/cfu7azP0

logact log:

09-13 18:31:33.056  11959-11959/com.example.ref_apps.winnerplayer E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.ref_apps.winnerplayer, PID: 11959
java.lang.NullPointerException
        at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:414)
        at android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:449)
        at android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:441)
        at com.example.ref_apps.winnerplayer.MainActivity.selectDrawerItem(MainActivity.java:118)
        at com.example.ref_apps.winnerplayer.MainActivity$1.onItemClick(MainActivity.java:64)
        at android.widget.AdapterView.performItemClick(AdapterView.java:299)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1153)
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:3065)
        at android.widget.AbsListView$3.run(AbsListView.java:4031)
        at android.os.Handler.handleCallback(Handler.java:808)
        at android.os.Handler.dispatchMessage(Handler.java:103)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:5292)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
        at dalvik.system.NativeStart.main(Native Method)

there are other files, obviously, but I don't think that they are the culprits. I'll add file by request.

Thanks!!


回答1:


In your Activity you are importing android.support.v4.app.Fragment and your Fragment is android.app.Fragment. That's why your cast fails after calling newInstance() and your fragment is null. The same goes with your ListFragment.

You also need to implement newInstance() in your fragment.

public static Fragment newInstance() 
{
    FragmentAudio myFragment = new FragmentAudio();
    return myFragment;
}



回答2:


Try after adding addToBackstack(null) in fragment replace.

(or)

Check backStack count and you should clear all the count before replace another fragment.



来源:https://stackoverflow.com/questions/32551319/null-pointer-exception-when-loading-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!