Android的 - Tabhost在Activity类工作
-
25-09-2019 - |
题
OK,我一定是傻忽视的东西,因为我不能让我的tabhost以显示在我的活动类。我越来越可怕的力接近,当我尝试运行应用程序。如果我延长TabActivity,将工作,但我不能这样做, [见下面编辑],因为一旦我的代码从我的工程样机移动它的将是一个自定义类,从活动继承。
下面是XML布局:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</TabHost>
和这里是试图创建并显示TabHost类:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TabHost;
public class my_proto extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
Log.i(this.toString(), "OnCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i(this.toString(), "get tab host");
TabHost host = (TabHost)findViewById(R.id.tabhost);
host.setup();
Log.i(this.toString(), "add tabs");
host.addTab(host.newTabSpec("one")
.setIndicator("First Results")
.setContent(new Intent(this, FirstResultsListViewActivity.class)));
host.addTab(host.newTabSpec("two")
.setIndicator("Second Results")
.setContent(new Intent(this, SecondResultsListViewActivity.class)));
Log.i(this.toString(), "adjust tab size");
host.getTabWidget().getChildAt(0).getLayoutParams().height = 35;
host.getTabWidget().getChildAt(1).getLayoutParams().height = 35;
}// end onCreate
}// end class my_proto
这是我在日志中得到的错误。
11-17 16:49:51.271: INFO/com.my_proto.my_proto@43b80b50(1302): OnCreate
11-17 16:49:51.472: INFO/com.my_proto.my_proto@43b80b50(1302): get tab host
11-17 16:49:51.480: INFO/com.my_proto.my_proto@43b80b50(1302): add tabs
11-17 16:49:51.521: DEBUG/AndroidRuntime(1302): Shutting down VM
11-17 16:49:51.521: WARN/dalvikvm(1302): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
11-17 16:49:51.530: ERROR/AndroidRuntime(1302): Uncaught handler: thread main exiting due to uncaught exception
11-17 16:49:51.552: ERROR/AndroidRuntime(1302): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my_proto/com.my_proto.my_proto}: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
11-17 16:49:51.552: ERROR/AndroidRuntime(1302): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
11-17 16:49:51.552: ERROR/AndroidRuntime(1302): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
11-17 16:49:51.552: ERROR/AndroidRuntime(1302): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
11-17 16:49:51.552: ERROR/AndroidRuntime(1302): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
11-17 16:49:51.552: ERROR/AndroidRuntime(1302): at android.os.Handler.dispatchMessage(Handler.java:99)
11-17 16:49:51.552: ERROR/AndroidRuntime(1302): at android.os.Looper.loop(Looper.java:123)
11-17 16:49:51.552: ERROR/AndroidRuntime(1302): at android.app.ActivityThread.main(ActivityThread.java:4363)
11-17 16:49:51.552: ERROR/AndroidRuntime(1302): at java.lang.reflect.Method.invokeNative(Native Method)
11-17 16:49:51.552: ERROR/AndroidRuntime(1302): at java.lang.reflect.Method.invoke(Method.java:521)
11-17 16:49:51.552: ERROR/AndroidRuntime(1302): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
11-17 16:49:51.552: ERROR/AndroidRuntime(1302): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
11-17 16:49:51.552: ERROR/AndroidRuntime(1302): at dalvik.system.NativeStart.main(Native Method)
11-17 16:49:51.552: ERROR/AndroidRuntime(1302): Caused by: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
11-17 16:49:51.552: ERROR/AndroidRuntime(1302): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:646)
11-17 16:49:51.552: ERROR/AndroidRuntime(1302): at android.widget.TabHost.setCurrentTab(TabHost.java:320)
11-17 16:49:51.552: ERROR/AndroidRuntime(1302): at android.widget.TabHost.addTab(TabHost.java:213)
11-17 16:49:51.552: ERROR/AndroidRuntime(1302): at com.mc_proto.mc_proto.onCreate(my_proto.java:29)
11-17 16:49:51.552: ERROR/AndroidRuntime(1302): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-17 16:49:51.552: ERROR/AndroidRuntime(1302): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
11-17 16:49:51.552: ERROR/AndroidRuntime(1302): ... 11 more
它的抱怨没有要求设置,但我打电话吧。有设置的另一个版本,需要某种LocalActivityMananger的。我曾尝试更换呼叫设置下面的电话,但没有运气。
LocalActivityManager mLocalActivityManager = new LocalActivityManager(this, false);
host.setup(mLocalActivityManager);
上述[编辑]呼叫没有工作,所以我把它背出来。虽然我正在一个新的错误记录。
11-17 20:24:58.382: ERROR/AndroidRuntime(1432): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my_proto/com.my_proto.my_proto}: java.lang.IllegalStateException: Activities can't be added until the containing group has been created.
[编辑]基于一些评论下面,我意识到我需要我为什么不能从TabActivity继承清晰。一旦我有原型的工作,我需要这个端口到现有的应用程序的屏幕,从活动继承。我需要将TabHost加载到活动中的布局。这将主要是与状态图标一排的LinearLayout,跟着我与Tabhost在它的LinearLayout。我知道,如果我能得到这个工作,我可以移动到下一个阶段。谢谢!
---------------------------
| status icons |
---------------------------
| Tab, Tab, Tab |
| |
| |
| |
---------------------------
| status icons |
---------------------------
解决方案
确定我计算出来。显然,TabActivity
延伸ActivityGroup
,延伸Activity
。但在你的代码的类延伸Activity
这是不活动组。
因此,有两个选项:
1)如果你想在标签内容要活动,有你的类扩展ActivityGroup
(而不是Activity
)。然后你要建立呼叫应host.setup(getLocalActivityManager());
此方式您正在仿效TabActivity
源代码。
2)如果你可以有你的标签内容是视图(VS活动),使你的类从Activity
延伸,并保持你对setup()
电话。但对于setContent部分做这样的事:
host.addTab(host.newTabSpec("two")
.setIndicator("Second Results")
.setContent(new TabContentFactory() {
public View createTabContent(String tag) {
return new TextView(TestActivity.this);
}
}));
,然后定义里面createTabContent您的列表视图(这通常是我做什么 - 我更喜欢使用的意见,而不是活动的选项卡的内容)。
其他提示
如果你有,你需要使用Activity类为您Tabhost你可以试试它的工作原理如下的场景。
tabHost = (TabHost) findViewById(R.id.tabhost); //here tabHost will be your Tabhost
LocalActivityManager mLocalActivityManager = new LocalActivityManager(mActivity, false);
mLocalActivityManager.dispatchCreate(state); // state will be bundle your activity state which you get in onCreate
tabHost.setup(mLocalActivityManager);
有同样的问题:这个问题有助于机器人:TabHost而不TabActivity
显然,你只需要添加一行:
LocalActivityManager mLocalActivityManager = new LocalActivityManager(this, false);
mLocalActivityManager.dispatchCreate(savedInstanceState);
host.setup(mLocalActivityManager);
我android:id="@+id/tabhost"
不工作。我必须写android:id="@android:id/tabhost"
我改变的类
public class my_proto extends ActivityGroup
和改变的情况如下:
this.mHost = (TabHost) findViewById(R.id.tabhost);
this.mHost.setup(getLocalActivityManager());
这似乎确定我
更改类声明,其具有这样的:
public class my_proto extends Activity
到
public class my_proto extends TabActivity
什么托米说的是真的。你也应该看看教程这里 http://developer.android.com /resources/tutorials/views/hello-tabwidget.html