I have developed a android application. I have an UI where I need a Fragment to be displayed in tabbed manner. For compatibility I am using "TabHelper" from CompatTab classes provided by android forum.
Application crashes in most of major mobile models.
APP_VERSION_CODE=30
ANDROID_VERSION=4.1.1
PHONE_MODEL=Galaxy Nexus
I also tried these links but did not get clues Android Fragment no view found for ID?
Fragment without a view crashes on configuration change
Here is my create method
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHelper tabHelper = getTabHelper();
//adding tabs....
}
I have OnTab Click event listener as follow
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);
}
}
My 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>
It works fine in Samsung Galaxy Tablet but fails in Galaxy S2,S3 Mobiles.
Here is the crash report
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}
I FEEL BELOW LINE OF CODE CREATES THE ISSUE
ft.add(android.R.id.tabcontent, fragment, tab.getTag());
I found below code in stack overflow but how to intergrate in my code?
fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ListeResultatFragment fragment = MyFragment.newInstance();
fragmentTransaction.add(android.R.id.tabcontent, fragment, "MyFragment");
fragmentTransaction.commit();