由于TabHost本身并没有被弃用,我只需要一个非常简单的解决方案来切换两个控件(可能是3个),我认为TabHost非常适合我的需求。但是我得到了一个非常神秘的错误......
这是我的代码
myTabHost = (TabHost) findViewById(android.R.id.tabhost);
myTabHost.setup();
TabHost.TabSpec tmpSpec = null;
TabContentFactory tmpContentFactory = null;
tmpSpec = myTabHost.newTabSpec("main_param_time");
tmpSpec.setIndicator("sometext");
// error happens here here
tmpSpec.setContent(R.id.charts_stickchart);
// rest of code here. e.g. addTab etc.
我得到的错误是
java.lang.RuntimeException:无法创建标签内容,因为可以 找不到ID为2131427359的视图
我的XML文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TabHost
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/tabhost"
>
<LinearLayout
android:id="@+id/LinearLayout01"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
>
</FrameLayout>
</LinearLayout>
</TabHost>
<cn.limc.androidcharts.view.StickChart
android:id="@+id/charts_stickchart"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:visibility="gone"
/>
<cn.limc.androidcharts.view.SpiderWebChart
android:id="@+id/charts_spiderwebchart"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:visibility="gone"
/>
</LinearLayout>
答案 0 :(得分:1)
将您的两个图表移动为<FrameLayout>
的子项,与this sample app中一样:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+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="match_parent">
<AnalogClock android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<Button android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="A semi-random button"
/>
</FrameLayout>
</LinearLayout>
</TabHost>