Android ListActivity错误:“;不幸的是,您的应用程序已停止”;
这是我在主要活动中的代码Android ListActivity错误:“;不幸的是,您的应用程序已停止”;,android,android-studio,Android,Android Studio,这是我在主要活动中的代码 public class MainActivity extends ListActivity { private String [] mainScreenList = {"Portal","Settings","Help","About"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceSt
public class MainActivity extends ListActivity {
private String [] mainScreenList = {"Portal","Settings","Help","About"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> mainScreen = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_activated_1, android.R.id.text1,mainScreenList);
ListView lv = (ListView) findViewById(R.id.listView);
lv.setAdapter(mainScreen);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Object o = this.getListAdapter().getItem(position);
String pen = o.toString();
Toast.makeText(this, "You have chosen the pen: " + " " + pen, Toast.LENGTH_LONG).show();
//get selected items
/* if(position==0)
{
Intent i = new Intent(this, DataEntryActivity.class);
startActivity(i);
}else if(position==1)
{
Intent i = new Intent(this, DataEntryActivity.class);
startActivity(i);
}else if(position==2)
{
Intent i = new Intent(this, DataEntryActivity.class);
startActivity(i);
}else if(position==3)
{
Intent i = new Intent(this, DataEntryActivity.class);
startActivity(i);
}*/
}
}
public类MainActivity扩展了ListActivity{
私有字符串[]mainScreenList={“门户”、“设置”、“帮助”、“关于”};
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter mainScreen=新的ArrayAdapter(这个,android.R.layout.simple\u list\u item\u activated\u 1,android.R.id.text1,mainScreenList);
ListView lv=(ListView)findViewById(R.id.ListView);
低压设置适配器(主屏幕);
}
@凌驾
受保护的void onListItemClick(列表视图l、视图v、整数位置、长id){
super.onListItemClick(左、右、位置、id);
对象o=this.getListAdapter().getItem(位置);
String pen=o.toString();
Toast.makeText(这是“您选择了画笔:+”+画笔,Toast.LENGTH\u LONG.show();
//获取所选项目
/*如果(位置==0)
{
Intent i=新Intent(这个,DataEntryActivity.class);
星触觉(i);
}否则如果(位置==1)
{
Intent i=新Intent(这个,DataEntryActivity.class);
星触觉(i);
}否则如果(位置==2)
{
Intent i=新Intent(这个,DataEntryActivity.class);
星触觉(i);
}否则如果(位置==3)
{
Intent i=新Intent(这个,DataEntryActivity.class);
星触觉(i);
}*/
}
}
目前,我只想显示所选列表视图项的名称
这是XML代码
<?xml version="1.0" encoding="utf-8"?>
<ListView
android:id="@android:id/list"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.bernine.practicalsessions.MainActivity">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/listView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>
</ListView>
运行应用程序时,会显示“不幸的是,应用程序已停止。”检查
activity\u main.xml
layout文件。您已将ListView
包含在另一个ListView
中。这是无效的
将ListView
包含在任何容器中,如LinearLayout、RelativeLayout等。
对于ListView
,您应该使用id作为@android:id/list
。
查看此示例布局后
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@android:id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ListView>
</LinearLayout>
Listview内部的Listview..???请参阅此,以设置ListActivity的自定义布局
谢谢,我按照您提到的那样使用线性布局进行了安排,并将id放置在Listview容器中。但是,在这行代码的MainActivity.java中,ListView lv=(ListView)findViewById(R.id.list);-'无法解析符号列表'use ListView lv=(ListView)findViewById(android.R.id.list);非常感谢你的工作!对Android来说还是新鲜事:)