使用google maps api并将其显示在片段上时,我收到一个错误放大类片段。 它显示在滑动选项卡中,但这不应该是问题。 这是我的代码:

活动:

public class MapActivity extends Fragment implements OnMapReadyCallback  {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_map, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        SupportMapFragment mapView = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
        mapView.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap map) {
        LatLng sydney = new LatLng(-33.867, 151.206);

        map.setMyLocationEnabled(true);
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));

        map.addMarker(new MarkerOptions()
                .title("Sydney")
                .snippet("The most populous city in Australia.")
                .position(sydney));
    }
}

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.google.android.gms.maps.SupportMapFragment"/>

</RelativeLayout>

堆栈跟踪:

android.view.InflateException: Binary XML file line #7: Error inflating class fragment
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:770)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
        at com.example.MapActivity.onCreateView(MapActivity.java:15)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:953)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
        at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:488)
        at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
        at android.view.View.measure(View.java:18425)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5802)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1835)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:725)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:616)
        at android.view.View.measure(View.java:18425)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5802)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
        at android.view.View.measure(View.java:18425)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5802)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1835)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:725)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:616)
        at android.view.View.measure(View.java:18425)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5802)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
        at android.view.View.measure(View.java:18425)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5802)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1835)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:725)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:616)
        at android.view.View.measure(View.java:18425)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5802)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2881)
        at android.view.View.measure(View.java:18425)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2237)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1295)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1537)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1180)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6558)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
        at android.view.Choreographer.doCallbacks(Choreographer.java:590)
        at android.view.Choreographer.doFrame(Choreographer.java:560)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:145)
        at android.app.ActivityThread.main(ActivityThread.java:5834)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)

显示错误的那一行是以下内容: View view = inflater.inflate(R.layout.activity_map, container, false);

===============>>#1 票数:0

您的Activity类扩展了Fragment ,这意味着它是一个Fragment而不是Activity

您应该扩展FragmentActivity

同样,对于示例地图代码,您可以在Android Studio中创建MapActivity ,如下所示:

在此处输入图片说明

然后,您可以将地图设置代码放入Activity

编辑

如果您想制作地图fragment ,并使用tabs类的tabs进行切换。 我试过了您的代码,没问题,我想,您需要检查充气到的片段layoutname 并将getFragmentManager更改为getChildFragmentManager

编辑

对于您的代码,当我切换选项卡时肯定会遇到充气问题,并且我在这里尝试过代码,它可以正常工作,您可以更改一点代码吗? 如果是这样,您可以尝试一下。

我在这里上传了工作代码。 我在我的一个项目中做了map fragment 只需签出名为UpcomingFrament第三个选项卡,其中包括Map fragment

  ask by Broadwell translate from so

未解决问题?本站智能推荐: