0

我正在尝试将具有一堆单个活动的应用程序更改为滑动标签应用程序。我想要滑动标签中的 3 个活动。我使用了 eclipse 为我创建的谷歌示例。我似乎没有得到正确的方法。

我有以下(相关)代码(其他东西在eclipse中,在这里发布太多了):

Main.class 在启动时调用(它的最后一个内部类):

public static class DummySectionFragment extends Fragment {


public static final String ARG_SECTION_NUMBER = "section_number";

        // public DummySectionFragment() {
        // }

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

            View rootView = inflater.inflate(R.layout.activity_fragment,
                    container, false);

            return rootView;
        }
    }

活动片段.xml:

<fragment
    android:id="@+id/main"
    android:name="com.mikebdev.refuel.MainActivity"
    class="com.mikebdev.refuel.MainActivity"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1" />

<fragment
    android:id="@+id/stats"
    android:name="com.mikebdev.refuel.Statistics"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="2" />

<fragment
    android:id="@+id/refuel"
    android:name="com.mikebdev.refuel.ReFuel"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="3" />

活动都FragmentActivityandroid.support.v4.app.FragmentActivity

现在启动我的应用程序时,出现以下错误:

04-27 12:08:11.244: E/AndroidRuntime(28728): FATAL EXCEPTION: main
04-27 12:08:11.244: E/AndroidRuntime(28728): android.view.InflateException: Binary XML file line #8: Error inflating class fragment
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at com.mikebdev.refuel.Main$DummySectionFragment.onCreateView(Main.java:184)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.support.v4.view.ViewPager.populate(ViewPager.java:1011)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.support.v4.view.ViewPager.populate(ViewPager.java:880)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1374)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.view.View.measure(View.java:15518)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.view.View.measure(View.java:15518)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.view.View.measure(View.java:15518)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.view.View.measure(View.java:15518)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.view.Choreographer.doFrame(Choreographer.java:532)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.os.Handler.handleCallback(Handler.java:725)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.os.Looper.loop(Looper.java:137)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.app.ActivityThread.main(ActivityThread.java:5041)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at java.lang.reflect.Method.invokeNative(Native Method)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at java.lang.reflect.Method.invoke(Method.java:511)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at dalvik.system.NativeStart.main(Native Method)
04-27 12:08:11.244: E/AndroidRuntime(28728): Caused by: java.lang.ClassCastException: com.mikebdev.refuel.MainActivity cannot be cast to android.support.v4.app.Fragment
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.support.v4.app.Fragment.instantiate(Fragment.java:394)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
04-27 12:08:11.244: E/AndroidRuntime(28728):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
4

2 回答 2

1

您不能在 xml 布局文件中嵌入嵌套片段。您必须在运行时添加它们。

编辑:有无数关于实现带有片段的可滑动标签的教程,请查看它们,许多都是不言自明的。要点是:

  • 创建一个布局文件,其中包含选项卡和ViewPager
  • 创建将用作标签/页面的片段ViewPager
  • 创建一个FragmentPagerAdapter并将其分配给ViewPager
  • 将选项卡选择绑定到ViewPager页面。

您可以ViewPager直接在 the 中使用,Activity也可以使用 wrapper Fragment,不同之处在于您需要传递getChildFragmentManager()给 theFragmentPagerAdapter而不是getSupportFragmentManager()

于 2013-04-27T10:38:09.750 回答
0

过了一会儿,我能够通过在此处添加 switch case 语句来解决所有问题:

@Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a DummySectionFragment (defined as a static inner class
            // below) with the page number as its lone argument.

            Fragment fragment;
            switch (position) {
                case 0:
                    fragment = new StatisticsFragment();
                    break;
                case 1:
                    fragment = new MainActivityFragment();
                    break;
                case 2:
                    fragment = new DbListViewFragment();
                    break;
                default:
                    fragment  = new DummySectionFragment();
                    break;
            }
            return fragment;
        }

我复制了我的活动并将其转换为片段,如该视频中所述:

将 Activity 转换为 Fragment

现在一切似乎都正常了。

于 2013-04-27T16:03:03.150 回答