以下是片段类调用包含标记
的XMLpublic class FragmentAbout extends Fragment {
public FragmentAbout(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_about, container, false);
return rootView;
}
}
以下是 XML 文件,其中包含 标记
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FragmentTripPlan" >
<fragment
android:id="@+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btn_draw"
android:layout_alignParentLeft="true"
class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
这是日志跟踪
01-14 16:32:36.530: E/AndroidRuntime(28486): FATAL EXCEPTION: main
01-14 16:32:36.530: E/AndroidRuntime(28486): android.view.InflateException: Binary XML file line #11: Error inflating class fragment
01-14 16:32:36.530: E/AndroidRuntime(28486): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
01-14 16:32:36.530: E/AndroidRuntime(28486): at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
01-14 16:32:36.530: E/AndroidRuntime(28486): at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
01-14 16:32:36.530: E/AndroidRuntime(28486): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
01-14 16:32:36.530: E/AndroidRuntime(28486): at com.c4a.itravellipaph.FragmentAbout.onCreateView(FragmentAbout.java:19)
01-14 16:32:36.530: E/AndroidRuntime(28486): at android.app.Fragment.performCreateView(Fragment.java:1695)
01-14 16:32:36.530: E/AndroidRuntime(28486): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
01-14 16:32:36.530: E/AndroidRuntime(28486): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075)
01-14 16:32:36.530: E/AndroidRuntime(28486): at android.app.BackStackRecord.run(BackStackRecord.java:682)
01-14 16:32:36.530: E/AndroidRuntime(28486): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1455)
01-14 16:32:36.530: E/AndroidRuntime(28486): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
01-14 16:32:36.530: E/AndroidRuntime(28486): at android.os.Handler.handleCallback(Handler.java:725)
01-14 16:32:36.530: E/AndroidRuntime(28486): at android.os.Handler.dispatchMessage(Handler.java:92)
01-14 16:32:36.530: E/AndroidRuntime(28486): at android.os.Looper.loop(Looper.java:137)
01-14 16:32:36.530: E/AndroidRuntime(28486): at android.app.ActivityThread.main(ActivityThread.java:5279)
01-14 16:32:36.530: E/AndroidRuntime(28486): at java.lang.reflect.Method.invokeNative(Native Method)
01-14 16:32:36.530: E/AndroidRuntime(28486): at java.lang.reflect.Method.invoke(Method.java:511)
01-14 16:32:36.530: E/AndroidRuntime(28486): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
01-14 16:32:36.530: E/AndroidRuntime(28486): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
01-14 16:32:36.530: E/AndroidRuntime(28486): at dalvik.system.NativeStart.main(Native Method)
01-14 16:32:36.530: E/AndroidRuntime(28486): Caused by: java.lang.ClassCastException: com.google.android.gms.maps.SupportMapFragment cannot be cast to android.app.Fragment
01-14 16:32:36.530: E/AndroidRuntime(28486): at android.app.Fragment.instantiate(Fragment.java:585)
01-14 16:32:36.530: E/AndroidRuntime(28486): at android.app.Fragment.instantiate(Fragment.java:560)
01-14 16:32:36.530: E/AndroidRuntime(28486): at android.app.Activity.onCreateView(Activity.java:4864)
01-14 16:32:36.530: E/AndroidRuntime(28486): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:686)
01-14 16:32:36.530: E/AndroidRuntime(28486): ... 19 more
我该如何解决这个问题? 非常感谢提前! XML中的片段标记无法在片段类中工作
答案 0 :(得分:1)
你需要
import android.support.v4.app.Fragment;
您的活动必须延长FragmentActivity
。 FragmentAbout
是在活动xml即FrameLayout
此外,您应该使用MapView
。您可以找到类似的示例@
Android - android.view.InflateException: Binary XML file line #8: Error inflating class fragment