0

我目前正在尝试将我的 OSMDroid 地图活动转移到片段中。似乎一切都已正确设置,但我得到了这个奇怪的 NullPointerException ,我不确定是否能解决。

02-20 23:59:36.140: E/AndroidRuntime(970): FATAL EXCEPTION: main
02-20 23:59:36.140: E/AndroidRuntime(970): java.lang.NullPointerException
02-20 23:59:36.140: E/AndroidRuntime(970):  at maps.y.p.onResume(Unknown Source)
02-20 23:59:36.140: E/AndroidRuntime(970):  at com.google.android.gms.maps.internal.IMapFragmentDelegate$Stub.onTransact(IMapFragmentDelegate.java:115)
02-20 23:59:36.140: E/AndroidRuntime(970):  at android.os.Binder.transact(Binder.java:297)
02-20 23:59:36.140: E/AndroidRuntime(970):  at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onResume(Unknown Source)
02-20 23:59:36.140: E/AndroidRuntime(970):  at com.google.android.gms.maps.SupportMapFragment$a.onResume(Unknown Source)
02-20 23:59:36.140: E/AndroidRuntime(970):  at com.google.android.gms.internal.d$1.a(Unknown Source)
02-20 23:59:36.140: E/AndroidRuntime(970):  at com.google.android.gms.internal.d.a(Unknown Source)
02-20 23:59:36.140: E/AndroidRuntime(970):  at com.google.android.gms.internal.d.onResume(Unknown Source)
02-20 23:59:36.140: E/AndroidRuntime(970):  at com.google.android.gms.maps.SupportMapFragment.onResume(Unknown Source)
02-20 23:59:36.140: E/AndroidRuntime(970):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:918)
02-20 23:59:36.140: E/AndroidRuntime(970):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
02-20 23:59:36.140: E/AndroidRuntime(970):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:635)
02-20 23:59:36.140: E/AndroidRuntime(970):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1431)
02-20 23:59:36.140: E/AndroidRuntime(970):  at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:420)
02-20 23:59:36.140: E/AndroidRuntime(970):  at android.os.Handler.handleCallback(Handler.java:605)
02-20 23:59:36.140: E/AndroidRuntime(970):  at android.os.Handler.dispatchMessage(Handler.java:92)
02-20 23:59:36.140: E/AndroidRuntime(970):  at android.os.Looper.loop(Looper.java:137)
02-20 23:59:36.140: E/AndroidRuntime(970):  at android.app.ActivityThread.main(ActivityThread.java:4511)
02-20 23:59:36.140: E/AndroidRuntime(970):  at java.lang.reflect.Method.invokeNative(Native Method)
02-20 23:59:36.140: E/AndroidRuntime(970):  at java.lang.reflect.Method.invoke(Method.java:511)
02-20 23:59:36.140: E/AndroidRuntime(970):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
02-20 23:59:36.140: E/AndroidRuntime(970):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
02-20 23:59:36.140: E/AndroidRuntime(970):  at dalvik.system.NativeStart.main(Native Method)
02-20 23:59:36.225: E/android.os.Debug(29093): !@Dumpstate > dumpstate -k -t -n -z -d -o /data/log/dumpstate_app_error

这是我的片段类代码的一部分:

public View onCreateView(LayoutInflater inflator, ViewGroup container, Bundle savedInstanceState) {

    final View view = inflator.inflate(R.layout.offline_map_activity, container, false);


    myOpenMapView = (MapView) view.findViewById(R.id.openmapview);

    mResourceProxy = new DefaultResourceProxyImpl(getSherlockActivity().getApplicationContext());
    myOpenMapView.getTileProvider().clearTileCache();

    File aFile = new File(Environment.getExternalStorageDirectory().toString() + 
            "/test/offlineMap/" + tID + ".zip");
    IArchiveFile[] myArchives = new IArchiveFile[1];
    myArchives[0] = ArchiveFileFactory.getArchiveFile(aFile);
    MyTileSource myTiles = new MyTileSource( "" + tID, null, 12, 12, 256, ".png");
    MapTileModuleProviderBase[] myProviders = new MapTileModuleProviderBase[2];
    myProviders[0] = new MapTileFileArchiveProvider(new SimpleRegisterReceiver(getSherlockActivity().getApplicationContext()), 
            myTiles, myArchives);
    myProviders[1] =  new MapTileDownloader(TileSourceFactory.MAPNIK);
    MapTileProviderArray MyTileProvider = new MapTileProviderArray(myTiles,null, myProviders);
    TilesOverlay MyTilesOverlay = new TilesOverlay(MyTileProvider, getSherlockActivity().getApplicationContext());
    //codes continue...


    return view;
}

如果需要显示更多代码,请告诉我。谢谢!

4

2 回答 2

0

我想你可能只是错过了这个,它在我的有类似问题的情况下修复了它。

super.onCreateView(inflater, container, savedInstanceState);
于 2014-02-25T17:08:25.940 回答
0

我所做的是在代码而不是 xml 中创建 Osmdroid mapview。您可以尝试这样做,然后将视图添加到您希望 Osmdroid mapview 驻留的容器中。这样,您还可以更轻松地测试所有内容。

一个例子:

    setContentView(R.layout.main);
    RelativeLayout mapContainer = (RelativeLayout)
    findViewById(R.id.osm_map_container);

    final Harbor harbor = getHarbor();

    mMapView = new MapView(this, 256);  // you can supply other parameters here as well.
    LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    mMapView.setLayoutParams(params);
    mapContainer.addView(mMapView);
于 2013-02-21T08:04:35.533 回答