我想從我的AsyncTask.OnPostExecute傳遞一些JSONObject數據到我的MainActivity.JSONCallBackComplete。Android的AsyncTask活動回調nullPointerException
我的問題是,當我試圖檢查我傳遞迴的JSONObject我得到以下execption:
10-14 18:40:25.820: E/AndroidRuntime(4153): FATAL EXCEPTION: main
10-14 18:40:25.820: E/AndroidRuntime(4153): java.lang.NullPointerException
10-14 18:40:25.820: E/AndroidRuntime(4153): at com.icerge.revivaltimes.MainActivity.JSONCallBackComplete(MainActivity.java:31)
10-14 18:40:25.820: E/AndroidRuntime(4153): at com.icerge.revivaltimes.JsonObj.onPostExecute(JsonObj.java:70)
10-14 18:40:25.820: E/AndroidRuntime(4153): at com.icerge.revivaltimes.JsonObj.onPostExecute(JsonObj.java:1)
10-14 18:40:25.820: E/AndroidRuntime(4153): at android.os.AsyncTask.finish(AsyncTask.java:417)
10-14 18:40:25.820: E/AndroidRuntime(4153): at android.os.AsyncTask.access$300(AsyncTask.java:127)
10-14 18:40:25.820: E/AndroidRuntime(4153): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
10-14 18:40:25.820: E/AndroidRuntime(4153): at android.os.Handler.dispatchMessage(Handler.java:99)
10-14 18:40:25.820: E/AndroidRuntime(4153): at android.os.Looper.loop(Looper.java:144)
10-14 18:40:25.820: E/AndroidRuntime(4153): at android.app.ActivityThread.main(ActivityThread.java:4937)
10-14 18:40:25.820: E/AndroidRuntime(4153): at java.lang.reflect.Method.invokeNative(Native Method)
10-14 18:40:25.820: E/AndroidRuntime(4153): at java.lang.reflect.Method.invoke(Method.java:521)
10-14 18:40:25.820: E/AndroidRuntime(4153): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
10-14 18:40:25.820: E/AndroidRuntime(4153): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-14 18:40:25.820: E/AndroidRuntime(4153): at dalvik.system.NativeStart.main(Native Method)
我的MainActivity類別如下:
public class MainActivity extends Activity{
private JSONObject jsonData = null;
public void JSONCallBackComplete(JSONObject jsonData){
this.jsonData = jsonData;
Log.e("TESTing in callback: ", jsonData.toString() );
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new JsonObj(this).execute("http://myserver/json");
}
}
而我的AsyncTask類是如如下:
public class JsonObj extends AsyncTask<String, Void, JSONObject>{
MainActivity activity;
int tid;
String term;
public JsonObj(MainActivity activity){
this.activity = activity;
// Log.e("TESTING: ", activity.getClass().toString());
}
@Override
protected JSONObject doInBackground(String... url) {
// TODO Auto-generated method stub
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost(url[0]);
JSONObject jsonObject = null;
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null){
sb.append(line + "\n");
}
result = sb.toString();
Log.e("JSON-Test [RESULT]: ", result);
jsonObject = new JSONObject(result);
} catch (Exception e) {
Log.e("JSON-Test [exception]: ", e.toString());
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return jsonObject;
}
@Override
protected void onPostExecute(JSONObject result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if(!result.equals(null)){
this.activity.JSONCallBackComplete(result);
}
}
}
我有一個構造函數在AsyncTask中指定MainActivity -Context並使用它通過JSONObject參數調用我的JSONCallBackComplete反饋函數。
但是,我沒有顯示我的JSON-DATA,而是得到了上面粘貼的異常。
在我看來,'MainActivity'的第31行是空的,你確定你的jsonData對象在返回時不是null? – tyczj
'onPostExecute()'中的'result''null'? – codeMagic
我已經在兩個相關位置執行了'.equals(NULL)'檢查,但是空指針異常仍然存在。我的代碼已更新以顯示onPostExecute函數中的檢查 – sisko