-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();
}

原木猫:

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)

注意:我不知道游标出现空指针异常的原因是什么。我将此代码用于三星 Galaxy s3。

4

1 回答 1

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

于 2013-04-26T08:51:23.250 回答