我已经将此代码http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/用于GCM。但是每当我单击以查看或创建一个新的生产按钮时都会遇到麻烦。我已经检查了本教程下面介绍的解决方案,但这不能解决我的问题。这是我的
02-14 20:06:09.775: W/KeyCharacterMap(738): No keyboard for id 0
02-14 20:06:09.775: W/KeyCharacterMap(738): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
02-14 20:06:13.105: E/JSON Parser(738): Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
02-14 20:06:13.105: W/dalvikvm(738): threadid=9: thread exiting with uncaught exception (group=0x40015560)
02-14 20:06:13.115: E/AndroidRuntime(738): FATAL EXCEPTION: AsyncTask #1
02-14 20:06:13.115: E/AndroidRuntime(738): java.lang.RuntimeException: An error occured while executing doInBackground()
02-14 20:06:13.115: E/AndroidRuntime(738): at android.os.AsyncTask$3.done(AsyncTask.java:200)
02-14 20:06:13.115: E/AndroidRuntime(738): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
02-14 20:06:13.115: E/AndroidRuntime(738): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
02-14 20:06:13.115: E/AndroidRuntime(738): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
02-14 20:06:13.115: E/AndroidRuntime(738): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
02-14 20:06:13.115: E/AndroidRuntime(738): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
02-14 20:06:13.115: E/AndroidRuntime(738): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
02-14 20:06:13.115: E/AndroidRuntime(738): at java.lang.Thread.run(Thread.java:1019)
02-14 20:06:13.115: E/AndroidRuntime(738): Caused by: java.lang.NullPointerException
02-14 20:06:13.115: E/AndroidRuntime(738): at com.example.androidhive.AllProductsActivity$LoadAllProducts.doInBackground(AllProductsActivity.java:130)
02-14 20:06:13.115: E/AndroidRuntime(738): at com.example.androidhive.AllProductsActivity$LoadAllProducts.doInBackground(AllProductsActivity.java:1)
02-14 20:06:13.115: E/AndroidRuntime(738): at android.os.AsyncTask$2.call(AsyncTask.java:185)
02-14 20:06:13.115: E/AndroidRuntime(738): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
02-14 20:06:13.115: E/AndroidRuntime(738): ... 4 more
02-14 20:06:14.213: E/WindowManager(738): Activity com.example.androidhive.AllProductsActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405375e0 that was originally added here
02-14 20:06:14.213: E/WindowManager(738): android.view.WindowLeaked: Activity com.example.androidhive.AllProductsActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405375e0 that was originally added here
02-14 20:06:14.213: E/WindowManager(738): at android.view.ViewRoot.<init>(ViewRoot.java:258)
02-14 20:06:14.213: E/WindowManager(738): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
02-14 20:06:14.213: E/WindowManager(738): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
02-14 20:06:14.213: E/WindowManager(738): at android.view.Window$LocalWindowManager.addView(Window.java:424)
02-14 20:06:14.213: E/WindowManager(738): at android.app.Dialog.show(Dialog.java:241)
02-14 20:06:14.213: E/WindowManager(738): at com.example.androidhive.AllProductsActivity$LoadAllProducts.onPreExecute(AllProductsActivity.java:117)
02-14 20:06:14.213: E/WindowManager(738): at android.os.AsyncTask.execute(AsyncTask.java:391)
02-14 20:06:14.213: E/WindowManager(738): at com.example.androidhive.AllProductsActivity.onCreate(AllProductsActivity.java:57)
02-14 20:06:14.213: E/WindowManager(738): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-14 20:06:14.213: E/WindowManager(738): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-14 20:06:14.213: E/WindowManager(738): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-14 20:06:14.213: E/WindowManager(738): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-14 20:06:14.213: E/WindowManager(738): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-14 20:06:14.213: E/WindowManager(738): at android.os.Handler.dispatchMessage(Handler.java:99)
02-14 20:06:14.213: E/WindowManager(738): at android.os.Looper.loop(Looper.java:123)
02-14 20:06:14.213: E/WindowManager(738): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-14 20:06:14.213: E/WindowManager(738): at java.lang.reflect.Method.invokeNative(Native Method)
02-14 20:06:14.213: E/WindowManager(738): at java.lang.reflect.Method.invoke(Method.java:507)
02-14 20:06:14.213: E/WindowManager(738): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-14 20:06:14.213: E/WindowManager(738): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-14 20:06:14.213: E/WindowManager(738): at dalvik.system.NativeStart.main(Native Method)
02-14 20:06:25.245: I/Process(738): Sending signal. PID: 738 SIG: 9
这是我的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(AllProductsActivity.this);
pDialog.setMessage("Loading products. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products 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("All Products: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_PRODUCTS);
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_PID);
String name = c.getString(TAG_NAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_PID, id);
map.put(TAG_NAME, name);
// adding HashList to ArrayList
productsList.add(map);
}
} else {
// no products found
// Launch Add New product Activity
Intent i = new Intent(getApplicationContext(),
NewProductActivity.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} 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(
AllProductsActivity.this, productsList,
R.layout.list_item, new String[] { TAG_PID,
TAG_NAME},
new int[] { R.id.pid, R.id.name });
// updating listview
setListAdapter(adapter);
}
});
}
}
在执行了ben所说的事情之后...错误的长度减少了.....现在只有这个错误-
02-14 20:39:06.094: E/JSON Parser(1182): Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
02-14 20:39:06.094: W/dalvikvm(1182): threadid=9: thread exiting with uncaught exception (group=0x40015560)
02-14 20:39:06.104: E/AndroidRuntime(1182): FATAL EXCEPTION: AsyncTask #1
02-14 20:39:06.104: E/AndroidRuntime(1182): java.lang.RuntimeException: An error occured while executing doInBackground()
02-14 20:39:06.104: E/AndroidRuntime(1182): at android.os.AsyncTask$3.done(AsyncTask.java:200)
02-14 20:39:06.104: E/AndroidRuntime(1182): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
02-14 20:39:06.104: E/AndroidRuntime(1182): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
02-14 20:39:06.104: E/AndroidRuntime(1182): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
02-14 20:39:06.104: E/AndroidRuntime(1182): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
02-14 20:39:06.104: E/AndroidRuntime(1182): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
02-14 20:39:06.104: E/AndroidRuntime(1182): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
02-14 20:39:06.104: E/AndroidRuntime(1182): at java.lang.Thread.run(Thread.java:1019)
02-14 20:39:06.104: E/AndroidRuntime(1182): Caused by: java.lang.NullPointerException
02-14 20:39:06.104: E/AndroidRuntime(1182): at com.example.androidhive.AllProductsActivity$LoadAllProducts.doInBackground(AllProductsActivity.java:137)
02-14 20:39:06.104: E/AndroidRuntime(1182): at com.example.androidhive.AllProductsActivity$LoadAllProducts.doInBackground(AllProductsActivity.java:1)
02-14 20:39:06.104: E/AndroidRuntime(1182): at android.os.AsyncTask$2.call(AsyncTask.java:185)
02-14 20:39:06.104: E/AndroidRuntime(1182): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
02-14 20:39:06.104: E/AndroidRuntime(1182): ... 4 more
最佳答案
这是主要错误:
Caused by: java.lang.NullPointerException at
com.example.androidhive.AllProductsActivity$LoadAllProducts.doInBackground(AllProductsActivity.java:130)
doInBackground方法的第130行发生的任何事情都将导致nullPointer。
请指出您的代码中哪一行是130。