基本上我收到此错误,我不确定代码的哪一部分导致它,只是寻找一些错误的建议,谢谢。 。
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 :(得分: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);
}
并取消pDialog
中的onDestroy()
,就像这样,
if(pDialog != null ){
pDialog.cancel();
}
希望这会对你有所帮助。
答案 1 :(得分:0)
onPostExecute()是UI线程。所以runOnUIThread()是错误的。此外,我会考虑将您的AsyncTask与它所处的活动分开。将来它将更加可重用。
答案 2 :(得分: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();
}