当我点击将json对象传递给下一个活动的微调器时,我得到一个空指针异常。
这是我的微调器代码
retrieveGames1BGTask = new RetrieveGames1BGTask();
retrieveGames1BGTask.execute();
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Intent intent = null;
switch (position) {
case 1:
intent = new Intent(view.getContext(), Basketball.class);
i.putExtra("json_data2", retrieveGames1BGTask.getResult());
startActivity(intent);
Toast.makeText(parent.getContext(),
parent.getItemAtPosition(position).toString() + " View Selected", Toast.LENGTH_LONG).show();
break;
}
}
在我的下一个活动中,我有这行代码
if (intent != null) {
json_string1 = intent.getStringExtra("json_data2");
}
logcat说这个
java.lang.NullPointerException at com.example.aaa.bbb.Games$1.onItemSelected(Games.java:216)
at android.widget.AdapterView.fireOnSelected(AdapterView.java:964) at android.widget.AdapterView.access$200(AdapterView.java:49)
at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:928)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
您尝试在任务完成之前绑定到列表。无法保证在绑定之前完成retrieveGames1BGTask。这就是为什么你应该覆盖AsyncTask中的onPostExecute方法,以确保在完成后台任务后绑定到微调器。