Android 将Listview放入TabHost时出现问题(作为视图,而不是活动)
我正在尝试将ListView作为选项卡之一插入tabhost。我在网上搜索了一下,发现了一些表示它们可以工作,但不适合我的实现(无论是在他们自己的项目中,还是在我的项目中) 我已经确定了以下内容,它可以工作(只要不崩溃),并且我在LogCat中没有收到任何错误,但是选项卡显示为空 我已经检查了提供列表(tooldisplay)的数组,并填充了它 我宁愿不必启动另一个活动来填充选项卡 setupdetail.xml:Android 将Listview放入TabHost时出现问题(作为视图,而不是活动),android,android-layout,android-widget,Android,Android Layout,Android Widget,我正在尝试将ListView作为选项卡之一插入tabhost。我在网上搜索了一下,发现了一些表示它们可以工作,但不适合我的实现(无论是在他们自己的项目中,还是在我的项目中) 我已经确定了以下内容,它可以工作(只要不崩溃),并且我在LogCat中没有收到任何错误,但是选项卡显示为空 我已经检查了提供列表(tooldisplay)的数组,并填充了它 我宁愿不必启动另一个活动来填充选项卡 setupdetail.xml: <?xml version="1.0" encoding="utf-8"?
<?xml version="1.0" encoding="utf-8"?>
<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" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:id="@+id/setupheader"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:text="This will be the setup header"
android:textSize="15dp" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:gravity="bottom" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" >
<!-- General Info Tab -->
<LinearLayout
android:id="@+id/general_tab"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
<!-- Tool Tab -->
<ListView
android:id="@+id/list1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:drawSelectorOnTop="false" />
</FrameLayout>
</LinearLayout>
</TabHost>
tabhost的活动(编辑以删除不相关的代码):
public类SetupDisplay扩展了TabActivity{
私有字符串[]工具显示=新字符串[20];
私有列表视图;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.setupdetailmain);
//设置选项卡
TabHost TabHost=getTabHost();
TabHost.TabSpec;
spec=tabHost.newTabSpec(“一般”).setIndicator(“一般”)
.setContent(R.id.general_选项卡);
tabHost.addTab(spec);
spec=tabHost.newTabSpec(“工具”).setIndicator(“工具”)
.setContent(R.id.list1);
tabHost.addTab(spec);
//将数据加载到tooldisplay[]
//获取视图和设置适配器(&S)
mListView=(ListView)findViewById(R.id.list1);
mListView.setAdapter(新阵列适配器(此,
android.R.layout.simple_list_item_1,tooldisplay));
}
}
在谷歌搜索了很多次之后,我找到了问题的根源。为稍后可能出现相同问题的任何人发布答案
我用错了控制器
因为我在布局中有不止一个textview,所以我需要为适配器指定布局和textview(注意,我的修复代码与我切换到自定义列表布局时最初发布的代码略有不同,但在遇到构造函数修复之前,问题仍然存在)
mListView.setAdapter(新的阵列适配器(此、,
android.R.layout.simple_list_item_1,tooldisplay));
应该是:
ListView mListView = (ListView)findViewById(R.id.list1);
mListView.setAdapter(new ArrayAdapter<String>(this,
R.layout.listlayout, R.id.ListItem1, tooldisplay));
ListView mListView=(ListView)findViewById(R.id.list1);
mListView.setAdapter(新阵列适配器(此,
R.layout.listlayout,R.id.ListItem1,tooldisplay);
您是否尝试用列表替换框架布局,并将列表上的id设置为@android:id/tabcontent?我为什么要这样做?根据文档,您必须具有框架布局才能包含选项卡。。。
mListView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, tooldisplay));
ListView mListView = (ListView)findViewById(R.id.list1);
mListView.setAdapter(new ArrayAdapter<String>(this,
R.layout.listlayout, R.id.ListItem1, tooldisplay));