想在tabhost里面填入listview组件 求大神指教 谢谢啦
xml代码(activity_main.xml)
<?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">
<TabWidget
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentBottom="true"
>
</TabWidget>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/tabcontent">
<!-- 定义第一个标签页的内容 -->
<LinearLayout
android:id="@+id/tab01"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="活动1 - 2010/12/12"
android:textSize="11pt"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="活动2 - 2010/12/18"
android:textSize="11pt"
/>
</LinearLayout>
<!-- 定义第二个标签页的内容 -->
<LinearLayout
android:id="@+id/tab02"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<ListView
android:id="@+id/lv_very"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout>
</FrameLayout>
</TabHost>
list_item.xml代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/exercise_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
/>
<TextView
android:id="@+id/exercise_collect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
/>
<TextView
android:id="@+id/exercise_else"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
/>
<TextView
android:id="@+id/exercise_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:textSize="10sp" />
</LinearLayout>
</LinearLayout>
java代码
package com.example.tabhost_listview_demo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.TabActivity;
import android.view.LayoutInflater;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TabHost;
public class MainActivity extends TabActivity {
private ListView lv_v;
private List list;
private SimpleAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
list = new ArrayList<Map<String, Object>>();
lv_v = (ListView)findViewById(R.id.lv_very);
Map<String, Object> map = new HashMap<String, Object>();
map.put("item1_imageView1", R.drawable.ic_launcher);
map.put("item1_exercise_name", "活动名称");
map.put("item1_exercise_collect", "活动征集");
map.put("item1_exercise_else", "其他");
map.put("item1_exercise_details", "详情进入");
list.add(map);
adapter = new SimpleAdapter(this,
list,
R.id.lv_very,
new String [] {"item1_imageView1","item1_exercise_name",
"item1_exercise_collect","item1_exercise_else","item1_exercise_details"},
new int [] {R.id.imageView1,R.id.exercise_name,
R.id.exercise_collect,R.id.exercise_else,R.id.exercise_details});
// 06-27 06:09:07.835: E/AndroidRuntime(922): java.lang.RuntimeException:
// Unable to start activity ComponentInfo{com.example.tabhost_listview_demo/com.example.tabhost_listview_demo.MainActivity}: java.lang.NullPointerException
lv_v.setAdapter(adapter);
TabHost tabHost = getTabHost();
//TabHost
LayoutInflater.from(this).inflate(R.layout.activity_main,
tabHost.getTabContentView(),true);
//第一选项卡
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("活动").
setContent(R.id.tab01));
//第二选项卡
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("竞选活动").
setContent(R.id.tab02));
setContentView(tabHost);
}
}