当使用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);
答案 0 :(得分:0)
您Activity
课程延伸Fragment
,这意味着它是Fragment
而不是Activity
。
你应该扩展FragmentActivity
。
同样对于示例地图代码,您可以在Android Studio中创建MapActivity
,如下所示:
然后,您可以将地图设置代码放在Activity
。
修改强>
如果您想制作fragment
地图,并使用tabs
之类的内容进行切换。我尝试了你的代码,没问题,我想,你需要查看你膨胀的name
片段layout
。并将getFragmentManager
更改为getChildFragmentManager
。
修改强>
对于你的代码,当我切换标签时肯定会遇到inflater问题,我尝试了代码here,它有效,你可以更改一些代码吗?如果是这样,你可以试试。
我上传了工作代码here。我在我的一个项目中完成了map fragment
。只需查看名为UpcomingFrament
的第三个标签,其中包含Map fragment