1

我想从 xml 文件加载布局,我试过:

View view = inflater.inflate(R.layout.map_cameras_fragment, container, false);  

但它只是显示了这一点:

java.lang.NullPointerException at maps.z.r.onResume(Unknown Source)...

这是我的整个代码(我正在使用android 地图扩展):

public class MapCamerasFragment extends SupportMapFragment {

private GoogleMap mMap = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater,
                         ViewGroup container, 
                         Bundle savedInstanceState) 
{    
    View view = inflater.inflate(R.layout.map_cameras_fragment, container, false);  
    SupportMapFragment myFrag = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.mapChooseCameras);
    mMap = myFrag.getExtendedMap();

    MarkerGenerator.addMarkersInWorld(mMap);
    mMap.setClustering(new ClusteringSettings().iconDataProvider(new DemoIconProvider(getResources())).addMarkersDynamically(true));

    return view;
}
}

我究竟做错了什么?

日志猫

E/AndroidRuntime(3673): FATAL EXCEPTION: main
E/AndroidRuntime(3673): java.lang.NullPointerException
at maps.z.r.onResume(Unknown Source)
at com.google.android.gms.maps.internal.IMapFragmentDelegate$Stub.onTransact(IMapFragmentDelegate.java:115)
at android.os.Binder.transact(Binder.java:297)
at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onResume(Unknown Source)
at com.google.android.gms.maps.SupportMapFragment$a.onResume(Unknown Source)
at com.google.android.gms.internal.e$1.a(Unknown Source)
at com.google.android.gms.internal.e.a(Unknown Source)
at com.google.android.gms.internal.e.onResume(Unknown Source)
at com.google.android.gms.maps.SupportMapFragment.onResume(Unknown Source)
at android.support.v4.app.Fragment.performResume(Fragment.java:1503)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
at android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:807)
at android.support.v4.app.FragmentManagerImpl.startPendingDeferredFragments(FragmentManager.java:1112)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1461)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1012)
at android.support.v4.view.ViewPager.populate(ViewPager.java:881)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1366)
at android.view.View.measure(View.java:12723)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
at android.view.View.measure(View.java:12723)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at android.view.View.measure(View.java:12723)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
at android.view.View.measure(View.java:12723)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)
at android.view.View.measure(View.java:12723)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)

map_cameras_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <fragment
        android:id="@+id/mapChooseCameras"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        class="pl.mg6.android.maps.extensions.SupportMapFragment"/>
      <Button android:id="@+id/googlemaps_select_location"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:text="Select"/>
</RelativeLayout>

更新 1:

我一直在尝试这个

  ...
  View view = inflater.inflate(R.layout.map_cameras_fragment, container, false);       
  super.onCreateView(inflater, container, savedInstanceState);
  mMap = super.getExtendedMap();
  MarkerGenerator.addMarkersInWorld(mMap);
  mMap.setClustering(new ClusteringSettings().iconDataProvider(new DemoIconProvider(getResources())).addMarkersDynamically(true));  
  return view;
  ...

也不例外,但标记是不可见的。似乎有两种观点...您知道如何解决吗?

4

2 回答 2

1

我有同样的问题。我将片段更改为扩展片段而不是 SupportMapFragment。

于 2013-05-02T05:16:37.723 回答
1

您不能同时扩展SupportMapFragment和包含SupportMapFragment在您的片段中。嗯,你可以……但这有意义吗?

您也不应该将片段添加到 xml 中的片段。它起作用的唯一方法是在代码中创建子片段并用于添加getChildFragmentManager()到您的片段中。FragmentManagerSupportMapFragment

试试这个代码:SupportMapFragment inside fragment

于 2013-05-05T09:52:26.510 回答