0

我正在关注以下链接以在 anadroid 中创建片段

http://www.vogella.com/articles/Android/article.html#fragments

但它总是给出classnotfound异常。请指导我如何解决它?

日志猫:

07-25 14:45:22.511: E/AndroidRuntime(497): FATAL EXCEPTION: main
07-25 14:45:22.511: E/AndroidRuntime(497): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragment/com.example.fragment.FragmentProjectActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
07-25 14:45:22.511: E/AndroidRuntime(497):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-25 14:45:22.511: E/AndroidRuntime(497):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-25 14:45:22.511: E/AndroidRuntime(497):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-25 14:45:22.511: E/AndroidRuntime(497):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-25 14:45:22.511: E/AndroidRuntime(497):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-25 14:45:22.511: E/AndroidRuntime(497):  at android.os.Looper.loop(Looper.java:123)
07-25 14:45:22.511: E/AndroidRuntime(497):  at android.app.ActivityThread.main(ActivityThread.java:4627)
07-25 14:45:22.511: E/AndroidRuntime(497):  at java.lang.reflect.Method.invokeNative(Native Method)
07-25 14:45:22.511: E/AndroidRuntime(497):  at java.lang.reflect.Method.invoke(Method.java:521)
07-25 14:45:22.511: E/AndroidRuntime(497):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-25 14:45:22.511: E/AndroidRuntime(497):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-25 14:45:22.511: E/AndroidRuntime(497):  at dalvik.system.NativeStart.main(Native Method)
07-25 14:45:22.511: E/AndroidRuntime(497): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
07-25 14:45:22.511: E/AndroidRuntime(497):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
07-25 14:45:22.511: E/AndroidRuntime(497):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
07-25 14:45:22.511: E/AndroidRuntime(497):  at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
07-25 14:45:22.511: E/AndroidRuntime(497):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
07-25 14:45:22.511: E/AndroidRuntime(497):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
07-25 14:45:22.511: E/AndroidRuntime(497):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
07-25 14:45:22.511: E/AndroidRuntime(497):  at android.app.Activity.setContentView(Activity.java:1647)
07-25 14:45:22.511: E/AndroidRuntime(497):  at com.example.fragment.FragmentProjectActivity.onCreate(FragmentProjectActivity.java:11)
07-25 14:45:22.511: E/AndroidRuntime(497):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-25 14:45:22.511: E/AndroidRuntime(497):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-25 14:45:22.511: E/AndroidRuntime(497):  ... 11 more
07-25 14:45:22.511: E/AndroidRuntime(497): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.example.fragment-2.apk]
07-25 14:45:22.511: E/AndroidRuntime(497):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
07-25 14:45:22.511: E/AndroidRuntime(497):  at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
07-25 14:45:22.511: E/AndroidRuntime(497):  at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
07-25 14:45:22.511: E/AndroidRuntime(497):  at android.view.LayoutInflater.createView(LayoutInflater.java:466)
07-25 14:45:22.511: E/AndroidRuntime(497):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:544)
07-25 14:45:22.511: E/AndroidRuntime(497):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
07-25 14:45:22.511: E/AndroidRuntime(497):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
07-25 14:45:22.511: E/AndroidRuntime(497):  ... 20 more
4

2 回答 2

2

您是否使用高于 Lvl 11 的 API lvl?碎片从 Lvl 11 开始添加。

StackTrace 显示它尝试加载类 'android.view.fragment' 但 Fragment 类位于 'android.app.Fragment',所以确实有问题。

您的 .xml 布局文件可能有问题。或者您正在开发的 SDK 版本是旧版本。

为了更好的分析,请发布更多代码(.xml 文件和 .java 文件)

请参阅文档:http: //developer.android.com/reference/android/app/Fragment.html

于 2012-07-25T09:55:38.323 回答
2

感谢大家的宝贵回复..我的包名不正确..我更正并解决了..:)

于 2012-08-10T10:57:20.333 回答