我在osmdroid库中遇到了一些问题。 我的应用程序包含一个带有3个片段的滑块,其中一个我试图添加打开的街道地图,但总有这个例外,我不知道如何解决它。我试图在他们的github网站https://github.com/osmdroid/osmdroid上实现这个库。如果我正在使用正常活动,它会起作用,但如果它也适用于片段,它会很棒。
非常感谢!
package ... .map;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import org.osmdroid.api.IMapController;
import org.osmdroid.config.Configuration;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import ... .R;
public class TabFragmentMap extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Configuration.getInstance().load(getContext(), PreferenceManager.getDefaultSharedPreferences(getContext()));
MapView map = (MapView) getActivity().findViewById(R.id.map);
map.setTileSource(TileSourceFactory.MAPNIK);
map.setBuiltInZoomControls(true);
map.setMultiTouchControls(true);
IMapController mapController = map.getController();
mapController.setZoom(9);
GeoPoint startPoint = new GeoPoint(48.8583, 2.2944);
mapController.setCenter(startPoint);
return inflater.inflate(R.layout.tab_fragment_map, container, false);
}
@Override
public void onResume() {
super.onResume();
Configuration.getInstance().load(getContext(), PreferenceManager.getDefaultSharedPreferences(getActivity()));
}
}
并且有布局xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<org.osmdroid.views.MapView
android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
引发了我的异常:
java.lang.NullPointerException: Attempt to invoke virtual method 'void org.osmdroid.views.MapView.setTileSource(org.osmdroid.tileprovider.tilesource.ITileSource)' on a null object reference
at ... .map.TabFragmentMap.onCreateView(TabFragmentMap.java:46)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2080)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1638)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:679)
at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:166)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1240)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1088)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1614)
at android.view.View.measure(View.java:18930)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:18930)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5976)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:18930)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5976)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1486)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:765)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:647)
at android.view.View.measure(View.java:18930)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5976)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18930)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5976)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1486)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:765)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:647)
at android.view.View.measure(View.java:18930)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5976)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2705)
at android.view.View.measure(View.java:18930)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2154)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1248)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1484)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1139)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6238)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:884)
at android.view.Choreographer.doCallbacks(Choreographer.java:696)
at android.view.Choreographer.doFrame(Choreographer.java:631)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:870)
at android.os.Handler.handleCallback(Handler.java:743)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5621)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
答案 0 :(得分:1)
您正在当前活动中搜索MapView
个实例(通过getActivity()
)。但是,视图不包含在您的活动布局中,而是包含在片段布局中。而是尝试搜索片段布局:
View view = inflater.inflate(R.layout.fragment_main, container, false);
MapView map = (MapView) view.findViewById(R.id.map);