我开发了一个安卓应用程序。我有一个 UI,我需要一个片段以选项卡的方式显示。为了兼容性,我使用了 android 论坛提供的 CompatTab 类中的“TabHelper”。
大多数主要移动模型中的应用程序崩溃。
APP_VERSION_CODE=30
ANDROID_VERSION=4.1.1
PHONE_MODEL=Galaxy Nexus
我也尝试了这些链接,但没有得到线索 Android Fragment no view found for ID?
这是我的创建方法
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHelper tabHelper = getTabHelper();
//adding tabs....
}
我有 OnTab Click 事件监听器如下
public static class InstantiatingTabListener implements CompatTabListener {
private final TabCompatActivity mActivity;
private final Class mClass;
public InstantiatingTabListener(TabCompatActivity activity, Class<? extends Fragment> cls) {
mActivity = activity;
mClass = cls;
}
/* The following are each of the ActionBar.TabListener callbacks */
@Override
public void onTabSelected(CompatTab tab, FragmentTransaction ft) {
// Check if the fragment is already initialized
Fragment fragment = tab.getFragment();
if (fragment == null) {
// If not, instantiate and add it to the activity
fragment = Fragment.instantiate(mActivity, mClass.getName());
tab.setFragment(fragment);
ft.add(android.R.id.tabcontent, fragment, tab.getTag());
} else {
// If it exists, simply attach it in order to show it
ft.attach(fragment);
}
}
我的 XML TabHost
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
它在三星 Galaxy 平板电脑中运行良好,但在 Galaxy S2、S3 手机中失败。
这是崩溃报告
APP_VERSION_CODE=30
ANDROID_VERSION=4.1.1
PHONE_MODEL=Galaxy Nexus
CUSTOM_DATA=
STACK_TRACE=java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.ishare.guide/com.ishare.guide.GuideBrowserActivity}:
java.lang.IllegalArgumentException: No view found for id 0x1020011 for fragment
GuideMenuFragment{41ee9418 #0 id=0x1020011 menu}
我感觉低于代码行造成了问题
ft.add(android.R.id.tabcontent, fragment, tab.getTag());
我在堆栈溢出中找到了下面的代码,但是如何在我的代码中集成?
fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ListeResultatFragment fragment = MyFragment.newInstance();
fragmentTransaction.add(android.R.id.tabcontent, fragment, "MyFragment");
fragmentTransaction.commit();