Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid 1.6 & Fragment & Tabhost
    text
    copied!<p>I'm working on upgrading an Android application (1.6 compatibility) which uses a <code>TabHost</code> to show 3 different tabs with nested activities.</p> <p>At the time I used the <code>ActivityGroup</code> trick to show nested activities in a tab but I'm very unhappy with this method since it's a real pain to handle some features.</p> <p>I heard about the Fragments API compatibility package for 1.6 and a <code>Fragment</code> looks perfect for what I want to do (show nested views / features within a tab with transition effects and stuff) but I can't make it work with a <code>TabHost</code> (It was meant to work with an <code>Action Bar</code> but it's not available in the compatibility package).</p> <p>Did any of you guys found a way to create such a structure in your applications?</p> <p>My error here is :</p> <blockquote> <pre><code>ERROR/AndroidRuntime(955): Caused by: java.lang.RuntimeException: Unable </code></pre> <p>to start activity ComponentInfo{com.XXX}: java.lang.IllegalArgumentException: No view found for id 0x1020011 for fragment MyFragment</p> </blockquote> <p><strong>CODE</strong></p> <p><strong>main.xml</strong></p> <p></p> <pre><code>&lt;TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="5dp" android:layout_weight="1" /&gt; &lt;TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0" /&gt; &lt;/LinearLayout&gt; &lt;/TabHost&gt; </code></pre> <p><strong>MainActivity.java</strong></p> <pre><code>public class MainActivity extends TabActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Resources res = getResources(); final TabHost tabs = getTabHost(); TabHost.TabSpec spec; Intent i; i = new Intent(this, MyActivity.class); spec = tabs.newTabSpec("MyActivity").setIndicator("MyActivity",res.getDrawable(R.drawable.tab)).setContent(i); tabs.addTab(spec); } } </code></pre> <p><strong>MyActivity.class</strong></p> <pre><code>public class MyActivity extends FragmentActivity { private static String TAG = "MyActivity"; private static FragmentManager fragmentManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); ListeResultatFragment fragment = MyFragment.newInstance(); fragmentTransaction.add(android.R.id.tabcontent, fragment, "MyFragment"); fragmentTransaction.commit(); } } </code></pre> <p><strong>MyFragment.java</strong></p> <pre><code>public class MyFragment extends Fragment { public static MyFragment newInstance() { MyFragment instance = new MyFragment(); return instance; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment, container, false); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload