2013-04-04 67 views
0

基本上我得到這個錯誤,我不確定代碼的哪一部分導致它,只是尋找一些什麼是錯的建議,謝謝。 。活動泄漏了最初在這裏添加的窗口

public class ViewHole extends ListActivity { 

// Progress Dialog 
private ProgressDialog pDialog; 

// Creating JSON Parser object 
JSONParser jParser = new JSONParser(); 

ArrayList<HashMap<String, String>> holesList; 


// url to get all products list 
private static String url_all_holes = "http://localhost/realdeal/get_hole_details.php"; 

// JSON Node names 
private static final String TAG_SUCCESS = "success"; 
private static final String TAG_COURSEONE = "courseone"; 
private static final String TAG_HOLENUMBER = "holenumber"; 
private static final String TAG_INDEX = "index"; 
private static final String TAG_YARDAGE = "yardage"; 
private static final String TAG_PAR = "par"; 

// products JSONArray 
JSONArray courseone = null; 

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

    // Hashmap for ListView 
    holesList = new ArrayList<HashMap<String, String>>(); 

    // Loading products in Background Thread 
    new loadallholes().execute(); 



/** 
* Background Async Task to Load all holes by making HTTP Request 
* */ 
class loadallholes extends AsyncTask<String, String, String> { 

    /** 
    * Before starting background thread Show Progress Dialog 
    * */ 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(ViewHole.this); 
     pDialog.setMessage("Loading holes. Please wait..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(false); 
     pDialog.show(); 
    } 

    /** 
    * getting All holes from url 
    * */ 
    protected String doInBackground(String... args) { 
     // Building Parameters 
     List<NameValuePair> params = new ArrayList<NameValuePair>(); 
     // getting JSON string from URL 
     JSONObject json = jParser.makeHttpRequest(url_all_holes, "GET", params); 

     // Check your log cat for JSON response 
     Log.d("All Holes: ", json.toString()); 

     try { 
      // Checking for SUCCESS TAG 
      int success = json.getInt(TAG_SUCCESS); 

      if (success == 1) { 
       // holes found 
       // Getting Array of hole numbers 
       courseone = json.getJSONArray(TAG_COURSEONE); 

       // looping through All holes 
       for (int i = 0; i < courseone.length(); i++) { 
        JSONObject c = courseone.getJSONObject(i); 

        // Storing each json item in variable 
        String holenumber = c.getString(TAG_HOLENUMBER); 
        String index = c.getString(TAG_INDEX); 
        String yardage = c.getString(TAG_YARDAGE); 
        String par = c.getString(TAG_PAR); 




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

        // adding each child node to HashMap key => value 
        map.put(TAG_HOLENUMBER, holenumber); 
        map.put(TAG_INDEX, index); 
        map.put(TAG_YARDAGE, yardage); 
        map.put(TAG_PAR, par); 





        // adding HashList to ArrayList 
        holesList.add(map); 
       } 
      } 


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

     return null; 
    } 

    /** 
    * After completing background task Dismiss the progress dialog 
    * **/ 
    protected void onPostExecute(String file_url) { 
     // dismiss the dialog after getting all products 

     pDialog.dismiss(); 
     // updating UI from Background Thread 
     runOnUiThread(new Runnable() { 
      public void run() { 
       /** 
       * Updating parsed JSON data into ListView 
       * */ 
       ListAdapter adapter = new SimpleAdapter(
         ViewHole.this, holesList, 
         R.layout.list_item, new String[] { TAG_HOLENUMBER, TAG_INDEX, TAG_YARDAGE, TAG_PAR},        
         new int[] { R.id.holenumber, R.id.index, R.id.yardage, R.id.par }); 

       // updating listview 
       setListAdapter(adapter); 
      } 
     }); 

    } 

} 
} 

logcat的錯誤 -

01-27 04:11:57.228: E/WindowManager(3566): Activity com.example.golfapp.ViewHole has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40cdf768 V.E..... R.....ID 0,0-684,192} that was originally added here 
01-27 04:11:57.228: E/WindowManager(3566): android.view.WindowLeaked: Activity com.example.golfapp.ViewHole has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40cdf768 V.E..... R.....ID 0,0-684,192} that was originally added here 
01-27 04:11:57.228: E/WindowManager(3566): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:354) 
01-27 04:11:57.228: E/WindowManager(3566): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216) 
01-27 04:11:57.228: E/WindowManager(3566): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) 
01-27 04:11:57.228: E/WindowManager(3566): at android.app.Dialog.show(Dialog.java:281) 
01-27 04:11:57.228: E/WindowManager(3566): at com.example.golfapp.ViewHole$loadallholes.onPreExecute(ViewHole.java:121) 
01-27 04:11:57.228: E/WindowManager(3566): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586) 
01-27 04:11:57.228: E/WindowManager(3566): at android.os.AsyncTask.execute(AsyncTask.java:534) 
01-27 04:11:57.228: E/WindowManager(3566): at com.example.golfapp.ViewHole.onCreate(ViewHole.java:61) 
01-27 04:11:57.228: E/WindowManager(3566): at android.app.Activity.performCreate(Activity.java:5104) 
01-27 04:11:57.228: E/WindowManager(3566): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
01-27 04:11:57.228: E/WindowManager(3566): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
01-27 04:11:57.228: E/WindowManager(3566): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
01-27 04:11:57.228: E/WindowManager(3566): at android.app.ActivityThread.access$600(ActivityThread.java:141) 
01-27 04:11:57.228: E/WindowManager(3566): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
01-27 04:11:57.228: E/WindowManager(3566): at android.os.Handler.dispatchMessage(Handler.java:99) 
01-27 04:11:57.228: E/WindowManager(3566): at android.os.Looper.loop(Looper.java:137) 
01-27 04:11:57.228: E/WindowManager(3566): at android.app.ActivityThread.main(ActivityThread.java:5039) 
01-27 04:11:57.228: E/WindowManager(3566): at java.lang.reflect.Method.invokeNative(Native Method) 
01-27 04:11:57.228: E/WindowManager(3566): at java.lang.reflect.Method.invoke(Method.java:511) 
01-27 04:11:57.228: E/WindowManager(3566): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
01-27 04:11:57.228: E/WindowManager(3566): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
01-27 04:11:57.228: E/WindowManager(3566): at dalvik.system.NativeStart.main(Native Method) 

