我想在一個具有ProgressDialog的片段中運行AsyncTask,我在其他活動中多次做了這個工作,但是當我在片段中嘗試相同時,它因爲崩潰一些內存泄漏。我知道這個原因與ProgressDialog有關,但我似乎無法弄清楚如何正確地做到這一點。AyncTask中的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 [email protected] 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 [email protected] 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
是否有人知道我做錯了嗎?
感謝您的幫助!
應用程序崩潰還是隻是錯誤? – Blackbelt
是的,它崩潰 –
然後發佈整個堆棧跟蹤 – Blackbelt