我正在开发一个需要2个标签的应用程序:“正常”和“硬” 我创建了两个标签。一切都很好
现在我想在普通标签中添加一个列表视图,但应用程序在启动后立即崩溃
这是我的项目
-Class
+ MainActivity.java
public class MainActivity extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabHost = getTabHost();
// Tab for Nomal
TabSpec nomallv = tabHost.newTabSpec("Normal");
// setting Title and Icon for the Tab
nomallv.setIndicator("Normal");
Intent nIntent = new Intent(this, NormalListActivity.class);
nomallv.setContent(nIntent);
// Tab for Hard
TabSpec hardlv = tabHost.newTabSpec("Hard");
// setting Title and Icon for the Tab
hardlv.setIndicator("Hard");
Intent hIntent = new Intent(this, HardListActivity.class);
hardlv.setContent(hIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(nomallv); // Adding tab
tabHost.addTab(hardlv); // Adding tab
}
}
+ NormalListActivity
public class NormalListActivity extends ListActivity {
String[] normallist = {"Home Town", "A trip to the country", "Story of Little Pea", "As long as you love me"
, "The way to success", "Pursuit of Happiness"};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.normallevellayout);
// TODO Auto-generated method stub
ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.normallevellayout, normallist);
ListView listView = (ListView)findViewById(R.id.listViewN);
listView.setAdapter(adapter);
}
}
+ HardListActiviity
public class HardListActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hardlevellayout);
// TODO Auto-generated method stub
}
}
- 这里布局:
+ activity_main:
<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: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>
+ hardlevellayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f7d1d7"
android:orientation="vertical" >
</LinearLayout>
+ normalevellayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#b7e2ec"
android:orientation="vertical" >
<ListView
android:id="@+id/listViewN"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout>
问题是标签中的列表视图 请帮忙!提前谢谢!
答案 0 :(得分:0)
将id更改为normallevellayout:
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
并且还为列表项扩展正确的布局。类似的东西:
ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.text, normallist);
ListView listView = (ListView)findViewById(android.R.id.list);
文字布局:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
替代解决方案: 保留text.xml和 将NormalListActivity更改为:
// setContentView(R.layout.normallevellayout);
// TODO Auto-generated method stub
ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.text, normallist);
ListView listView = getListView();//(ListView)findViewById(android.R.id.list);
listView.setAdapter(adapter);