2013-07-28 76 views
-2

我在手上有點問題,我需要一點點。我的應用程序總是崩潰,當我看堆棧跟蹤這是我回來......Android ListView窗口泄漏錯誤

theproblemsolver.ListView has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40d0e800 V.E..... R.....ID 0,0-234,72} that was originally added here 
07-27 11:05:56.119: E/WindowManager(913): android.view.WindowLeaked: Activity com.theproblemsolver.ListView has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40d0e800 V.E..... R.....ID 0,0-234,72} that was originally added here 
07-27 11:05:56.119: E/WindowManager(913): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:354) 
07-27 11:05:56.119: E/WindowManager(913): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216) 
07-27 11:05:56.119: E/WindowManager(913): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) 
07-27 11:05:56.119: E/WindowManager(913): at android.app.Dialog.show(Dialog.java:281) 
07-27 11:05:56.119: E/WindowManager(913): at com.theproblemsolver.ListView$LoadAllData.onPreExecute(ListView.java:85) 
07-27 11:05:56.119: E/WindowManager(913): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586) 
07-27 11:05:56.119: E/WindowManager(913): at android.os.AsyncTask.execute(AsyncTask.java:534) 
07-27 11:05:56.119: E/WindowManager(913): at com.theproblemsolver.ListView.onCreate(ListView.java:52) 
07-27 11:05:56.119: E/WindowManager(913): at android.app.Activity.performCreate(Activity.java:5104) 
07-27 11:05:56.119: E/WindowManager(913): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
27 11:05:56.119: E/WindowManager(913): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
07-27 11:05:56.119: E/WindowManager(913): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
07-27 11:05:56.119: E/WindowManager(913): at android.app.ActivityThread.access$600(ActivityThread.java:141) 
07-27 11:05:56.119: E/WindowManager(913): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
07-27 11:05:56.119: E/WindowManager(913): at android.os.Handler.dispatchMessage(Handler.java:99) 
07-27 11:05:56.119: E/WindowManager(913): at android.os.Looper.loop(Looper.java:137) 
07-27 11:05:56.119: E/WindowManager(913): at android.app.ActivityThread.main(ActivityThread.java:5041) 
07-27 11:05:56.119: E/WindowManager(913): at java.lang.reflect.Method.invokeNative(Native Method) 
07-27 11:05:56.119: E/WindowManager(913): at java.lang.reflect.Method.invoke(Method.java:511) 
07-27 11:05:56.119: E/WindowManager(913): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
07-27 11:05:56.119: E/WindowManager(913): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
07-27 11:05:56.119: E/WindowManager(913): at dalvik.system.NativeStart.main(Native Method) 

有人可以告訴我這是什麼,我能做些什麼來解決這個..

MainActivity

public class MainActivity extends Activity { 

    EditText et; 
    Button getanswer; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Button getanswer = (Button) findViewById(R.id.button1); 
     getanswer.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Intent i = new Intent(MainActivity.this, ListView.class); 
      startActivity(i); 
     } 


     }); 
    } 
} 

ListView的活動

public class ListView extends ListActivity { 

    private ProgressDialog pDialog; 

    JSONParsser jParser = new JSONParsser(); 
    ArrayList<HashMap<String, String>> questionList; 

    private static String URI = "http://example.com/json";; 

    //JSONObject json = jParser.getJSONFromURI(URI); 

    final String TAG_RESULTS = "ResultsSet"; 
    final String TAG_SUBJECT = "Subject"; 
    final String TAG_NUMANSWERS = "NumAnswers"; 
    final String TAG_QUESTION = "Question"; 
    final String TAG_QUESTION_CONTENT = "Content"; 
    final String TAG_QUESTION_CHOSENANSWER = "ChosenAnswer"; 
    final String TAG_QUESTION_ANSWERS = "Answers"; 

      JSONArray ResultsSet = null; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     //setContentView(R.layout.listview); 

    questionList = new ArrayList<HashMap<String, String>>(); 

    android.widget.ListView lv = getListView(); 

    new LoadAllData().execute(); 

    lv.setOnItemClickListener(new OnItemClickListener() { 

    @Override 
     public void onItemClick(AdapterView<?> parent, View view,int position, long id) { 


     } 
    }); 

} 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 

     if (resultCode == 100) { 
      Intent intent = getIntent(); 
      finish(); 
      startActivity(intent); 
     } 
    } 

    class LoadAllData extends AsyncTask<String, String, String> { 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(ListView.this); 
      pDialog.setMessage("Loading Data. Please wait..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(false); 
      pDialog.show(); 
     } 

     protected String doInBackground(String... args) { 

      try { 
        JSONObject json = jParser.getJSONFromURI(URI); 
        ResultsSet = json.getJSONArray(TAG_RESULTS); 

        for(int i = 0; i < ResultsSet.length(); i++) { 
         JSONObject r = ResultsSet.getJSONObject(i); 
         String Subject = r.getString(TAG_SUBJECT); 
         String NumAnswers = r.getString(TAG_NUMANSWERS); 

         JSONObject Question = r.getJSONObject(TAG_QUESTION); 
         String Content = Question.getString(TAG_QUESTION_CONTENT); 
         String ChosenAnswer = Question.getString(TAG_QUESTION_CHOSENANSWER); 
         String Answers = Question.getString(TAG_QUESTION_ANSWERS); 

         HashMap<String, String> map = new HashMap<String, String>(); 

         map.put(TAG_SUBJECT, Subject); 
         map.put(TAG_NUMANSWERS, NumAnswers); 
        } 



      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
      return null; 


     } 


     protected void onPostExecute(String file_URI) { 

      pDialog.dismiss(); 
      runOnUiThread(new Runnable() { 
      public void run() { 
       ListAdapter adapter = new SimpleAdapter(getBaseContext(), questionList, 
         R.layout.row, 
         new String[] { TAG_SUBJECT, TAG_NUMANSWERS }, new int[] { 
         R.id.Subject, R.id.NumAnswers }); 

       setListAdapter(adapter); 



       } 
      }); 

     } 
}} 

