我正在尝试将包含大量单个活动的应用程序更改为滑动选项卡应用程序。 我想在滑动选项卡中进行3项活动。我使用了为我创建的eclipse 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;
}
}
activity_fragment.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" />
活动全部延伸FragmentActivity
来自android.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)
答案 0 :(得分:1)
您不能在xml布局文件中嵌入嵌套片段。你必须在运行时添加它们。
编辑:有关无法实现带有碎片的可滑动标签的教程,请查看它们,许多都是自我解释的。要点是:
ViewPager
ViewPager
FragmentPagerAdapter
并将其分配给ViewPager
ViewPager
页。您可以直接在ViewPager
中使用Activity
,也可以使用包装Fragment
,区别在于您需要将getChildFragmentManager()
传递给{ {1}}代替FragmentPagerAdapter
。
答案 1 :(得分: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;
}
我制作了一份我的活动副本并将其转换为片段,如本视频所述:
Converting An Activity To A Fragment
现在一切似乎都有效。