gpt4 book ai didi

android - 从 onActivityResult 调用的 onPostExecute 更新 fragment 的 UI

转载 作者:行者123 更新时间:2023-11-29 02:31:58 28 4
gpt4 key购买 nike

在我的应用程序中,我通过点击按钮选择文档来启动带有 ACTION_OPEN_DOCUMENT 的 Intent 。

private static final int READ_REQUEST_CODE = 42;
public void performFileSearch() {

// ACTION_OPEN_DOCUMENT is the intent to choose a file via the system's file
// browser.
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
String[] mimeTypes = {
"image/*",
"text/plain",
"application/pdf"
};
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
startActivityForResult(intent, READ_REQUEST_CODE);
}

之后,我在 onActivityResult 中获取用户选择的文档的 Uri 并将其传递给一个 AsyncTask文档已压缩。

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
if (data != null) {
dialog = new ProgressDialog(getActivity());
dialog.setMessage(getString(R.string.upload_dialog));
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();

// The document selected by the user won't be returned in the intent.
// Instead, a URI to that document will be contained in the return intent
// provided to this method as a parameter.
// Pull that URI using resultData.getData().
new ZipFile().execute(data.getData());
}
}
}

下面是我的 AsyncTask 类:

private class ZipFile extends AsyncTask<Uri, Void, String> {

@Override
protected String doInBackground(Uri... uris) {
Uri uri = uris[0];
String fileName = null;

try {
fileName = zm.createPath("user");
zm.zipFromUri(uri, fileName, commonMethods.getFileName(uri));
} catch (NullPointerException | IOException e) {
e.printStackTrace();
}
return fileName;
}

@Override
protected void onPostExecute(String fileName) {
dialog.dismiss();
if (fileName != null) {
renderView(view);
}
}
}

renderView 函数中,我显示了一个 snackbar 并更新了 fragment 的 UI。

private void renderView(View view) {

CoordinatorLayout fileSnackbarView = view.findViewById(R.id.filesCoordinatorLayout);

//Exception at this line
fileSnackbar = Snackbar.make(fileSnackbarView, String.format(getResources().getString(R.string.snackbarMessage), FILE_COUNT), Snackbar.LENGTH_INDEFINITE)
.setAction(getResources().getString(R.string.snackbarAction), new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), ViewSelectedFilesActivity.class);
Bundle bundle = new Bundle();
ArrayList<String> selectedFileName = new ArrayList<>();
selectedFileName.add(commonMethods.getFromPreferences(Constants.originalFileNameKey, "string"));
bundle.putStringArrayList("fileList", selectedFileName);
intent.putExtras(bundle);
startActivity(intent);
}
});
... code
}

但是,我的应用程序崩溃并出现异常 Fragment not attached to a context。下面是完整的堆栈跟踪:

03-08 12:09:20.647 11490-11490/com.zeetim.zeeprintmobile.android E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.zeetim.zeeprintmobile.android, PID: 11490
java.lang.IllegalStateException: Fragment HomeFragment{674af0} not attached to a context.
at android.support.v4.app.Fragment.requireContext(Fragment.java:611)
at android.support.v4.app.Fragment.getResources(Fragment.java:675)
at com.zeetim.zeeprintmobile.fragment.HomeFragment.renderView(HomeFragment.java:287)
at com.zeetim.zeeprintmobile.fragment.HomeFragment.access$700(HomeFragment.java:51)
at com.zeetim.zeeprintmobile.fragment.HomeFragment$ZipFile.onPostExecute(HomeFragment.java:595)
at com.zeetim.zeeprintmobile.fragment.HomeFragment$ZipFile.onPostExecute(HomeFragment.java:563)
at android.os.AsyncTask.finish(AsyncTask.java:695)
at android.os.AsyncTask.-wrap1(Unknown Source:0)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

在一些调试中,我发现 isAdded() 返回 falsegetActivity(), getContext()null

流程似乎非常简单,但行不通。那么我在这方面有什么遗漏吗?
如何从 onActivityResult 调用的 onPostExecute 更新 fragment 的 UI?

谢谢。

最佳答案

onPostExecute 中试试这个

   new Handler().postDelayed(new Runnable() {
@Override
public void run() {

//if(isAdded()) {
if (fileName != null) {
renderView(view);
}
}

// }
},300);

关于android - 从 onActivityResult 调用的 onPostExecute 更新 fragment 的 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49171787/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com