我正在关注这个Google 演示,当我包含片段视图时,我的应用程序失败了。eclipse 模拟器会生成一个对话框,说明应用程序意外关闭。
该代码根本没有任何意义,也不是问题,因为当我在没有 activity_main.xml 中的片段的情况下运行它时,它工作正常。
代码:
导入android.app.Activity;导入android.os.Bundle;
公共类 MainActivity 扩展 Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
我无法复制 xml,但它是默认的空白“activity_main”xml 文件(带有 RelativeLayout 根),其中在 RelativeLayout 父级内部添加了片段,与演示中完全相同,只是没有重新复制的 xml 版本标记(这与已经存在的相同)。
当我删除片段时,它运行良好。
我在做什么使片段类无法使用?
更新:这里是 logcat
04-25 01:35:42.202: D/AndroidRuntime(441): Shutting down VM
04-25 01:35:42.202: W/dalvikvm(441): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-25 01:35:42.221: E/AndroidRuntime(441): FATAL EXCEPTION: main
04-25 01:35:42.221: E/AndroidRuntime(441): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sikkemamap/com.example.sikkemamap.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
04-25 01:35:42.221: E/AndroidRuntime(441): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
04-25 01:35:42.221: E/AndroidRuntime(441): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-25 01:35:42.221: E/AndroidRuntime(441): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-25 01:35:42.221: E/AndroidRuntime(441): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-25 01:35:42.221: E/AndroidRuntime(441): at android.os.Handler.dispatchMessage(Handler.java:99)
04-25 01:35:42.221: E/AndroidRuntime(441): at android.os.Looper.loop(Looper.java:123)
04-25 01:35:42.221: E/AndroidRuntime(441): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-25 01:35:42.221: E/AndroidRuntime(441): at java.lang.reflect.Method.invokeNative(Native Method)
04-25 01:35:42.221: E/AndroidRuntime(441): at java.lang.reflect.Method.invoke(Method.java:507)
04-25 01:35:42.221: E/AndroidRuntime(441): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-25 01:35:42.221: E/AndroidRuntime(441): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-25 01:35:42.221: E/AndroidRuntime(441): at dalvik.system.NativeStart.main(Native Method)
04-25 01:35:42.221: E/AndroidRuntime(441): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
04-25 01:35:42.221: E/AndroidRuntime(441): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
04-25 01:35:42.221: E/AndroidRuntime(441): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
04-25 01:35:42.221: E/AndroidRuntime(441): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
04-25 01:35:42.221: E/AndroidRuntime(441): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
04-25 01:35:42.221: E/AndroidRuntime(441): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
04-25 01:35:42.221: E/AndroidRuntime(441): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
04-25 01:35:42.221: E/AndroidRuntime(441): at android.app.Activity.setContentView(Activity.java:1657)
04-25 01:35:42.221: E/AndroidRuntime(441): at com.example.sikkemamap.MainActivity.onCreate(MainActivity.java:11)
04-25 01:35:42.221: E/AndroidRuntime(441): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-25 01:35:42.221: E/AndroidRuntime(441): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-25 01:35:42.221: E/AndroidRuntime(441): ... 11 more
04-25 01:35:42.221: E/AndroidRuntime(441): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.example.sikkemamap-2.apk]
04-25 01:35:42.221: E/AndroidRuntime(441): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
04-25 01:35:42.221: E/AndroidRuntime(441): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
04-25 01:35:42.221: E/AndroidRuntime(441): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
04-25 01:35:42.221: E/AndroidRuntime(441): at android.view.LayoutInflater.createView(LayoutInflater.java:471)
04-25 01:35:42.221: E/AndroidRuntime(441): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549)
04-25 01:35:42.221: E/AndroidRuntime(441): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
04-25 01:35:42.221: E/AndroidRuntime(441): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
04-25 01:35:42.221: E/AndroidRuntime(441): ... 20 more
04-25 01:35:55.342: I/Process(441): Sending signal. PID: 441 SIG: 9