我想从服务器获取数据并在列表视图中显示数据。应用程序一旦运行就会停止。我不知道哪个部分出了问题。我的应用程序停止并显示以下logcat消息:
10-08 07:45:56.996: D/OpenGLRenderer(1446): Enabling debug mode 0
10-08 07:45:57.092: W/EGL_genymotion(1446): eglSurfaceAttrib not implemented
10-08 07:46:02.356: I/Choreographer(1446): Skipped 315 frames! The application may be doing too much work on its main thread.
10-08 07:46:07.424: I/Choreographer(1446): Skipped 303 frames! The application may be doing too much work on its main thread.
10-08 07:46:07.716: E/WindowManager(1446): android.view.WindowLeaked: Activity com.zanqi.datatolistview.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{527e8d54 V.E..... R......D 0,0-1664,192} that was originally added here
10-08 07:46:07.716: E/WindowManager(1446): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:348)
10-08 07:46:07.716: E/WindowManager(1446): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
10-08 07:46:07.716: E/WindowManager(1446): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
10-08 07:46:07.716: E/WindowManager(1446): at android.app.Dialog.show(Dialog.java:286)
10-08 07:46:07.716: E/WindowManager(1446): at com.zanqi.datatolistview.MainActivity$ProgressTask.onPreExecute(MainActivity.java:64)
10-08 07:46:07.716: E/WindowManager(1446): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
10-08 07:46:07.716: E/WindowManager(1446): at android.os.AsyncTask.execute(AsyncTask.java:535)
10-08 07:46:07.716: E/WindowManager(1446): at com.zanqi.datatolistview.MainActivity.onCreate(MainActivity.java:43)
10-08 07:46:07.716: E/WindowManager(1446): at android.app.Activity.performCreate(Activity.java:5231)
10-08 07:46:07.716: E/WindowManager(1446): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-08 07:46:07.716: E/WindowManager(1446): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
10-08 07:46:07.716: E/WindowManager(1446): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
10-08 07:46:07.716: E/WindowManager(1446): at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-08 07:46:07.716: E/WindowManager(1446): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-08 07:46:07.716: E/WindowManager(1446): at android.os.Handler.dispatchMessage(Handler.java:102)
10-08 07:46:07.716: E/WindowManager(1446): at android.os.Looper.loop(Looper.java:136)
10-08 07:46:07.716: E/WindowManager(1446): at android.app.ActivityThread.main(ActivityThread.java:5017)
10-08 07:46:07.716: E/WindowManager(1446): at java.lang.reflect.Method.invokeNative(Native Method)
10-08 07:46:07.716: E/WindowManager(1446): at java.lang.reflect.Method.invoke(Method.java:515)
10-08 07:46:07.716: E/WindowManager(1446): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-08 07:46:07.716: E/WindowManager(1446): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-08 07:46:07.716: E/WindowManager(1446): at dalvik.system.NativeStart.main(Native Method)
有人可以告诉我我的代码有什么问题吗?我的代码是:
public class MainActivity extends ListActivity {
private Context context;
private static String url = "";
private static final String NUMBER = "Number";
private static final String INFO = "Info";
private static final String REMARKS = "Remarks";
ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>();
ListView lv ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new ProgressTask(MainActivity.this).execute(); }
private class ProgressTask extends AsyncTask<String, Void, Boolean> {
private ProgressDialog dialog;
private ListActivity activity;
// private List<Message> messages;
public ProgressTask(ListActivity activity) {
this.activity = activity;
context = activity;
dialog = new ProgressDialog(context); }
private Context context;
protected void onPreExecute() {
this.dialog.setMessage("Progress start");
this.dialog.show(); }
@Override
protected void onPostExecute(final Boolean success) {
if (dialog.isShowing()) { dialog.dismiss(); }
ListAdapter adapter = new SimpleAdapter(context, jsonlist, R.layout.list_item, new String[] { NUMBER, INFO, REMARKS },
new int[] { R.id.vehicleType, R.id.vehicleColor, R.id.fuel }); setListAdapter(adapter);
//select single ListView item
lv = getListView(); }
protected Boolean doInBackground(final String... args) {
JSONParser jParser = new JSONParser();
// get JSON data from URL
JSONArray json = jParser.getJSONFromUrl(url);
for (int i = 0; i < json.length(); i++) {
try {
JSONObject c = json.getJSONObject(i);
String number = c.getString(NUMBER);
String info = c.getString(INFO);
String remarks = c.getString(REMARKS);
HashMap<String, String> map = new HashMap<String, String>();
// Add child node to HashMap key & value
map.put(NUMBER, number);
map.put(INFO, info);
map.put(REMARKS, remarks);
jsonlist.add(map);
} catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}
}
}
public class JSONParser {
static InputStream iStream = null;
static JSONArray jarray = null;
static String json = "";
public JSONParser() { }
public JSONArray getJSONFromUrl(String url) {
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) { builder.append(line); } }
else { Log.e("==>", "Failed to download file"); } }
catch (ClientProtocolException e) { e.printStackTrace(); }
catch (IOException e) { e.printStackTrace(); }
// Parse String to JSON object
try {
jarray = new JSONArray( builder.toString());
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString()); }
// return JSON Object
return jarray;
}
}
答案 0 :(得分:0)
这是Leaked Window
错误。当一些UI函数在后台访问活动时会发生这种情况。
在MainActivity
方法第72行的onPreExecute
中查看此内容,
if(dialog != null && !activity.isFinishing()) {
this.dialog.setMessage("Progress start");
this.dialog.show();
}
检查activity
是否不在后台且Dialog
不是null
,然后显示。
答案 1 :(得分:0)
有两种可能的答案:
答案 2 :(得分:0)
您可以像AsyncTask
一样从onPreExcecute
修改用户界面。
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
dialog.setMessage("Progress start");
dialog.show();
}
});