我在我的類中有一個異步任務,它是關於解析JSON。但是當我執行代碼時,在LogCat中,我看到以下消息:FATAL EXCEPTION:AsyncTask#1使用異步任務時Android空指針異常
。
。
。
引起:java.lang.NullPointerException。
如果你想看看吧,這裏是我的代碼:
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new ReadJSON().execute(there is an address here);
textView = (TextView) findViewById(R.id.textView1);
}
//async task:
InputStream inputStream = null;
String result = null;
HttpResponse response;
BufferedReader reader;
JSONObject jObject;
String aJsonString1;
String aJsonString2;
public class ReadJSON extends AsyncTask<String, Void, String> {
protected String doInBackground(String... url) {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost(there is the same address here);
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
try {
response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpEntity entity = response.getEntity();
try {
inputStream = entity.getContent();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// json is UTF-8 by default i beleive
try {
reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
result = sb.toString();
try {
jObject = new JSONObject(result);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
aJsonString1 = jObject.getString("value");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
aJsonString2 = jObject.getString("timestamp");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return aJsonString1;
}
protected void onPostExecute(String result) {
textView.setText(aJsonString1);
}
}
你可以看到這裏的問題?任何人有想法?
在此先感謝
編輯:這裏是logcat的:
04-25 16:27:06.851: W/System.err(4697): at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-25 16:27:06.861: W/System.err(4697): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
04-25 16:27:06.861: W/System.err(4697): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
04-25 16:27:06.871: W/System.err(4697): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
04-25 16:27:06.871: W/System.err(4697): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
04-25 16:27:06.881: W/System.err(4697): at java.lang.Thread.run(Thread.java:856)
04-25 16:27:06.891: W/dalvikvm(4697): threadid=11: thread exiting with uncaught exception (group=0x40a71930)
04-25 16:27:06.911: E/AndroidRuntime(4697): FATAL EXCEPTION: AsyncTask #1
04-25 16:27:06.911: E/AndroidRuntime(4697): java.lang.RuntimeException: An error occured while executing doInBackground()
04-25 16:27:06.911: E/AndroidRuntime(4697): at android.os.AsyncTask$3.done(AsyncTask.java:299)
04-25 16:27:06.911: E/AndroidRuntime(4697): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
04-25 16:27:06.911: E/AndroidRuntime(4697): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
04-25 16:27:06.911: E/AndroidRuntime(4697): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
04-25 16:27:06.911: E/AndroidRuntime(4697): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
04-25 16:27:06.911: E/AndroidRuntime(4697): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
04-25 16:27:06.911: E/AndroidRuntime(4697): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
04-25 16:27:06.911: E/AndroidRuntime(4697): at java.lang.Thread.run(Thread.java:856)
04-25 16:27:06.911: E/AndroidRuntime(4697): Caused by: java.lang.NullPointerException
04-25 16:27:06.911: E/AndroidRuntime(4697): at com.example.ayasms.MainActivity$ReadJSON.doInBackground(MainActivity.java:189)
04-25 16:27:06.911: E/AndroidRuntime(4697): at com.example.ayasms.MainActivity$ReadJSON.doInBackground(MainActivity.java:1)
04-25 16:27:06.911: E/AndroidRuntime(4697): at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-25 16:27:06.911: E/AndroidRuntime(4697): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
04-25 16:27:06.911: E/AndroidRuntime(4697): ... 4 more
發佈完整堆棧跟蹤 – 2013-04-25 13:22:50
您不應該隱藏所有異常!如果拋出異常,就不會像沒有任何事情發生一樣繼續。 – BobTheBuilder 2013-04-25 13:25:47
您可以點擊日誌,選擇全部,點擊保存圖標,然後在這裏複製/粘貼日誌 – codeMagic 2013-04-25 13:26:48