2013-04-26 176 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