我的应用程序崩溃并且说#34;不幸的是已停止"每当我在HTTP连接期间取消/取消进度拨号时,或者由于互联网速度慢而无法访问服务器时。这是类,XML和logCat:
package com.abc123;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class Result extends ListActivity {
//final String category = ((Spinner) findViewById(R.id.spinner1)).getSelectedItem().toString();
//final String location = ((Spinner) findViewById(R.id.spinner2)).getSelectedItem().toString();
String category = "Furniture";
String location = "Kugbo";
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> productsList;
// url to get all products list
private String url_all_products = "localhost://abc.com.ng/123.php?category="+category+"&location="+location;
category="+category+"&location="+location;
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "data";
private static final String TAG_NAME = "name";
private static final String TAG_ADDRESS = "address";
private static final String TAG_PHONE = "phone";
// products JSONArray
JSONArray products = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
// Hashmap for ListView
productsList = new ArrayList<HashMap<String, String>>();
// Loading results in Background Thread
new LoadAllProducts().execute();
// Get listview
ListView lv = getListView();
}
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Result.this);
//pDialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.home_img));
pDialog.setMessage("we don dey reach...");
pDialog.setIndeterminate(true);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* getting results 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_products, "GET", params);
// Check your log cat for JSON reponse
Log.d("Results: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// results returned
// Getting Array of results
products = json.getJSONArray(TAG_PRODUCTS);
// looping through results
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String name = c.getString(TAG_NAME);
String address = c.getString(TAG_ADDRESS);
String phone = c.getString(TAG_PHONE);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_NAME, name);
map.put(TAG_ADDRESS, address);
map.put(TAG_PHONE, phone);
// adding HashList to ArrayList
productsList.add(map);
}
}
else {
Toast.makeText(getApplicationContext(), "oops! No results for the search criteria", 3).show();
}
} 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 results
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
Result.this, productsList,
R.layout.list_item, new String[] { TAG_NAME,
TAG_ADDRESS, TAG_PHONE},
new int[] { R.id.name, R.id.address, R.id.phone });
// updating listview
setListAdapter(adapter);
}
});
}
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout2"
android:background="@color/resultPage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!--<include layout="@layout/header" />-->
<include layout="@layout/footer_2" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="45dp"
android:layout_marginBottom="30dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:divider="@null"
android:cacheColorHint="#00000000"
android:scrollbarThumbVertical="@drawable/scroller"
/>
</LinearLayout>
</RelativeLayout>
LOGCAT:
04-30 19:47:21.425: I/Choreographer(1160): Skipped 76 frames! The application may be doing too much work on its main thread.
04-30 19:47:22.935: D/dalvikvm(1160): GC_FOR_ALLOC freed 45K, 4% free 3616K/3752K, paused 143ms, total 148ms
04-30 19:47:23.635: I/Choreographer(1160): Skipped 59 frames! The application may be doing too much work on its main thread.
04-30 19:47:24.935: I/Choreographer(1160): Skipped 805 frames! The application may be doing too much work on its main thread.
04-30 19:47:25.565: I/Choreographer(1160): Skipped 316 frames! The application may be doing too much work on its main thread.
04-30 19:47:26.035: I/Choreographer(1160): Skipped 264 frames! The application may be doing too much work on its main thread.
04-30 19:47:26.635: I/Choreographer(1160): Skipped 30 frames! The application may be doing too much work on its main thread.
04-30 19:47:26.735: W/InputEventReceiver(1160): Attempted to finish an input event but the input event receiver has already been disposed.
04-30 19:47:26.735: I/Choreographer(1160): Skipped 66 frames! The application may be doing too much work on its main thread.
04-30 19:47:28.265: W/System.err(1160): org.apache.http.conn.ConnectTimeoutException: Connect to /68.169.55.104:80 timed out
04-30 19:47:28.275: W/System.err(1160): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:121)
04-30 19:47:28.275: W/System.err(1160): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
04-30 19:47:28.275: W/System.err(1160): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
04-30 19:47:28.285: W/System.err(1160): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-30 19:47:28.285: W/System.err(1160): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
04-30 19:47:28.285: W/System.err(1160): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
04-30 19:47:28.285: W/System.err(1160): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
04-30 19:47:28.295: W/System.err(1160): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
04-30 19:47:28.295: W/System.err(1160): at com.insidetown.JSONParser.makeHttpRequest(JSONParser.java:67)
04-30 19:47:28.295: W/System.err(1160): at com.insidetown.Result$LoadAllProducts.doInBackground(Result.java:104)
04-30 19:47:28.295: W/System.err(1160): at com.insidetown.Result$LoadAllProducts.doInBackground(Result.java:1)
04-30 19:47:28.295: W/System.err(1160): at android.os.AsyncTask$2.call(AsyncTask.java:288)
04-30 19:47:28.305: W/System.err(1160): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-30 19:47:28.305: W/System.err(1160): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-30 19:47:28.315: W/System.err(1160): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-30 19:47:28.315: W/System.err(1160): at java.lang.Thread.run(Thread.java:841)
04-30 19:47:28.315: E/Buffer Error(1160): Error converting result java.lang.NullPointerException: lock == null
04-30 19:47:28.325: E/JSON Parser(1160): Error parsing data org.json.JSONException: End of input at character 0 of
04-30 19:47:28.335: W/dalvikvm(1160): threadid=12: thread exiting with uncaught exception (group=0xb4b10b90)
04-30 19:47:28.355: E/AndroidRuntime(1160): FATAL EXCEPTION: AsyncTask #1
04-30 19:47:28.355: E/AndroidRuntime(1160): Process: com.insidetown, PID: 1160
04-30 19:47:28.355: E/AndroidRuntime(1160): java.lang.RuntimeException: An error occured while executing doInBackground()
04-30 19:47:28.355: E/AndroidRuntime(1160): at android.os.AsyncTask$3.done(AsyncTask.java:300)
04-30 19:47:28.355: E/AndroidRuntime(1160): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
04-30 19:47:28.355: E/AndroidRuntime(1160): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
04-30 19:47:28.355: E/AndroidRuntime(1160): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
04-30 19:47:28.355: E/AndroidRuntime(1160): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-30 19:47:28.355: E/AndroidRuntime(1160): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-30 19:47:28.355: E/AndroidRuntime(1160): at java.lang.Thread.run(Thread.java:841)
04-30 19:47:28.355: E/AndroidRuntime(1160): Caused by: java.lang.NullPointerException
04-30 19:47:28.355: E/AndroidRuntime(1160): at com.insidetown.Result$LoadAllProducts.doInBackground(Result.java:108)
04-30 19:47:28.355: E/AndroidRuntime(1160): at com.insidetown.Result$LoadAllProducts.doInBackground(Result.java:1)
04-30 19:47:28.355: E/AndroidRuntime(1160): at android.os.AsyncTask$2.call(AsyncTask.java:288)
04-30 19:47:28.355: E/AndroidRuntime(1160): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-30 19:47:28.355: E/AndroidRuntime(1160): ... 3 more
04-30 19:47:29.235: I/Choreographer(1160): Skipped 426 frames! The application may be doing too much work on its main thread.
04-30 19:47:29.335: I/Choreographer(1160): Skipped 58 frames! The application may be doing too much work on its main thread.
04-30 19:47:31.165: I/Choreographer(1160): Skipped 169 frames! The application may be doing too much work on its main thread.
04-30 19:47:36.685: I/Process(1160): Sending signal. PID: 1160 SIG: 9
答案 0 :(得分:0)
从你的堆栈跟踪看,它似乎与对话框无关,而是来自服务器的代码试图解析的可疑响应。
也许有一个更完整的堆栈跟踪,如果它尝试.dismiss()对话框,它会显示正在发生的事情?
答案 1 :(得分:0)
所以我今天有时间这样做 - 你的AsyncTask看起来应该是这样的。
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
//pDialog = new ProgressDialog(CancelAsyncTask.this);
//pDialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.home_img));
//pDialog.setMessage("we don dey reach...");
//pDialog.setIndeterminate(true);
//pDialog.setCancelable(true);
//pDialog.setCanceledOnTouchOutside(true);
//pDialog.show();
pDialog = ProgressDialog.show(
CancelAsyncTaskActivity.this,
"Title",
"we don dey reach...",
true,
true,
new DialogInterface.OnCancelListener(){
@Override
public void onCancel(DialogInterface dialog) {
// cancel the AsyncTask here!
mAsyncTask.cancel(true);
}
}
);
}
/**
* getting results from url
* */
protected String doInBackground(String... args) {
// Do your background stuff here
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting results
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
}
});
}
}