SimpleListItem

public class SingleListItem extends Activity { 

    TextView subject; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     this.setContentView(R.layout.singlelistitem); 

    subject = (TextView) findViewById(R.id.Subject2); 

    Intent i = getIntent(); 
    String Subject = i.getStringExtra("TAG_SUBJECT"); 
    subject.setText(Subject); 

    } 
} 

我把兩個活動是在我的項目,我沒有張貼的原因是因爲我不知道他們是否重要與否

JSONParsser活動

public class JSONParsser { 

    static InputStream is = null; 
    static JSONObject jObj = null; 
    static String json = ""; 
    EditText et; 

    public JSONParsser() { 
    } 

    public JSONObject getJSONFromURI(String URI) { 

     try{ 
     HttpClient client = new DefaultHttpClient(); 
     try { 
      URI website = new URI("http://example.com/json"); 
      HttpGet request = new HttpGet(); 
      request.setURI(website); 
      try { 
       HttpResponse response = client.execute(request); 
      HttpEntity httpEntity = response.getEntity(); 
      is = httpEntity.getContent(); 

      } catch (ClientProtocolException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } catch (URISyntaxException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     return jObj; 

     }finally{} 

    }{ 

    }} 

InstanceOfJSONParsser活動

public class JSONParserInstance { 

    JSONParsser jParser = new JSONParsser(); 

    public String URI; 

    JSONObject json = jParser.getJSONFromURI(URI); 


    private static final String TAG_RESULTS = "ResultsSet"; 
    private static final String TAG_SUBJECT = "Subject"; 
    private static final String TAG_NUMANSWERS = "NumAnswers"; 
    private static final String TAG_QUESTION = "Question"; 
    private static final String TAG_QUESTION_CONTENT = "Content"; 
    private static final String TAG_QUESTION_CHOSENANSWER = "ChosenAnswer"; 
    private static final String TAG_QUESTION_ANSWERS = "Answers"; 

      JSONArray ResultsSet = null; { 

    try { 
     ResultsSet = json.getJSONArray(TAG_RESULTS); 

     for(int i = 0; i < ResultsSet.length(); i++){ 
      JSONObject r = ResultsSet.getJSONObject(i); 

      String Subject = r.getString(TAG_SUBJECT); 
      String NmAnswers = r.getString(TAG_NUMANSWERS); 

      JSONObject Question = r.getJSONObject(TAG_QUESTION); 
      String Content = Question.getString(TAG_QUESTION_CONTENT); 
      String ChosenAnswer = Question.getString(TAG_QUESTION_CHOSENANSWER); 
      String Answers = Question.getString(TAG_QUESTION_ANSWERS); 

     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 

}}} 
+0

不太確定是什麼原因造成的問題,但兩件事情看起來很怪異。你在doInBackground中創建地圖,根本不使用它。其次你通過onPostExecute中的runOnUiThread運行代碼。 AFAIK onPostExecute無論如何都是在UI線程上運行的。 – robotoaster

+0

它看起來像你在調用'pDialog.dismiss();'當Activity已經退出。嘗試在'onPause()'中解僱它,以確保在Activity停用時它被正確關閉。 –

回答

0

看來,您的AsyncTask具有很強的參考你的活動。請考慮使用具有WeakReference的靜態類來代替。

private static class StaticAsyncTask extends AsyncTask<String, String, String>() { 
    private WeakReference<Activity> mActivityRef; 

    private StaticAsyncTask(Activity callingActivity) { 
     mActivityRef = new WeakReference<Activity>(callingActivity); 
    } 

    @Override 
    protected String doInBackground(String... args) { 
     // TODO: Do stuff 
    } 

    @Override 
    protected void onProgressUpdate(String... progress) { 
     // TODO: Do stuff 
    } 

    @Override 
    protected void onPostExecute(String results) { 
     Activity activity = mActivityRef.get(); 
     if (activity != null) { 
      // TODO: Do stuff with my non-null activity 
     } 
    } 
} 
+0

你可以用我的代碼寫這個asynctask嗎我可以看到一個例子 –

+0

我編輯我的問題 –

+0

我很難破譯你的AsyncTask試圖完成什麼(充分利用這三個參數!AsyncTask )。 基本上,任何時候你想在Activity中使用一個字段或方法,或者在Activity中訪問一個View,從WeakReference獲取(),看它是否有效。我建議避免從doInBackground中訪問Activity。 – JuniKim