我将游标的数据插入到arrayList中,由于该错误,应用程序崩溃。当我使用cursor.getCount()检查光标的大小时,向我显示188。但是当我是cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)时,出现此错误。空对象如何给我计数,然后向我显示空对象引用。
public ArrayList<songInfo> getSongArray(){
ArrayList<songInfo> songsList = new ArrayList<>();
songInfo sInfo;
Cursor c = getSongCursor();
/*if(c==null) {
Log.v("empty_cursor","yes");
return songsList;
}else{
Log.v("cursor'sLength",""+c.getCount()); \\ giving me 188.
return songsList;
}*/
int songPathIndex = c.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA); \\ giving me ERROR here.
/*int songTitleIndex = c.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
//Toast.makeText(, "", Toast.LENGTH_SHORT).show();
sInfo = new songInfo("Hello Mayank","no path to this");
songsList.add(sInfo);
songsList.add(sInfo);
Log.v("Cursor_length",""+c.getCount());
for(int i=0;i<c.getCount();i++){
sInfo = new songInfo(c.getString(songTitleIndex),c.getString(songPathIndex));
//songsList.add(sInfo);
}*/
return songsList;
}
public Cursor getSongCursor(){
try{
Uri externalUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String[] projection = new String[]{
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.DATA
//MediaStore.Audio.Media._ID,
};
String selection = null;
String[] selectionArgs = null;
String sortOrder = MediaStore.Audio.Media.TITLE+" ASC";
Cursor songCursor = getContext().getContentResolver().query(externalUri,projection,selection,selectionArgs,sortOrder);
Log.v("cursor_length","" + songCursor.getCount());
Toast.makeText(getActivity(), "Okay till now", Toast.LENGTH_SHORT).show();
return songCursor;
}catch (Exception e){
return null;
}
}
错误(空对象引用):
java.lang.NullPointerException: Attempt to invoke interface method 'int android.database.Cursor.getColumnIndexOrThrow(java.lang.String)' on a null object reference
完全错误:
2019-06-22 11:44:05.909 14962-14962/com.example.mymusicplayer E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mymusicplayer, PID: 14962
java.lang.NullPointerException: Attempt to invoke interface method 'int android.database.Cursor.getColumnIndexOrThrow(java.lang.String)' on a null object reference
at com.example.mymusicplayer.RecyclerViewFragment.getSongArray(RecyclerViewFragment.java:78)
at com.example.mymusicplayer.RecyclerViewFragment.onViewCreated(RecyclerViewFragment.java:60)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1471)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2243)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:654)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:146)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1244)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1092)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1622)
at android.view.View.measure(View.java:22105)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6606)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
at android.view.View.measure(View.java:22105)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6606)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
at android.view.View.measure(View.java:22105)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6606)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:401)
at android.view.View.measure(View.java:22105)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6606)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:22105)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6606)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
at android.view.View.measure(View.java:22105)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6606)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:727)
at android.view.View.measure(View.java:22105)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2467)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1549)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1806)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1437)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6886)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1029)
at android.view.Choreographer.doCallbacks(Choreographer.java:841)
at android.view.Choreographer.doFrame(Choreographer.java:772)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1015)
at android.os.Handler.handleCallback(Handler.java:794)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
2019-06-22 11:44:05.909 14962-14962/com.example.mymusicplayer E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6651)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)