我试图在具有ProgressDialog的片段中运行AsyncTask,我已经在其他活动中多次使用了这个工作,但是当我在片段中尝试相同时它会因为一些内存泄漏而崩溃。我知道这个原因与ProgressDialog有关,但我似乎无法弄清楚如何正确地做到这一点。
AsynTask:
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
有人知道我做错了吗?
感谢您的帮助!
答案 0 :(得分:1)
您正在从片段的show()
调用onCreateView()
方法。该片段尚未通过其onResume()
回调,因此它没有用于进度对话框的有效窗口,因此这种不匹配会导致泄漏。将AsyncTask
的起点移至您的片段onResume()
方法中,这样就可以解决问题。