gpt4 book ai didi

android - Fragment 中 AyncTask 中的 ProgressDialog(崩溃)

转载 作者:太空狗 更新时间:2023-10-29 14:15:28 24 4
gpt4 key购买 nike

我正在尝试在具有 ProgressDialog 的 fragment 中运行 AsyncTask,我已在其他 Activity 中多次执行此操作,但当我在 fragment 中尝试相同操作时,它会因内存泄漏而崩溃。我知道造成这种情况的原因与 ProgressDialog 有关,但我似乎无法弄清楚如何正确执行此操作。

异步任务:

    private void startNewAsyncTask() {
NetCheck asyncTask = new NetCheck();
asyncTask.execute();
}

private class NetCheck extends AsyncTask<String, String, Boolean>
{

private ProgressDialog nDialog;

protected void onPreExecute(){
super.onPreExecute();
nDialog = new ProgressDialog(getActivity());
nDialog.setTitle("Checking Network");
nDialog.setMessage("Loading..");
nDialog.setIndeterminate(false);
nDialog.setCancelable(true);
nDialog.show();
}


protected Boolean doInBackground(String... args){

ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni != null && ni.isConnected()){
try {
URL url = new URL("http://www.google.com");
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setConnectTimeout(4000);
urlc.connect();
if (urlc.getResponseCode() == 200){
return true;
}
}catch (MalformedURLException e1){
e1.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}

}

return false;
}

protected void onPostExecute(Boolean th){

if(th == true){
nDialog.dismiss();

}
else{
nDialog.dismiss();
errorMessage.setText("Error connecting to network");
}
}

错误日志:

    03-25 10:53:58.074: E/WindowManager(4549): Activity com.gmail.lucsantisf.software_project.views.ProfileView has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41d2cba8 that was originally added here
03-25 10:53:58.074: E/WindowManager(4549): android.view.WindowLeaked: Activity com.gmail.lucsantisf.software_project.views.ProfileView has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41d2cba8 that was originally added here
03-25 10:53:58.074: E/WindowManager(4549): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:418)
03-25 10:53:58.074: E/WindowManager(4549): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:294)
03-25 10:53:58.074: E/WindowManager(4549): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:226)
03-25 10:53:58.074: E/WindowManager(4549): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:151)
03-25 10:53:58.074: E/WindowManager(4549): at android.view.Window$LocalWindowManager.addView(Window.java:547)
03-25 10:53:58.074: E/WindowManager(4549): at android.app.Dialog.show(Dialog.java:277)
03-25 10:53:58.074: E/WindowManager(4549): at com.gmail.lucsantisf.software_project.fragments.ProfilePhotosFragment$NetCheck.onPreExecute(ProfilePhotosFragment.java:70)
03-25 10:53:58.074: E/WindowManager(4549): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
03-25 10:53:58.074: E/WindowManager(4549): at android.os.AsyncTask.execute(AsyncTask.java:534)
03-25 10:53:58.074: E/WindowManager(4549): at com.gmail.lucsantisf.software_project.fragments.ProfilePhotosFragment.startNewAsyncTask(ProfilePhotosFragment.java:55)
03-25 10:53:58.074: E/WindowManager(4549): at com.gmail.lucsantisf.software_project.fragments.ProfilePhotosFragment.onCreateView(ProfilePhotosFragment.java:49)
03-25 10:53:58.074: E/WindowManager(4549): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
03-25 10:53:58.074: E/WindowManager(4549): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
03-25 10:53:58.074: E/WindowManager(4549): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
03-25 10:53:58.074: E/WindowManager(4549): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
03-25 10:53:58.074: E/WindowManager(4549): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
03-25 10:53:58.074: E/WindowManager(4549): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
03-25 10:53:58.074: E/WindowManager(4549): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
03-25 10:53:58.074: E/WindowManager(4549): at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
03-25 10:53:58.074: E/WindowManager(4549): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
03-25 10:53:58.074: E/WindowManager(4549): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
03-25 10:53:58.074: E/WindowManager(4549): at android.view.View.measure(View.java:15264)
03-25 10:53:58.074: E/WindowManager(4549): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916)
03-25 10:53:58.074: E/WindowManager(4549): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
03-25 10:53:58.074: E/WindowManager(4549): at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
03-25 10:53:58.074: E/WindowManager(4549): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
03-25 10:53:58.074: E/WindowManager(4549): at android.view.View.measure(View.java:15264)
03-25 10:53:58.074: E/WindowManager(4549): at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1196)
03-25 10:53:58.074: E/WindowManager(4549): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-25 10:53:58.074: E/WindowManager(4549): at android.widget.ScrollView.onMeasure(ScrollView.java:318)
03-25 10:53:58.074: E/WindowManager(4549): at android.view.View.measure(View.java:15264)
03-25 10:53:58.074: E/WindowManager(4549): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916)
03-25 10:53:58.074: E/WindowManager(4549): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-25 10:53:58.074: E/WindowManager(4549): at android.view.View.measure(View.java:15264)
03-25 10:53:58.074: E/WindowManager(4549): at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
03-25 10:53:58.074: E/WindowManager(4549): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
03-25 10:53:58.074: E/WindowManager(4549): at android.view.View.measure(View.java:15264)
03-25 10:53:58.074: E/WindowManager(4549): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916)
03-25 10:53:58.074: E/WindowManager(4549): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-25 10:53:58.074: E/WindowManager(4549): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2339)
03-25 10:53:58.074: E/WindowManager(4549): at android.view.View.measure(View.java:15264)
03-25 10:53:58.074: E/WindowManager(4549): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2131)
03-25 10:53:58.074: E/WindowManager(4549): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1242)
03-25 10:53:58.074: E/WindowManager(4549): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1435)
03-25 10:53:58.074: E/WindowManager(4549): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1127)
03-25 10:53:58.074: E/WindowManager(4549): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4606)
03-25 10:53:58.074: E/WindowManager(4549): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:747)
03-25 10:53:58.074: E/WindowManager(4549): at android.view.Choreographer.doCallbacks(Choreographer.java:567)
03-25 10:53:58.074: E/WindowManager(4549): at android.view.Choreographer.doFrame(Choreographer.java:536)
03-25 10:53:58.074: E/WindowManager(4549): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:733)
03-25 10:53:58.074: E/WindowManager(4549): at android.os.Handler.handleCallback(Handler.java:615)
03-25 10:53:58.074: E/WindowManager(4549): at android.os

有人知道我做错了什么吗?

感谢您的帮助!

最佳答案

您正在调用 fragment 的 onCreateView() 中的 show() 方法。该 fragment 尚未通过其 onResume() 回调,因此它没有可供进度对话框使用的有效窗口,因此这种不匹配会导致泄漏。将 AsyncTask 的开头移动到 fragment 的 onResume() 方法中,这应该可以解决问题。

关于android - Fragment 中 AyncTask 中的 ProgressDialog(崩溃),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22632228/

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