我找到了这个https://github.com/abidk/android_masterdetail_tab_issue,它正是我的应用程序的基础,来自abidk的出色工作。它在平板电脑上执行完美,添加更多碎片非常容易,但在手机上却产生错误:可能是什么原因以及如何修复它?
D/dalvikvm(3695): Late-enabling CheckJNI
D/AndroidRuntime(3695): Shutting down VM
W/dalvikvm(3695): threadid=1: thread exiting with uncaught exception (group=0x41b192a0)
E/AndroidRuntime(3695): FATAL EXCEPTION: main
E/AndroidRuntime(3695): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.example.masterdetail_tabs/com.example.example.masterdetail_tabs.ItemListActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class fragment
E/AndroidRuntime(3695): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2092)
E/AndroidRuntime(3695): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
E/AndroidRuntime(3695): at android.app.ActivityThread.access$700(ActivityThread.java:134)
E/AndroidRuntime(3695): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
E/AndroidRuntime(3695): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(3695): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(3695): at android.app.ActivityThread.main(ActivityThread.java:4867)
E/AndroidRuntime(3695): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(3695): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(3695): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
E/AndroidRuntime(3695): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
E/AndroidRuntime(3695): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(3695): Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class fragment
E/AndroidRuntime(3695): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
E/AndroidRuntime(3695): at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
E/AndroidRuntime(3695): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
E/AndroidRuntime(3695): at com.example.example.masterdetail_tabs.Tab1Fragment.onCreateView(Tab1Fragment.java:18)
E/AndroidRuntime(3695): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
E/AndroidRuntime(3695): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
E/AndroidRuntime(3695): at android.app.BackStackRecord.run(BackStackRecord.java:635)
E/AndroidRuntime(3695): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1399)
E/AndroidRuntime(3695): at android.app.Activity.performStart(Activity.java:5056)
E/AndroidRuntime(3695): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2065)
E/AndroidRuntime(3695): ... 11 more
E/AndroidRuntime(3695): Caused by: java.lang.ClassCastException: com.example.example.masterdetail_tabs.ItemListFragment cannot be cast to android.support.v4.app.Fragment
E/AndroidRuntime(3695): at android.support.v4.app.Fragment.instantiate(Fragment.java:394)
E/AndroidRuntime(3695): at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
E/AndroidRuntime(3695): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
E/AndroidRuntime(3695): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
E/AndroidRuntime(3695): ... 20 more
I/Process(3695): Sending signal. PID: 3695 SIG: 9
答案 0 :(得分:1)
如果没有看到您的代码,确实很难确定,但看起来您为ItemListFragment或使用它的Activity继承或导入了错误的Fragment
。
您的logcat将异常显示为:
Caused by: java.lang.ClassCastException: com.example.example.masterdetail_tabs.ItemListFragment cannot be cast to android.support.v4.app.Fragment
检查您的用法和进口等
import android.support.v4.app.Fragment;
vs
import android.app.Fragment
还要注意Activity和Fragment的哪些变体可以使用哪些类型并与API的哪个版本兼容。您应该阅读并遵循official Android tutorial on Fragments以获取更多信息和正确的用法。