在Android中实施分段时出现错误
作者:互联网
每次运行Fragment API示例应用程序时,都会遇到错误. Android开发人员指南http://developer.android.com/reference/android/app/Fragment.html引用了它.
我不知道它如何返回错误,为什么?请提出任何建议,让我得到正确的结果.
错误堆栈:
01-03 11:54:55.255: E/AndroidRuntime(363): FATAL EXCEPTION: main
01-03 11:54:55.255: E/AndroidRuntime(363): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragment/com.example.fragment.FragmentExampleActivity}: android.view.InflateException: Binary XML file line #5: Error inflating class fragment
01-03 11:54:55.255: E/AndroidRuntime(363): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1736)
01-03 11:54:55.255: E/AndroidRuntime(363): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
01-03 11:54:55.255: E/AndroidRuntime(363): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
01-03 11:54:55.255: E/AndroidRuntime(363): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
01-03 11:54:55.255: E/AndroidRuntime(363): at android.os.Handler.dispatchMessage(Handler.java:99)
01-03 11:54:55.255: E/AndroidRuntime(363): at android.os.Looper.loop(Looper.java:126)
01-03 11:54:55.255: E/AndroidRuntime(363): at android.app.ActivityThread.main(ActivityThread.java:3997)
01-03 11:54:55.255: E/AndroidRuntime(363): at java.lang.reflect.Method.invokeNative(Native Method)
01-03 11:54:55.255: E/AndroidRuntime(363): at java.lang.reflect.Method.invoke(Method.java:491)
01-03 11:54:55.255: E/AndroidRuntime(363): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
01-03 11:54:55.255: E/AndroidRuntime(363): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
01-03 11:54:55.255: E/AndroidRuntime(363): at dalvik.system.NativeStart.main(Native Method)
01-03 11:54:55.255: E/AndroidRuntime(363): Caused by: android.view.InflateException: Binary XML file line #5: Error inflating class fragment
01-03 11:54:55.255: E/AndroidRuntime(363): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:688)
01-03 11:54:55.255: E/AndroidRuntime(363): at android.view.LayoutInflater.rInflate(LayoutInflater.java:724)
01-03 11:54:55.255: E/AndroidRuntime(363): at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
01-03 11:54:55.255: E/AndroidRuntime(363): at android.view.LayoutInflater.inflate(LayoutInflater.java:391)
01-03 11:54:55.255: E/AndroidRuntime(363): at android.view.LayoutInflater.inflate(LayoutInflater.java:347)
01-03 11:54:55.255: E/AndroidRuntime(363): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:224)
01-03 11:54:55.255: E/AndroidRuntime(363): at android.app.Activity.setContentView(Activity.java:1777)
01-03 11:54:55.255: E/AndroidRuntime(363): at com.example.fragment.FragmentExampleActivity.onCreate(FragmentExampleActivity.java:11)
01-03 11:54:55.255: E/AndroidRuntime(363): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
01-03 11:54:55.255: E/AndroidRuntime(363): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700)
01-03 11:54:55.255: E/AndroidRuntime(363): ... 11 more
01-03 11:54:55.255: E/AndroidRuntime(363): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.fragment$TitlesFragment: make sure class name exists, is public, and has an empty constructor that is public
01-03 11:54:55.255: E/AndroidRuntime(363): at android.app.Fragment.instantiate(Fragment.java:500)
01-03 11:54:55.255: E/AndroidRuntime(363): at android.app.Fragment.instantiate(Fragment.java:468)
01-03 11:54:55.255: E/AndroidRuntime(363): at android.app.Activity.onCreateView(Activity.java:4081)
01-03 11:54:55.255: E/AndroidRuntime(363): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:664)
01-03 11:54:55.255: E/AndroidRuntime(363): ... 20 more
01-03 11:54:55.255: E/AndroidRuntime(363): Caused by: java.lang.ClassNotFoundException: com.example.fragment$TitlesFragment in loader dalvik.system.PathClassLoader[/data/app/com.example.fragment-1.apk]
01-03 11:54:55.255: E/AndroidRuntime(363): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:251)
01-03 11:54:55.255: E/AndroidRuntime(363): at java.lang.ClassLoader.loadClass(ClassLoader.java:548)
01-03 11:54:55.255: E/AndroidRuntime(363): at java.lang.ClassLoader.loadClass(ClassLoader.java:508)
01-03 11:54:55.255: E/AndroidRuntime(363): at android.app.Fragment.instantiate(Fragment.java:490)
01-03 11:54:55.255: E/AndroidRuntime(363): ... 23 more
layout-land / fragment_layout.xml:
<fragment class="com.example.fragment$TitlesFragment"
android:id="@+id/titles" android:layout_weight="1"
android:layout_width="0px" android:layout_height="match_parent" />
<FrameLayout android:id="@+id/details" android:layout_weight="1"
android:layout_width="0px" android:layout_height="match_parent"
android:background="?android:attr/detailsElementBackground" />
layout / fragment_layout.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<fragment class="com.example.fragment$TitlesFragment"
android:id="@+id/titles"
android:layout_width="match_parent" android:layout_height="match_parent" />
</FrameLayout>
解决方法:
您需要将片段名称字段设置为
<fragment
android:name="my.package.name.classname"
//other stuff
/>
而不是仅添加类的名称.
标签:android-fragments,android 来源: https://codeday.me/bug/20191201/2084414.html