gpt4 book ai didi

android - 从 xml 加载 android 布局

转载 作者:行者123 更新时间:2023-11-29 21:44:46 25 4
gpt4 key购买 nike

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

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

但它只是显示了这个:

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

这是我的全部代码(我使用的是 android maps extensions ):

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;
}
}

我做错了什么?

LOGCAT:

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)

ma​​p_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;
...

没有异常(exception),但标记不可见。貌似有两种看法。。。你知道怎么解决吗?

最佳答案

您不能在 fragment 中同时扩展 SupportMapFragment 和包含 SupportMapFragment。好吧,你可以……但这有意义吗?

您也不应该将 fragment 添加到来自 xml 的 fragment 中。它工作的唯一方法是在代码中创建子 fragment 并使用 getChildFragmentManager() 获取 FragmentManager 以将 SupportMapFragment 添加到您的 fragment 。

试试这个代码:SupportMapFragment inside fragment .

关于android - 从 xml 加载 android 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16175241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com