我正在尝试将一些JSONObject数据从我的AsyncTask.OnPostExecute传递回我的MainActivity.JSONCallBackComplete。
我的问题是当我尝试检查传回的JSONObject时,我得到以下的执行:
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,而是获得了上面粘贴的异常。
答案 0 :(得分:1)
你有NullPointerException
个。我看到的第一个来自这里
if(!result.equals(null) ){
这应该是
if (result != null) {
您不应该使用.equals()
与null
进行比较,因为.equals()
通常用于检查2个对象的人类感知值是否相同,而不是内存地址。如果要将某些内容与null
内存地址进行比较,只需使用直接==
或!=
,其中Java将为您比较地址(您想要的内容)。