简体   繁体   中英

Null pointer exception when loading fragment

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

Try after adding addToBackstack(null) in fragment replace.

(or)

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

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

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