Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use two tabHost on Android
    primarykey
    data
    text
    <p>Below is my layout xml file: </p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" &gt; &lt;LinearLayout android:id="@+id/l1" android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_weight="1" android:gravity="right" android:orientation="vertical" &gt; &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" android:background="@drawable/ic_launcher"&gt; &lt;RelativeLayout android:id="@+id/RelativeLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" &gt; &lt;TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:background="#FF888888"/&gt; &lt;FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentLeft="true" android:layout_above="@android:id/tabs" /&gt; &lt;/RelativeLayout&gt; &lt;/TabHost&gt; &lt;/LinearLayout&gt; &lt;LinearLayout android:id="@+id/l2" android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_weight="0.5" android:background="#000000" android:orientation="vertical" &gt; &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" android:background="@drawable/ic_launcher"&gt; &lt;RelativeLayout android:id="@+id/RelativeLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" &gt; &lt;TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:background="#FF888888"/&gt; &lt;FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentLeft="true" android:layout_above="@android:id/tabs" /&gt; &lt;/RelativeLayout&gt; &lt;/TabHost&gt; &lt;/LinearLayout&gt; &lt;/LinearLayout&gt; </code></pre> <p>There 2 tabhost in layout l1 and l2.<br> And below code is mine to use tabhost: </p> <pre><code>TabWidget tw; TabHost tabHost; tabHost = getTabHost(); TabHost.TabSpec spec; Intent intent; Resources res = getResources(); tw = (TabWidget)findViewById(android.R.id.tabs); intent = new Intent().setClass(this, MyGroup.class); spec = tabHost.newTabSpec("A").setIndicator("A", res.getDrawable(R.drawable.ic_action_search)).setContent(intent); tabHost.addTab(spec); intent = new Intent().setClass(this, MyGroup.class); spec = tabHost.newTabSpec("B").setIndicator("B", res.getDrawable(R.drawable.ic_action_search)).setContent(intent); tabHost.addTab(spec); intent = new Intent().setClass(this, MyGroup.class); spec = tabHost.newTabSpec("C").setIndicator("C", res.getDrawable(R.drawable.ic_action_search)).setContent(intent); tabHost.addTab(spec); </code></pre> <p>But it can only control the tabhost in l1.<br> How can I do to control the tabhost in l2? </p> <p>I make some modify as below: </p> <pre><code> private TabWidget tw; private TabHost tabHost; private TabWidget tw1; private TabHost tabHost1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tabHost = getTabHost(); TabHost.TabSpec spec; Intent intent; Resources res = getResources(); tw = (TabWidget)findViewById(android.R.id.tabs); intent = new Intent().setClass(this, MyGroup.class); spec = tabHost.newTabSpec("A").setIndicator("A", res.getDrawable(R.drawable.ic_action_search)).setContent(intent); tabHost.addTab(spec); intent = new Intent().setClass(this, MyGroup.class); spec = tabHost.newTabSpec("B").setIndicator("B", res.getDrawable(R.drawable.ic_action_search)).setContent(intent); tabHost.addTab(spec); intent = new Intent().setClass(this, MyGroup.class); spec = tabHost.newTabSpec("C").setIndicator("C", res.getDrawable(R.drawable.ic_action_search)).setContent(intent); tabHost.addTab(spec); intent = new Intent().setClass(this, MyGroup.class); spec = tabHost.newTabSpec("A").setIndicator("A", res.getDrawable(R.drawable.ic_action_search)).setContent(intent); tabHost.addTab(spec); tabHost1 = (TabHost)findViewById(R.id.tabhost1); TabHost.TabSpec spec1; Intent intent1; tw1 = (TabWidget)findViewById(android.R.id.tabs); intent1 = new Intent().setClass(this, MyGroup.class); spec1 = tabHost1.newTabSpec("D").setIndicator("D", res.getDrawable(R.drawable.ic_action_search)).setContent(intent1); tabHost1.addTab(spec1); intent1 = new Intent().setClass(this, MyGroup.class); spec1 = tabHost1.newTabSpec("E").setIndicator("E", res.getDrawable(R.drawable.ic_action_search)).setContent(intent1); tabHost1.addTab(spec1); intent1 = new Intent().setClass(this, MyGroup.class); spec1 = tabHost1.newTabSpec("F").setIndicator("F", res.getDrawable(R.drawable.ic_action_search)).setContent(intent1); tabHost1.addTab(spec1); intent1 = new Intent().setClass(this, MyGroup.class); spec1 = tabHost1.newTabSpec("G").setIndicator("G", res.getDrawable(R.drawable.ic_action_search)).setContent(intent1); tabHost1.addTab(spec1); } </code></pre> <p>And error message is below: </p> <pre><code>08-14 16:26:26.960: D/AndroidRuntime(9763): Shutting down VM 08-14 16:26:26.960: W/dalvikvm(9763): threadid=1: thread exiting with uncaught exception (group=0x40a291f8) 08-14 16:26:26.960: E/AndroidRuntime(9763): FATAL EXCEPTION: main 08-14 16:26:26.960: E/AndroidRuntime(9763): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testtwolayout/com.example.testtwolayout.MainActivity}: java.lang.NullPointerException 08-14 16:26:26.960: E/AndroidRuntime(9763): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 08-14 16:26:26.960: E/AndroidRuntime(9763): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 08-14 16:26:26.960: E/AndroidRuntime(9763): at android.app.ActivityThread.access$600(ActivityThread.java:123) 08-14 16:26:26.960: E/AndroidRuntime(9763): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 08-14 16:26:26.960: E/AndroidRuntime(9763): at android.os.Handler.dispatchMessage(Handler.java:99) 08-14 16:26:26.960: E/AndroidRuntime(9763): at android.os.Looper.loop(Looper.java:137) 08-14 16:26:26.960: E/AndroidRuntime(9763): at android.app.ActivityThread.main(ActivityThread.java:4424) 08-14 16:26:26.960: E/AndroidRuntime(9763): at java.lang.reflect.Method.invokeNative(Native Method) 08-14 16:26:26.960: E/AndroidRuntime(9763): at java.lang.reflect.Method.invoke(Method.java:511) 08-14 16:26:26.960: E/AndroidRuntime(9763): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 08-14 16:26:26.960: E/AndroidRuntime(9763): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 08-14 16:26:26.960: E/AndroidRuntime(9763): at dalvik.system.NativeStart.main(Native Method) 08-14 16:26:26.960: E/AndroidRuntime(9763): Caused by: java.lang.NullPointerException 08-14 16:26:26.960: E/AndroidRuntime(9763): at android.widget.TabHost.addTab(TabHost.java:232) 08-14 16:26:26.960: E/AndroidRuntime(9763): at com.example.testtwolayout.MainActivity.onCreate(MainActivity.java:68) 08-14 16:26:26.960: E/AndroidRuntime(9763): at android.app.Activity.performCreate(Activity.java:4465) 08-14 16:26:26.960: E/AndroidRuntime(9763): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 08-14 16:26:26.960: E/AndroidRuntime(9763): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 08-14 16:26:26.960: E/AndroidRuntime(9763): ... 11 more </code></pre> <p>The line MainActivity.java:68 means line tabHost1.addTab(spec1);</p> <p>I have resolve by this <a href="https://stackoverflow.com/questions/3163884/android-tabhost-without-tabactivity">Android: TabHost without TabActivity</a> .<br> And simple test code as below: </p> <pre><code> private LocalActivityManager mlam; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mlam = new LocalActivityManager(this, false); mlam.dispatchCreate(savedInstanceState); tw = (TabWidget)findViewById(android.R.id.tabs); Resources res = getResources(); tabHost =(TabHost)findViewById(android.R.id.tabhost); tabHost.setup(mlam); TabHost.TabSpec spec; Intent intent; intent = new Intent().setClass(this, MyGroup.class); spec = tabHost.newTabSpec("A").setIndicator("A", res.getDrawable(R.drawable.ic_action_search)).setContent(intent); tabHost.addTab(spec); intent = new Intent().setClass(this, MyGroup.class); spec = tabHost.newTabSpec("B").setIndicator("B", res.getDrawable(R.drawable.ic_action_search)).setContent(intent); tabHost.addTab(spec); tabHost1 = (TabHost)findViewById(R.id.tabhost1); tabHost1.setup(mlam); TabHost.TabSpec spec1; Intent intent1; intent1 = new Intent().setClass(this, MyGroup.class); spec1 = tabHost1.newTabSpec("C").setIndicator("C", res.getDrawable(R.drawable.ic_action_search)).setContent(intent1); tabHost1.addTab(spec1); intent1 = new Intent().setClass(this, MyGroup.class); spec1 = tabHost1.newTabSpec("D").setIndicator("D", res.getDrawable(R.drawable.ic_action_search)).setContent(intent1); tabHost1.addTab(spec1); tabHost1.setCurrentTab(0); } </code></pre> <p>And MyGroup is below: </p> <pre><code>public class MyGroup extends ActivityGroup { public static ActivityGroup group; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); group = this; } @Override public void onBackPressed() { group.getLocalActivityManager().getCurrentActivity().onBackPressed(); } @Override protected void onResume() { super.onResume(); Intent it; Window w; View view; it = new Intent(this, AActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); w = group.getLocalActivityManager().startActivity("AActivity", it); view = w.getDecorView(); group.setContentView(view); } } </code></pre> <p>And AActivity as below only show a random number: </p> <pre><code>public class AActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.a); TextView tv = (TextView)findViewById(R.id.atv); int x = (int)(Math.random()*42+1); String xx = Integer.toString(x); Log.d("q", xx); tv.setText(xx); } } </code></pre> <p>But I find the left layout's tab 0 will not show number.<br> But the line Log.d("q", xx); is show.<br> How about this? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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