0

我在一个活动中有两个内部片段。

活动被称为:MainFragmentActivity (extends FragmentACtivity)

里面是这样的:

public static class FeaturedFrag extends ListFragment {
public static class TopFrag extends ListFragment {

我如何识别这里?

   <fragment
        android:id="@+id/featured_frag"
        android:layout_width="400dp"
        android:layout_height="match_parent"
        class="com.---.---.MainFragmentActvity$FeaturedFrag" />

    <fragment
        android:id="@+id/top_items_frag"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.---.---.MainFragmentActvity$TopItemFrag" />

将 XML 更改为建议,LogCat 下面。

12-05 00:27:35.639: E/AndroidRuntime(1243): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.---.---.MainFragmentActvit$MasterFrag: make sure class name exists, is public, and has an empty constructor that is public

我确实有一个具有该名称的类,一个空的构造函数并且它是公共的。

完整的 LogCat:

   12-05 00:37:38.090: E/AndroidRuntime(1384): FATAL EXCEPTION: main
12-05 00:37:38.090: E/AndroidRuntime(1384): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.---.---/com.---.---.master.MainFragmentActivity}: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
12-05 00:37:38.090: E/AndroidRuntime(1384):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
12-05 00:37:38.090: E/AndroidRuntime(1384):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-05 00:37:38.090: E/AndroidRuntime(1384):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-05 00:37:38.090: E/AndroidRuntime(1384):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-05 00:37:38.090: E/AndroidRuntime(1384):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-05 00:37:38.090: E/AndroidRuntime(1384):     at android.os.Looper.loop(Looper.java:137)
12-05 00:37:38.090: E/AndroidRuntime(1384):     at android.app.ActivityThread.main(ActivityThread.java:4745)
12-05 00:37:38.090: E/AndroidRuntime(1384):     at java.lang.reflect.Method.invokeNative(Native Method)
12-05 00:37:38.090: E/AndroidRuntime(1384):     at java.lang.reflect.Method.invoke(Method.java:511)
12-05 00:37:38.090: E/AndroidRuntime(1384):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-05 00:37:38.090: E/AndroidRuntime(1384):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-05 00:37:38.090: E/AndroidRuntime(1384):     at dalvik.system.NativeStart.main(Native Method)
12-05 00:37:38.090: E/AndroidRuntime(1384): Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
12-05 00:37:38.090: E/AndroidRuntime(1384):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
12-05 00:37:38.090: E/AndroidRuntime(1384):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
12-05 00:37:38.090: E/AndroidRuntime(1384):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
12-05 00:37:38.090: E/AndroidRuntime(1384):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
12-05 00:37:38.090: E/AndroidRuntime(1384):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
12-05 00:37:38.090: E/AndroidRuntime(1384):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
12-05 00:37:38.090: E/AndroidRuntime(1384):     at android.app.Activity.setContentView(Activity.java:1867)
12-05 00:37:38.090: E/AndroidRuntime(1384):     at com.---.---.master.MainFragmentActivity.onCreate(MainFragmentActivity.java:85)
12-05 00:37:38.090: E/AndroidRuntime(1384):     at android.app.Activity.performCreate(Activity.java:5008)
12-05 00:37:38.090: E/AndroidRuntime(1384):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-05 00:37:38.090: E/AndroidRuntime(1384):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
12-05 00:37:38.090: E/AndroidRuntime(1384):     ... 11 more
12-05 00:37:38.090: E/AndroidRuntime(1384): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.---.---.MainFragmentActivity$MasterFrag: make sure class name exists, is public, and has an empty constructor that is public
12-05 00:37:38.090: E/AndroidRuntime(1384):     at android.support.v4.app.Fragment.instantiate(Fragment.java:395)
12-05 00:37:38.090: E/AndroidRuntime(1384):     at android.support.v4.app.Fragment.instantiate(Fragment.java:363)
12-05 00:37:38.090: E/AndroidRuntime(1384):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:264)
12-05 00:37:38.090: E/AndroidRuntime(1384):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
12-05 00:37:38.090: E/AndroidRuntime(1384):     ... 21 more
12-05 00:37:38.090: E/AndroidRuntime(1384): Caused by: java.lang.ClassNotFoundException: com.kickinglettce.rate_this.MainFragmentActivity$MasterFrag
12-05 00:37:38.090: E/AndroidRuntime(1384):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
12-05 00:37:38.090: E/AndroidRuntime(1384):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
12-05 00:37:38.090: E/AndroidRuntime(1384):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
12-05 00:37:38.090: E/AndroidRuntime(1384):     at android.support.v4.app.Fragment.instantiate(Fragment.java:385)
12-05 00:37:38.090: E/AndroidRuntime(1384):     ... 24 more


Sample code from one of the inner fragments:

    public static class MasterFrag extends ListFragment {

       // fields

        public MasterFrag() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            return inflater.inflate(R.layout.mastercat, container, false);
        }
4

1 回答 1

1

校对所有代码。确保所有类名都相同,都在同一个包中,等等。否则 Android 不知道任何东西在哪里。

至于你原来的问题,格式是class = "package.Activity$Fragment"

于 2012-12-09T19:36:25.287 回答