java.lang.NullPointerException是由Fragment中的getContentResolver()引起的

时间:2014-11-25 13:04:57

标签: java android android-fragments nullpointerexception

我在下面有这个代码。它在Activity之前有效。现在我想将它转换为Fragment。我尝试了 context.getContentResolver(); getActivity()。getContentResolver(); 但它不起作用。如何解决这个问题?

public class MyFragmentA extends Fragment {

//song list variables
    private ArrayList<Song> songList;
    private ListView songView;
    public Context context;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View myFragmentView = inflater.inflate(R.layout.fragment_a, container, false);

    //retrieve list view
            songView = (ListView) myFragmentView.findViewById(R.id.song_list);
            //instantiate list
            songList = new ArrayList<Song>();
            //get songs from device
            getSongList();
            //sort alphabetically by title
            Collections.sort(songList, new Comparator<Song>(){
                public int compare(Song a, Song b){
                    return a.getTitle().compareTo(b.getTitle());
                }
            });
            //create and set adapter
            SongAdapter songAdt = new SongAdapter(this.getActivity(), songList);
            songView.setAdapter(songAdt);

    return myFragmentView;
}


public void getSongList() {

ContentResolver musicResolver = context.getContentResolver();
Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
----------------------
----------------------
----------------------
----------------------
            }
        }
    }

这是我的logcat跟踪

11-25 20:29:34.001: E/AndroidRuntime(4092): FATAL EXCEPTION: main
11-25 20:29:34.001: E/AndroidRuntime(4092): Process: com.ddev.delta, PID: 4092
11-25 20:29:34.001: E/AndroidRuntime(4092): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ddev.delta/com.ddev.delta.AndroidNavigationTabsActivity}: java.lang.NullPointerException
11-25 20:29:34.001: E/AndroidRuntime(4092):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212)
11-25 20:29:34.001: E/AndroidRuntime(4092):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
11-25 20:29:34.001: E/AndroidRuntime(4092):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
11-25 20:29:34.001: E/AndroidRuntime(4092):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
11-25 20:29:34.001: E/AndroidRuntime(4092):     at android.os.Handler.dispatchMessage(Handler.java:102)
11-25 20:29:34.001: E/AndroidRuntime(4092):     at android.os.Looper.loop(Looper.java:136)
11-25 20:29:34.001: E/AndroidRuntime(4092):     at android.app.ActivityThread.main(ActivityThread.java:5146)
11-25 20:29:34.001: E/AndroidRuntime(4092):     at java.lang.reflect.Method.invokeNative(Native Method)
11-25 20:29:34.001: E/AndroidRuntime(4092):     at java.lang.reflect.Method.invoke(Method.java:515)
11-25 20:29:34.001: E/AndroidRuntime(4092):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
11-25 20:29:34.001: E/AndroidRuntime(4092):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
11-25 20:29:34.001: E/AndroidRuntime(4092):     at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
11-25 20:29:34.001: E/AndroidRuntime(4092):     at dalvik.system.NativeStart.main(Native Method)
11-25 20:29:34.001: E/AndroidRuntime(4092): Caused by: java.lang.NullPointerException
11-25 20:29:34.001: E/AndroidRuntime(4092):     at com.ddev.delta.AndroidNavigationTabsActivity.onCreate(AndroidNavigationTabsActivity.java:17)
11-25 20:29:34.001: E/AndroidRuntime(4092):     at android.app.Activity.performCreate(Activity.java:5231)
11-25 20:29:34.001: E/AndroidRuntime(4092):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-25 20:29:34.001: E/AndroidRuntime(4092):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
11-25 20:29:34.001: E/AndroidRuntime(4092):     ... 12 more

5 个答案:

答案 0 :(得分:6)

ContentResolver musicResolver = context.getContentResolver();

您的context未初始化。

由于您处于片段生命周期回调onCreateView()或从一个调用的方法,您可以getActivity()使用Context

ContentResolver musicResolver = getActivity().getContentResolver();

删除Context context;声明也是不必要的。

答案 1 :(得分:0)

public Context context;

您的上下文 null ,请尝试使用getActivity().getContentResolver();

答案 2 :(得分:0)

在调用getSongList()之前在onCreateView()中初始化上下文对象:

context = getActivity();

getActivity().getContentResolver()

答案 3 :(得分:0)

创建一个Activity对象,然后按

初始化它
Activity a=getActivity();

然后使用

ContentResolver musicResolver = a.getContentResolver();

答案 4 :(得分:0)

对这些案例使用以下内容:

  • 应用程序: - getApplicationContext()
  • 活动: - 上下文
  • 片段: - getActivity()