クラスに非同期タスクがあり、JSON の解析に関するものです。しかし、コードを実行すると、LogCat に次のメッセージが表示されます: FATAL EXCEPTION: AsyncTask #1
.
.
.
原因: 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