2013-04-26 104 views
-4
try{ 

    String[] filePathColumn = { MediaStore.Images.Media.DATA }; 
     Cursor cursor = getContentResolver().query(imgPath, 
       filePathColumn, null, null, null); 
     if(cursor.getCount()>0){ 
     cursor.moveToFirst(); 
     int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
     path = cursor.getString(columnIndex); } 
     else{ 
      Toast.makeText(getApplicationContext(), "Cursor Null", Toast.LENGTH_SHORT).show(); 
     } 
} 
catch(Exception e){ 
    e.printStackTrace(); 
} 

登錄貓:光標空例外三星galaxy S3

04-26 14:07:12.920: W/System.err(18580): java.lang.NullPointerException 
04-26 14:07:12.930: W/System.err(18580): at com.example.camera.CameraAct$10$1.run(CameraAct.java:365) 
04-26 14:07:12.930: W/System.err(18580): at android.os.Handler.handleCallback(Handler.java:587) 
04-26 14:07:12.930: W/System.err(18580): at android.os.Handler.dispatchMessage(Handler.java:92) 
04-26 14:07:12.930: W/System.err(18580): at android.os.Looper.loop(Looper.java:132) 
04-26 14:07:12.930: W/System.err(18580): at android.app.ActivityThread.main(ActivityThread.java:4126) 
04-26 14:07:12.930: W/System.err(18580): at java.lang.reflect.Method.invokeNative(Native Method) 
04-26 14:07:12.930: W/System.err(18580): at java.lang.reflect.Method.invoke(Method.java:491) 

注:我不知道什麼是從遊標空pointerexception的原因。我用這個代碼爲三星Galaxy S3。

+0

從logcat發佈您的堆棧跟蹤 – 2013-04-26 08:48:54

+0

您是否定義了imgPath? 我建議你在使用它之前檢查遊標!= null。此外,它可能filePathColumn不存在每個設備(可能會受到保護,或可能指向一個不存在的可移動內存) 請粘貼更大的一段代碼來幫助你。 – 2013-04-26 08:50:35

+3

你的麪包是不正確的。它在光標爲空時記錄,而不是爲空。 – njzk2 2013-04-26 08:53:25

回答

1
Cursor cursor = getContentResolver().query(imgPath, 
      filePathColumn, null, null, null); 
    if(cursor.getCount()>0){ 
    cursor.moveToFirst(); 
    int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
    path = cursor.getString(columnIndex); } 
    else{ 
     Toast.makeText(getApplicationContext(), "Cursor Null", Toast.LENGTH_SHORT).show(); 
    } 

很明顯getContentResolver().query返回空遊標。您需要確保query(imgPath,filePathColumn, null, null, null)是正確的,特別是imgPath