Android - 从主要 activity 导航到平板电脑 activity
Android - navigate from main activity to tablets activity
在我的应用程序中,我必须从登录名 activity(使用 RelativeLayout 开发的正常 activity)导航到包含 Tab Host 的 activity。我为 Tab Host 创建了以下 xml:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linearLayout1"
android:orientation="vertical">
<TabWidget
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@android:id/tabs"
android:layout_marginBottom="-4dp">
</TabWidget>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/tabcontent"
android:layout_weight="1">
</FrameLayout>
</LinearLayout>
</TabHost>
这是控制标签主机的 class 的代码:
public class TabBarActivity extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabbar);
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, HomeActivity.class);
spec = tabHost.newTabSpec("Home").setIndicator("", getResources().getDrawable(R.drawable.home)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, HistoryActivity.class);
spec = tabHost.newTabSpec("History").setIndicator("", getResources().getDrawable(R.drawable.bell)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, SettingsActivity.class);
spec = tabHost.newTabSpec("Settings").setIndicator("", getResources().getDrawable(R.drawable.gear)).setContent(intent);
tabHost.addTab(spec);
}
}
我的登录 activity 将用户名和密码发送到 Web 服务,如果 Web 服务允许用户登录,它应该在底部显示带有 3 个不同 activity 的选项卡主机。如何使用 Tab Host 显示 activity?我试过这样做:
Intent i = new Intent(MainActivity.this, TabBarActivity.class);
startActivity(i);
但是它崩溃了。你能帮帮我吗?
更新
02-26 15:16:27.533 5504-5504/com.sample.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.sample.app, PID: 5504
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sample.app/com.sample.app.TabBarActivity}: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access0(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
at android.app.TabActivity.onContentChanged(TabActivity.java:131)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:362)
at android.app.Activity.setContentView(Activity.java:2010)
at armandotesta.it.atbrain.TabBarActivity.onCreate(TabBarActivity.java:15)
at android.app.Activity.performCreate(Activity.java:5426)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access0(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
替换
TabHost tabHost = getTabHost();
和
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
tabHost.setup();
您是否尝试过将 android:id="@android:id/tabhost"
添加到您的 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">
顺便说一句:TabHost
似乎已被弃用,您应该改用 Fragments。
在我的应用程序中,我必须从登录名 activity(使用 RelativeLayout 开发的正常 activity)导航到包含 Tab Host 的 activity。我为 Tab Host 创建了以下 xml:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linearLayout1"
android:orientation="vertical">
<TabWidget
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@android:id/tabs"
android:layout_marginBottom="-4dp">
</TabWidget>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/tabcontent"
android:layout_weight="1">
</FrameLayout>
</LinearLayout>
</TabHost>
这是控制标签主机的 class 的代码:
public class TabBarActivity extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabbar);
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, HomeActivity.class);
spec = tabHost.newTabSpec("Home").setIndicator("", getResources().getDrawable(R.drawable.home)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, HistoryActivity.class);
spec = tabHost.newTabSpec("History").setIndicator("", getResources().getDrawable(R.drawable.bell)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, SettingsActivity.class);
spec = tabHost.newTabSpec("Settings").setIndicator("", getResources().getDrawable(R.drawable.gear)).setContent(intent);
tabHost.addTab(spec);
}
}
我的登录 activity 将用户名和密码发送到 Web 服务,如果 Web 服务允许用户登录,它应该在底部显示带有 3 个不同 activity 的选项卡主机。如何使用 Tab Host 显示 activity?我试过这样做:
Intent i = new Intent(MainActivity.this, TabBarActivity.class);
startActivity(i);
但是它崩溃了。你能帮帮我吗?
更新
02-26 15:16:27.533 5504-5504/com.sample.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.sample.app, PID: 5504
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sample.app/com.sample.app.TabBarActivity}: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access0(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'
at android.app.TabActivity.onContentChanged(TabActivity.java:131)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:362)
at android.app.Activity.setContentView(Activity.java:2010)
at armandotesta.it.atbrain.TabBarActivity.onCreate(TabBarActivity.java:15)
at android.app.Activity.performCreate(Activity.java:5426)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access0(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
替换
TabHost tabHost = getTabHost();
和
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
tabHost.setup();
您是否尝试过将 android:id="@android:id/tabhost"
添加到您的 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">
顺便说一句:TabHost
似乎已被弃用,您应该改用 Fragments。