我下面有下面的代碼。它在Activity中工作。現在我想將它轉換爲Fragment。我試過context.getContentResolver();或getActivity()。getContentResolver();但它不起作用。如何解決這個問題?java.lang.NullPointerException是由片段中的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
哪個代碼中的第57行? '57號線發生異常。 – 2014-11-25 13:07:59
上下文爲空...使用getActivity()。getContentResolver(); – 2014-11-25 13:08:39
'AndroidNavigationTabsActivity.onCreate(AndroidNavigationTabsActivity.java:17)'中有另一個NPE' – laalto 2014-11-25 13:37:17