JSON類 -

 public class JSONParser { 

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

// constructor 
public JSONParser() { 

} 

// function get json from url 
// by making HTTP POST or GET method 
public JSONObject makeHttpRequest(String url, String method, 
     List<NameValuePair> params) { 

    // Making HTTP request 
    try { 

     // check for request method 
     if(method == "POST"){ 
      // request method is POST 
      // defaultHttpClient 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url); 
      httpPost.setEntity(new UrlEncodedFormEntity(params)); 

      HttpResponse httpResponse = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      is = httpEntity.getContent(); 

     }else if(method == "GET"){ 
      // request method is GET 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      String paramString = URLEncodedUtils.format(params, "utf-8"); 
      url += "?" + paramString; 
      HttpGet httpGet = new HttpGet(url); 

      HttpResponse httpResponse = httpClient.execute(httpGet); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      is = httpEntity.getContent(); 
     }   


    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    try { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(
       is, "iso-8859-1"), 8); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
     is.close(); 
     json = sb.toString(); 
    } catch (Exception e) { 
     Log.e("Buffer Error", "Error converting result " + e.toString()); 
    } 

    // try parse the string to a JSON object 
    try { 
     jObj = new JSONObject(json); 
    } catch (JSONException e) { 
     Log.e("JSON Parser", "Error parsing data " + e.toString()); 
    } 

    // return JSON String 
    return jObj; 

} 
} 
+0

爲什麼你在onPostExecute中使用runOnUiThread? – 2013-04-04 03:35:12

回答

1

我認爲這裏不需要使用runOnUIThread()

onPostExecute

protected void onPostExecute(String file_url) { 
     // dismiss the dialog after getting all products 
     if(pDialog.isShowing()){ 
      pDialog.dismiss(); 
     } 

     ListAdapter adapter = new SimpleAdapter(
       ViewHole.this, holesList, 
       R.layout.list_item, new String[] { TAG_HOLENUMBER, TAG_INDEX, TAG_YARDAGE, TAG_PAR},        
       new int[] { R.id.holenumber, R.id.index, R.id.yardage, R.id.par }); 

       // updating listview 
       setListAdapter(adapter); 
    } 

,並取消onDestroy()這樣的pDialog

if(pDialog != null){ 
    pDialog.cancel(); 
} 

希望這將幫助你。

+0

感謝您的建議,但是當我嘗試了您的建議時,我得到了一個致命的異常:doInBackground()中的AsyncTask#1( – dGray 2013-04-04 16:30:17

+0

)是否在doInBackground()中做過任何更改? – 2013-04-04 16:58:15

+0

我還沒有在doInBackground中做過任何更改。 – dGray 2013-04-04 17:06:03

0

onPostExecute()是UI線程。所以runOnUIThread()是錯誤的。另外,我會考慮將你的AsyncTask從它所處的活動中分離出來。未來它將更加可重用。

0
try { 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost(
         url_all_holes); 

      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 

      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      ResponseHandler<String> responseHandler = new BasicResponseHandler(); 

      String yourReponseInJSONStr = httpclient.execute(httppost, 
        responseHandler); 
      //you will get json string here 
      Log.d("yourReponseInJSONStr ", yourReponseInJSONStr); 

      //check here your getting json string in logcat. 
      JSONObject yourJsonObj = new JSONObject(yourReponseInJSONStr); 

      JSONArray results = jObject.getJSONArray(TAG_COURSEONE); 

      for (int i = 0; i < results.length(); i++) { 
       //your parsing snippet 
      } 
     } 
} catch (Exception e) { 
      e.printStackTrace(); 
     } 
+0

我應該在哪裏放這個代碼,我應該刪除任何東西? – dGray 2013-04-04 18:06:00

+0

將此代碼放在第一行的doInBackground()中,並保持for循環的原樣。但首先檢查你從server.Log.d()得到的響應,如果你沒有收到響應服務器,那麼它會拋出你的json解析錯誤。 – 2013-04-04 18:10:27

+0

代碼給了我很多問題,有沒有什麼辦法可以給你發送文件? – dGray 2013-04-04 18:23:14

相關問題