java.lang.RuntimeException:无法恢复活动在游标关闭后尝试访问游标

时间:2017-05-19 11:44:12

标签: java android android-activity onactivityresult

我有以下方法将所选图像路径传递给另一个活动:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
            Log.d("fsd", "onActivityResult: ");

            Intent intent = new Intent(getApplicationContext(), AddContentImage.class);
            Bundle b = new Bundle();
            b.putString("path", selectedImagePath);
            intent.putExtras(b);
            startActivity(intent);
        }
    }
}

实际上工作正常,但是当按下后退按钮时出现跟随错误。

java.lang.RuntimeException: Unable to resume activity : android.database.StaleDataException: Attempted to access a cursor after it has been closed.
      at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3169)
      at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3247)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1440)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:148)
      at android.app.ActivityThread.main(ActivityThread.java:5582)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
   Caused by: android.database.StaleDataException: Attempted to access a cursor after it has been closed.
      at android.database.BulkCursorToCursorAdaptor.throwIfCursorIsClosed(BulkCursorToCursorAdaptor.java:63)
      at android.database.BulkCursorToCursorAdaptor.requery(BulkCursorToCursorAdaptor.java:132)
      at android.database.CursorWrapper.requery(CursorWrapper.java:228)
      at android.app.Activity.performRestart(Activity.java:6363)
      at android.app.Activity.performResume(Activity.java:6389)
      at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3158)
      at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3247) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1440) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:148) 
      at android.app.ActivityThread.main(ActivityThread.java:5582) 

如果从onActivityResult打开第二个活动,则只会出现此错误。

编辑:

getpath方法

public String getPath(Uri uri) {
    // just some safety built in
    if( uri == null ) {
        // TODO perform some logging or show user feedback
        return null;
    }
    // try to retrieve the image from the media store first
    // this will only work for images selected from gallery
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    if( cursor != null ){
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String path = cursor.getString(column_index);
        cursor.close();
        return path;
    }
    // this is our fallback here
    return uri.getPath();
}

如何解决此错误?

1 个答案:

答案 0 :(得分:1)

首先,managedQuery()已被弃用超过六年。不要使用它。使用CursorLoader,或在某种形式的后台主题中使用ContentResolverquery()

其次,请务必遵循您使用的内容的说明。在这种情况下,您错过了the instructions for using managedQuery()

  

警告:不要对使用此方法获得的游标调用close(),因为活动会在适当的时候为您执行此操作。

除此之外,Uri在大多数情况下都不指向文件,因此您无法获得该文件的“真实路径”。