這是我在真實設備上嘗試我的應用程序時得到的錯誤。 在模擬器,一切工作正常在AsyncTask中的真實設備上的應用程序崩潰,但不在仿真器上
10-24 20:10:01.330: E/Buffer Error(10479): Error converting result java.lang.NullPointerException
10-24 20:10:01.330: E/JSON Parser(10479): Error parsing data org.json.JSONException: End of input at character 0 of
10-24 20:10:01.330: E/AndroidRuntime(10479): FATAL EXCEPTION: AsyncTask #1
10-24 20:10:01.330: E/AndroidRuntime(10479): java.lang.RuntimeException: An error occured while executing doInBackground()
10-24 20:10:01.330: E/AndroidRuntime(10479): at android.os.AsyncTask$3.done(AsyncTask.java)
10-24 20:10:01.330: E/AndroidRuntime(10479): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java)
10-24 20:10:01.330: E/AndroidRuntime(10479): at java.util.concurrent.FutureTask.setException(FutureTask.java)
10-24 20:10:01.330: E/AndroidRuntime(10479): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java)
10-24 20:10:01.330: E/AndroidRuntime(10479): at java.util.concurrent.FutureTask.run(FutureTask.java)
10-24 20:10:01.330: E/AndroidRuntime(10479): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java)
10-24 20:10:01.330: E/AndroidRuntime(10479): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java)
10-24 20:10:01.330: E/AndroidRuntime(10479): at java.lang.Thread.run(Thread.java)
10-24 20:10:01.330: E/AndroidRuntime(10479): Caused by: java.lang.NullPointerException
10-24 20:10:01.330: E/AndroidRuntime(10479): at app.nightfind.easynight.SearchResult$LoadAllProducts.doInBackground(SearchResult.java:140)
10-24 20:10:01.330: E/AndroidRuntime(10479): at app.nightfind.easynight.SearchResult$LoadAllProducts.doInBackground(SearchResult.java:1)
10-24 20:10:01.330: E/AndroidRuntime(10479): at android.os.AsyncTask$2.call(AsyncTask.java)
10-24 20:10:01.330: E/AndroidRuntime(10479): ... 5 more
10-24 20:10:01.950: E/WindowManager(10479): Activity app.nightfind.easynight.SearchResult has leaked window [email protected] that was originally added here
10-24 20:10:01.950: E/WindowManager(10479): android.view.WindowLeaked: Activity app.nightfind.easynight.SearchResult has leaked window [email protected] that was originally added here
10-24 20:10:01.950: E/WindowManager(10479): at android.view.ViewRootImpl.<init>(ViewRootImpl.java)
10-24 20:10:01.950: E/WindowManager(10479): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java)
10-24 20:10:01.950: E/WindowManager(10479): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java)
10-24 20:10:01.950: E/WindowManager(10479): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java)
10-24 20:10:01.950: E/WindowManager(10479): at android.view.Window$LocalWindowManager.addView(Window.java)
10-24 20:10:01.950: E/WindowManager(10479): at android.app.Dialog.show(Dialog.java)
10-24 20:10:01.950: E/WindowManager(10479): at app.nightfind.easynight.SearchResult$LoadAllProducts.onPreExecute(SearchResult.java:115)
10-24 20:10:01.950: E/WindowManager(10479): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java)
10-24 20:10:01.950: E/WindowManager(10479): at android.os.AsyncTask.execute(AsyncTask.java)
10-24 20:10:01.950: E/WindowManager(10479): at app.nightfind.easynight.SearchResult.onCreate(SearchResult.java:76)
10-24 20:10:01.950: E/WindowManager(10479): at android.app.Activity.performCreate(Activity.java)
10-24 20:10:01.950: E/WindowManager(10479): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
10-24 20:10:01.950: E/WindowManager(10479): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
10-24 20:10:01.950: E/WindowManager(10479): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
10-24 20:10:01.950: E/WindowManager(10479): at android.app.ActivityThread.access$600(ActivityThread.java)
10-24 20:10:01.950: E/WindowManager(10479): at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
10-24 20:10:01.950: E/WindowManager(10479): at android.os.Handler.dispatchMessage(Handler.java)
10-24 20:10:01.950: E/WindowManager(10479): at android.os.Looper.loop(Looper.java)
10-24 20:10:01.950: E/WindowManager(10479): at android.app.ActivityThread.main(ActivityThread.java)
10-24 20:10:01.950: E/WindowManager(10479): at java.lang.reflect.Method.invokeNative(Native Method)
10-24 20:10:01.950: E/WindowManager(10479): at java.lang.reflect.Method.invoke(Method.java)
10-24 20:10:01.950: E/WindowManager(10479): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
10-24 20:10:01.950: E/WindowManager(10479): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
10-24 20:10:01.950: E/WindowManager(10479): at dalvik.system.NativeStart.main(Native Method)
這是searchreslut.java 的URL都ok
public class SearchResult extends Activity {
ListView Listview;
LazyAdapter adapter;
private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> productsList;
private static String url_all_products = "http://*****";
private static String url_images = "http://*****";
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "products";
public static String TAG_PID = "pid";
public static String TAG_NAME = "name";
public static String TAG_PTYPE = "ptype";
public static String TAG_DESC = "desc";
public static String TAG_ADDRESS = "address";
public static String TAG_CITY = "city";
public static String TAG_IMGT = "cover";
JSONArray products = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_result);
productsList = new ArrayList<HashMap<String, String>>();
new LoadAllProducts().execute();
Listview = (ListView) findViewById (R.id.slist);
Listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String pid = ((TextView) view.findViewById(R.id.pid)).getText()
.toString();
Intent in = new Intent(getApplicationContext(),
ViewBusiness.class);
in.putExtra(TAG_PID, pid);
startActivityForResult(in, 100);
}
});
}
public class LoadAllProducts extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(SearchResult.this);
pDialog.setMessage("LOADING..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
Bundle extras = getIntent().getExtras();
String getwhat = extras.getString("setwhat");
String getarea = extras.getString("setarea");
String getcity = extras.getString("setcity");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("ptype", getwhat));
params.add(new BasicNameValuePair("area", getarea));
params.add(new BasicNameValuePair("city", getcity));
JSONObject json = jParser.makeHttpRequest(url_all_products, params);
Log.d("All Products: ", json.toString());
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
products = json.getJSONArray(TAG_PRODUCTS);
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
String id = c.getString(TAG_PID);
String name = c.getString(TAG_NAME);
String desc = c.getString(TAG_DESC);
String city = c.getString(TAG_CITY);
String address = c.getString(TAG_ADDRESS);
String ptype = c.getString(TAG_PTYPE);
String imgt = c.getString(TAG_IMGT);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_PID, id);
map.put(TAG_NAME, name);
map.put(TAG_DESC, desc);
map.put(TAG_CITY, city);
map.put(TAG_ADDRESS, address);
map.put(TAG_PTYPE, ptype);
map.put(TAG_IMGT, url_images + imgt);
productsList.add(map);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
pDialog.dismiss();
runOnUiThread(new Runnable() {
public void run() {
adapter = new LazyAdapter(SearchResult.this, productsList);
Listview.setAdapter(adapter);
}
});
}
}
}
我不明白它是如何及其對仿真器,但崩潰真實設備
優良工程編輯:
我添加了一個選項,當沒有connec該應用程序將顯示一條消息並關閉。 但是當我用wifi它說,該設備具有互聯網連接(和瀏覽器的工作完美),但應用程序繼續當我用wifi崩潰,當我use3G連接一切正常
也許它我的家庭WiFi引起問題?
這看起來像你的問題:在app.nightfind.easynight.SearchResult $ LoadAllProducts.doInBackground(信息搜索結果顯示java.lang.NullPointerException:所致。 java:140) 你可以看看SearchResult.java,第140行,並指出是哪一行? – Tim
這是行:Log.d(「所有產品:」,json.toString()); – iZikoo
你的json對象爲null。你有有效的互聯網連接?您應該爲無法處理Http請求的情況做好準備,並顯示錯誤消息或其他內容。不要以爲你會從你的Http請求中獲得有效的數據。 – Tim