DoInBackground 工作正常,加载 locals var 没有问题,但是当 doinBackground return Local[] 得到 RunTimeException 而我不知道这一点。
下面我展示了我的SupportMapFragment
类实现和异常。
public class LocalMapFragment extends SupportMapFragment {
private Local[] locals;
private MapDrawer mapDrawer;
public LocalMapFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_map_local, container, false);
}
@Override
public void onActivityCreated(Bundle state) {
super.onActivityCreated(state);
new MapLocals(this).execute();
}
public void initMap(Local[] locals) {
this.mapDrawer = new MapDrawer(this.getMap());
this.locals = locals;
this.mapDrawer.showLocals(this.locals);
}
static class MapLocals extends AsyncTask<String, Integer, Local[]> {
LocalMapFragment localMapFragment;
public MapLocals(LocalMapFragment localMapFragment) {
this.localMapFragment = localMapFragment;
}
protected void onPreExecute() {
}
protected Local[] doInBackground(String... params) {
Local[] locals = null;
try {
LocalService localService = new LocalService(this.localMapFragment.getActivity(),
"/rest/localList.json");
locals = localService.getLocalsFromUserCity();
} catch (Exception ex) {
Log.e("APPERROR", ex.getMessage());
}
return locals;
}
protected void onProgressUpdate(Integer... values) {
}
protected void onPostExecute(Local[] locals) {
this.localMapFragment.initMap(locals);
/*
* int x = 0; x = x + 2;
*
* FragmentActivity activity = (FragmentActivity) this.context; if
* (activity != null && !activity.isFinishing()) {
*
* initMap(locals); }
*/
}
}
}
日志猫:
10-04 11:00:04.566: E/AndroidRuntime(9542): FATAL EXCEPTION: main
10-04 11:00:04.566: E/AndroidRuntime(9542): java.lang.RuntimeException: Unable to resume activity {com.myapp/com.myapp.HomeActivity}: java.lang.NullPointerException
10-04 11:00:04.566: E/AndroidRuntime(9542): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2732)
10-04 11:00:04.566: E/AndroidRuntime(9542): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2760)
10-04 11:00:04.566: E/AndroidRuntime(9542): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2216)
10-04 11:00:04.566: E/AndroidRuntime(9542): at android.app.ActivityThread.access$600(ActivityThread.java:149)
10-04 11:00:04.566: E/AndroidRuntime(9542): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300)
10-04 11:00:04.566: E/AndroidRuntime(9542): at android.os.Handler.dispatchMessage(Handler.java:99)
10-04 11:00:04.566: E/AndroidRuntime(9542): at android.os.Looper.loop(Looper.java:153)
10-04 11:00:04.566: E/AndroidRuntime(9542): at android.app.ActivityThread.main(ActivityThread.java:5086)
10-04 11:00:04.566: E/AndroidRuntime(9542): at java.lang.reflect.Method.invokeNative(Native Method)
10-04 11:00:04.566: E/AndroidRuntime(9542): at java.lang.reflect.Method.invoke(Method.java:511)
10-04 11:00:04.566: E/AndroidRuntime(9542): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
10-04 11:00:04.566: E/AndroidRuntime(9542): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
10-04 11:00:04.566: E/AndroidRuntime(9542): at dalvik.system.NativeStart.main(Native Method)
10-04 11:00:04.566: E/AndroidRuntime(9542): Caused by: java.lang.NullPointerException
10-04 11:00:04.566: E/AndroidRuntime(9542): at maps.ag.bg.b(Unknown Source)
10-04 11:00:04.566: E/AndroidRuntime(9542): at bob.onTransact(SourceFile:115)
10-04 11:00:04.566: E/AndroidRuntime(9542): at android.os.Binder.transact(Binder.java:326)
10-04 11:00:04.566: E/AndroidRuntime(9542): at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onResume(Unknown Source)
10-04 11:00:04.566: E/AndroidRuntime(9542): at com.google.android.gms.maps.SupportMapFragment$a.onResume(Unknown Source)
10-04 11:00:04.566: E/AndroidRuntime(9542): at com.google.android.gms.internal.bh$6.b(Unknown Source)
10-04 11:00:04.566: E/AndroidRuntime(9542): at com.google.android.gms.internal.bh.a(Unknown Source)
10-04 11:00:04.566: E/AndroidRuntime(9542): at com.google.android.gms.internal.bh.onResume(Unknown Source)
10-04 11:00:04.566: E/AndroidRuntime(9542): at com.google.android.gms.maps.SupportMapFragment.onResume(Unknown Source)
10-04 11:00:04.566: E/AndroidRuntime(9542): at android.support.v4.app.Fragment.performResume(Fragment.java:1521)
10-04 11:00:04.566: E/AndroidRuntime(9542): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:963)
10-04 11:00:04.566: E/AndroidRuntime(9542): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
10-04 11:00:04.566: E/AndroidRuntime(9542): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
10-04 11:00:04.566: E/AndroidRuntime(9542): at android.support.v4.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:1887)
10-04 11:00:04.566: E/AndroidRuntime(9542): at android.support.v4.app.FragmentActivity.onResumeFragments(FragmentActivity.java:460)
10-04 11:00:04.566: E/AndroidRuntime(9542): at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:449)
10-04 11:00:04.566: E/AndroidRuntime(9542): at android.app.Activity.performResume(Activity.java:5107)
10-04 11:00:04.566: E/AndroidRuntime(9542): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2718)
10-04 11:00:04.566: E/AndroidRuntime(9542): ... 12 more