3

我試圖通過DialogFragment從圖庫中選擇照片。但是在初始化cursor時,我得到了nullpointerexception。任何想法爲什麼得到這個錯誤?Nullpointerexcepiton在光標上從對話框片段中選擇照片時

下面是我的代碼:

if (resultCode == Activity.RESULT_OK) { 
     Uri selectedImage = imageReturnedIntent.getData(); 
     String[] filePathColumn = {MediaStore.Images.Media.DATA}; 
     // Nullpointerexcepiton on this line 
     Cursor cursor = getActivity().getContentResolver().query(selectedImage, filePathColumn, null, null, null); 
     cursor.moveToFirst(); 
     int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 

     cursor.close(); 

    } 

這裏是我的logcat錯誤:

03-24 12:34:37.645: E/AndroidRuntime(21479): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65538, result=-1, data=Intent { dat=content://media/external/images/media/3890 flg=0x1 }} to activity {com.example/com.example.MainActivity}: java.lang.NullPointerException 

    03-24 12:34:37.645: E/AndroidRuntime(21479): at android.app.ActivityThread.deliverResults(ActivityThread.java:3462) 
    03-24 12:34:37.645: E/AndroidRuntime(21479): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3505) 
    03-24 12:34:37.645: E/AndroidRuntime(21479): at android.app.ActivityThread.access$1100(ActivityThread.java:150) 
    03-24 12:34:37.645: E/AndroidRuntime(21479): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1346) 
    03-24 12:34:37.645: E/AndroidRuntime(21479): at android.os.Handler.dispatchMessage(Handler.java:99) 
    03-24 12:34:37.645: E/AndroidRuntime(21479): at android.os.Looper.loop(Looper.java:213) 
    03-24 12:34:37.645: E/AndroidRuntime(21479): at android.app.ActivityThread.main(ActivityThread.java:5225) 
    03-24 12:34:37.645: E/AndroidRuntime(21479): at java.lang.reflect.Method.invokeNative(Native Method) 
    03-24 12:34:37.645: E/AndroidRuntime(21479): at java.lang.reflect.Method.invoke(Method.java:525) 
    03-24 12:34:37.645: E/AndroidRuntime(21479): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741) 
    03-24 12:34:37.645: E/AndroidRuntime(21479): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557) 
    03-24 12:34:37.645: E/AndroidRuntime(21479): at dalvik.system.NativeStart.main(Native Method) 

    03-24 12:34:37.645: E/AndroidRuntime(21479): Caused by: java.lang.NullPointerException 
    03-24 12:34:37.645: E/AndroidRuntime(21479): at com.example.MainDialogFragment.onActivityResult(MainDialogFragment.java:226) 
    03-24 12:34:37.645: E/AndroidRuntime(21479): at com.example.MainActivity.onActivityResult(DelictActivity.java:85) 
    03-24 12:34:37.645: E/AndroidRuntime(21479): at android.app.Activity.dispatchActivityResult(Activity.java:5322) 
    03-24 12:34:37.645: E/AndroidRuntime(21479): at android.app.ActivityThread.deliverResults(ActivityThread.java:3458) 
    03-24 12:34:37.645: E/AndroidRuntime(21479): ... 11 more 
+0

後全堆棧跟蹤,它與NPE – tbruyelle

+0

開始@tbruyelle請參閱我的編輯之一。 – MSr

+0

在您發佈的那個下面是不是有第二個stacktrace?這是很有趣的一個。 – tbruyelle

回答

5

如何開始您的活動並從哪裏開始? 如果你通過你的結果低谷的MainActivity,你可以嘗試做一個新的功能,在您的DialogFragment這樣的:

public void onMyActivityResult(Context main, int resultCode... an so on){ 

    main.getContentResolver....... 

} 

編輯: 我已經這樣做了:

獲取活動的onCreate:

@Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    mActivity = (MainActivity) this.getActivity(); 
    } 

則:

 @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == REQUEST_GALLERY && resultCode == Activity.RESULT_OK) { 
      ... 

      Uri selectedImage = data.getData(); 
      String path = getRealPathFromURI(selectedImage); 
      ... 
      reloadImages(); 

     } 
     super.onActivityResult(requestCode, resultCode, data); 
     } 

和:

private String getRealPathFromURI(Uri contentURI) { 
    Cursor cursor = mActivity.getContentResolver().query(contentURI, null, null, null, null); 
    if (cursor == null) { // Source is Dropbox or other similar local file 
        // path 
     return contentURI.getPath(); 
    } else { 
     cursor.moveToFirst(); 
     int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
     String path = cursor.getString(idx); 
     cursor.close(); 
     return path; 
    } 
    } 
2

的問題是在這條線

Cursor cursor = getActivity().getContentResolver().query(selectedImage, filePathColumn, null, null, null); 

它發生時,可能是因爲getActivity()返回空你的片段沒有附加到一個活動。我看到2個可能的原因:

  • 您在片段生命週期中過早地致電getActivity()。您需要等待調用onAttach(Activity)才能玩getActivity()
  • 您的片段已從活動中分離出來,請檢查返回的Fragment.isAdded()以確保您的片段仍處於附着狀態。
0

對於從廊 INT PICK_IMAGE = 1選擇圖像;

public void getGalleryImage() { 
     Intent intent = new Intent(); 
     intent.setType("image/*"); 
     intent.setAction(Intent.ACTION_GET_CONTENT); 
     activity.startActivityForResult(Intent.createChooser(intent,"Select Picture"), PICK_IMAGE); 
     } 

在活動結果

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (resultCode != Activity.RESULT_CANCELED) { 
      if (requestCode == PICK_IMAGE) { 
       String selectedImagePath = getAbsolutePath(data.getData()); 
       Log.d("GetImage","Selected Image Path "+selectedImagePath) 
      } else { 
       super.onActivityResult(requestCode, resultCode, data); 
      } 
     } 
} 

//獲取圖像路徑

public String getAbsoluteImagePath(Uri uri) { 
     String[] projection = { MediaColumns.DATA }; 
     @SuppressWarnings("deprecation") 
     Cursor cursor = activity.managedQuery(uri, projection, null, null, null); 
     if (cursor != null) { 
      int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA); 
      cursor.moveToFirst(); 
      return cursor.getString(column_index); 
     } else 
      return null; 
    }