Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.lang.RuntimeException:无法恢复试图在游标关闭后访问它的活动_Java_Android_Android Activity_Onactivityresult - Fatal编程技术网

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

java.lang.RuntimeException:无法恢复试图在游标关闭后访问它的活动,java,android,android-activity,onactivityresult,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)

我使用以下方法将选定的图像路径传递给另一个活动:

@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) 
仅当从ActivityResult上的
打开第二个活动时,才会发生此错误

编辑:

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();
}
如何解决此错误?

首先,
managedQuery()
已被弃用超过六年。不要使用它。在某种形式的后台线程上使用
游标加载程序
,或使用
内容解析器
查询()

第二,一定要按照你使用的东西的说明去做。在本例中,您错过了:

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

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

首先,
managedQuery()
已经被弃用了六年多。不要使用它。在某种形式的后台线程上使用
游标加载程序
,或使用
内容解析器
查询()

第二,一定要按照你使用的东西的说明去做。在本例中,您错过了:

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


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

“实际上运行良好”——可能不是。删除
getPath()
,因为这种方法没有有效的实现。“当按下后退按钮时,我出现以下错误”--如果我不得不猜测,您使用的是
managedQuery()
方法,该方法已被弃用六年。您可能希望编辑问题并显示获取
光标的代码。我已添加了getpath方法。通过使用“感谢帮助”中的解决方案解决了此问题,“它实际上工作正常”-可能没有。删除
getPath()
,因为这种方法没有有效的实现。“当按下后退按钮时,我出现以下错误”--如果我不得不猜测,您使用的是
managedQuery()
方法,该方法已被弃用六年。您可能需要编辑问题并显示获取
光标的代码。我已添加了getpath方法。使用解决方案解决了问题,谢谢您的帮助