当我尝试运行以下代码时,应用程序崩溃了。但是如果我将此片段留空,它不会崩溃...
public class F_Songs extends Fragment {
ListView SngList;
ArrayList<SongDetails> songdetails=null;
String[] TRACK_COLUMNS;Cursor songCursor = null;
AdapterView.AdapterContextMenuInfo info;
ViewGroup view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{ view = (ViewGroup) inflater.inflate(R.layout.l_songs, null);
TRACK_COLUMNS = new String[] {
MediaStore.Audio.AudioColumns.ALBUM,
MediaStore.Audio.AudioColumns.ARTIST,
MediaStore.MediaColumns.DATA,
MediaStore.MediaColumns.DISPLAY_NAME,
MediaStore.Audio.AudioColumns.DURATION,
MediaStore.Audio.AudioColumns.TRACK,
MediaStore.Audio.AudioColumns.YEAR,
MediaStore.MediaColumns.SIZE,
MediaStore.MediaColumns.TITLE,
MediaStore.Audio.Media._ID
};
Cursor songCursor = getActivity().managedQuery(MediaStore.Audio.Media.
INTERNAL_CONTENT_URI, TRACK_COLUMNS, null, null, null);
if(songCursor != null){
while(songCursor.moveToNext()) {
SongDetails songs = new SongDetails();
songs.song= songCursor.getString(3).toString();
songs.Path=songCursor.getString(2).toString();
songs.Artist=songCursor.getString(1).toString();
songdetails.add(songs);
// publishProgress();
}
}
SngList = (ListView) view.findViewById(R.id.SongList);
registerForContextMenu(SngList);
SngList.setAdapter(new Adapter_ListView(songdetails));
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
// new ListPopulate().execute();
return view;
}}
这是logcat
10-01 22:09:24.890: E/AndroidRuntime(538): FATAL EXCEPTION: main
10-01 22:09:24.890: E/AndroidRuntime(538): java.lang.NullPointerException
10-01 22:09:24.890: E/AndroidRuntime(538): at source.justanothermusicplayer.F_Songs.onCreateView(F_Songs.java:55)
10-01 22:09:24.890: E/AndroidRuntime(538): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
10-01 22:09:24.890: E/AndroidRuntime(538): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
10-01 22:09:24.890: E/AndroidRuntime(538): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
10-01 22:09:24.890: E/AndroidRuntime(538): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
10-01 22:09:24.890: E/AndroidRuntime(538): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
10-01 22:09:24.890: E/AndroidRuntime(538): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
10-01 22:09:24.890: E/AndroidRuntime(538): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
10-01 22:09:24.890: E/AndroidRuntime(538): at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
10-01 22:09:24.890: E/AndroidRuntime(538): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
10-01 22:09:24.890: E/AndroidRuntime(538): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
10-01 22:09:24.890: E/AndroidRuntime(538): at android.view.View.measure(View.java:12603)
10-01 22:09:24.890: E/AndroidRuntime(538): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4677)
10-01 22:09:24.890: E/AndroidRuntime(538): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
10-01 22:09:24.890: E/AndroidRuntime(538): at android.view.View.measure(View.java:12603)
10-01 22:09:24.890: E/AndroidRuntime(538): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4677)
10-01 22:09:24.890: E/AndroidRuntime(538): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
10-01 22:09:24.890: E/AndroidRuntime(538): at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
10-01 22:09:24.890: E/AndroidRuntime(538): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
10-01 22:09:24.890: E/AndroidRuntime(538): at android.view.View.measure(View.java:12603)
10-01 22:09:24.890: E/AndroidRuntime(538): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4677)
10-01 22:09:24.890: E/AndroidRuntime(538): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
10-01 22:09:24.890: E/AndroidRuntime(538): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2072)
10-01 22:09:24.890: E/AndroidRuntime(538): at android.view.View.measure(View.java:12603)
10-01 22:09:24.890: E/AndroidRuntime(538): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1044)
10-01 22:09:24.890: E/AndroidRuntime(538): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2418)
10-01 22:09:24.890: E/AndroidRuntime(538): at android.os.Handler.dispatchMessage(Handler.java:99)
10-01 22:09:24.890: E/AndroidRuntime(538): at android.os.Looper.loop(Looper.java:137)
10-01 22:09:24.890: E/AndroidRuntime(538): at android.app.ActivityThread.main(ActivityThread.java:4340)
10-01 22:09:24.890: E/AndroidRuntime(538): at java.lang.reflect.Method.invokeNative(Native Method)
10-01 22:09:24.890: E/AndroidRuntime(538): at java.lang.reflect.Method.invoke(Method.java:511)
10-01 22:09:24.890: E/AndroidRuntime(538): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-01 22:09:24.890: E/AndroidRuntime(538): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-01 22:09:24.890: E/AndroidRuntime(538): at dalvik.system.NativeStart.main(Native Method)
错误在这行 songdetails.add(songs);