1

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();
Community
  • 1
  • 1
dblue
  • 21
  • 2
  • 6

2 Answers2

1

The id which you pass into ft.add(), must be a child of the layout specified in setContentView(). I think you have wrongly initialized your setContentView() of the onCreate(). Also make sure the ID which you are passing to FT is unique.

Raja Asthana
  • 2,080
  • 2
  • 19
  • 35
  • I used setContentView(R.layout.main); I updated my post with main.xml code – dblue Jan 24 '13 at 13:12
  • Raja, how can i make sure the ID which you are passing to FT is unique.? Is there any method like "IsExist" in ft? – dblue Jan 24 '13 at 13:15
  • @dblue you can't have same id for two element or control in a single layout.xml. If you have anything like that, your code will not compile. You just want to make sure that you are setting the correct layout. – Raja Asthana Jan 24 '13 at 13:23
  • Raja, again I am not understanding you. can u please more clear so your help become fruitful. what u mean by "same id" in sinle layout. I do not have duplicate id. can u post some code plz – dblue Jan 24 '13 at 13:44
  • Can you try using ft.beginTransaction().add(android.R.id.tabcontent, fragment, tab.getTag()).commit(); – Raja Asthana Jan 24 '13 at 13:51
  • Raja, i did it before posting itself. i am now trying mContainerId = Build.VERSION.SDK_INT >= 11 ? android.R.id.content : R.id.tabcontent; and then adding like ft.add(mContainerId, fragment, tab.getTag()); – dblue Jan 24 '13 at 13:58
  • No view found for 0x7f0d0015; in the code i have public static final int tabcontent=0x7f0d0015; – dblue Jan 24 '13 at 14:02
  • Check this thread http://stackoverflow.com/questions/7589032/illegalargumentexception-no-view-found-for-id-for-fragment-when-fast-switching – Raja Asthana Jan 24 '13 at 14:05
  • Thanks Raja, let me try put try catch and see. i will update you – dblue Jan 24 '13 at 14:08
  • Issue resolved. In config file max version was 15, i changed it to 16. – dblue Jan 27 '13 at 06:00
-1

There are helpful resources on developer.android.com which describe horizontal navigation:

Check Implementing Lateral Navigation for horizontal and also tab navigation.

There is a backward compatibility package from android which supports this functionality. Check android.support.v4.app

faceman
  • 1,318
  • 11
  • 20