加载片段时的空指针异常 [英] Null pointer exception when loading fragment

查看:265
本文介绍了加载片段时的空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的最后一个项目在Android系统的程序设计课程的工作(所以我还是pretty新)。我建立我自己的媒体播放器。我实现了包含不同媒体类型(视频,音频流等),我要的是让每个点击加载项,将处理任务片段的抽屉。第一个我工作的是音频片段。它是名单上的第esecond,但我认为它会比Ë视频更容易。无论如何。当我点击声音(图片链接)我得到一个空指针异常。我一直在试图弄明白了几天,但现在我真的很浪费precious时间。我在做什么错了?

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?

由于有相当多的code的参与,我会用片段(如果人们preFER我其实粘贴在这里所有的code,让我知道,我会改变它)。

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

MainActivity: http://pastebin.com/zsXuZHkN

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

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

ListAdapterAudio: http://pastebin.com/cfu7azP0

ListAdapterAudio: http://pastebin.com/cfu7azP0

logact日志:

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.

谢谢!

推荐答案

在您的活动要导入 android.support.v4.app.Fragment 和你的片段 android.app.Fragment 。这就是为什么在调用后,您的转换失败的newInstance()和你的片段为空。同样的问题也与你的 ListFragment

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.

您还需要实现的newInstance()在你的片段。

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

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

这篇关于加载片段时的空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