在Android中实现碎片时出错

时间:2012-01-03 06:35:41

标签: android android-fragments

每次运行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

布局脊/ 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" />

布局/ 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>

3 个答案:

答案 0 :(得分:2)

检查一下。

确保类名存在,是公共的,并且具有公共

的空构造函数

Fragment的所有子类都必须包含一个公共空构造函数。框架通常会在需要时重新实例化一个片段类,特别是在状态恢复期间,并且需要能够找到此构造函数来实例化它。如果空构造函数不可用,则在状态恢复期间的某些情况下将发生运行时异常。

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

还要确保您的片段类是公开的

public class MyFragment extends Fragment

声明片段类时。此外,您不应该在类中有任何构造函数。所以请确保你没有。

答案 1 :(得分:1)

您需要将片段名称字段设为

<fragment
    android:name="my.package.name.classname"
    //other stuff
    />

而不是仅仅列出该类的名称。

答案 2 :(得分:0)

这是因为运行时在创建活动时无法找到TitlesFragment

caused by: java.lang.ClassNotFoundException: com.example.fragment$TitlesFragment