Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
setContentView使listview中的Android应用程序崩溃_Android - Fatal编程技术网

setContentView使listview中的Android应用程序崩溃

setContentView使listview中的Android应用程序崩溃,android,Android,我有一个应用程序在加载时因为setContentView行而崩溃。 在你提问之前,我会在第15行注释掉setContentView行,这样我就得到了一个可用的应用程序。但当我把它放进去的时候,它就错了。现在使用的是2.1-update1 android 爪哇: package com.clark.listview; 导入android.app.ListActivity; 导入android.content.Intent; 导入android.os.Bundle; 导入android.view.v

我有一个应用程序在加载时因为setContentView行而崩溃。 在你提问之前,我会在第15行注释掉setContentView行,这样我就得到了一个可用的应用程序。但当我把它放进去的时候,它就错了。现在使用的是2.1-update1 android

爪哇:

package com.clark.listview;
导入android.app.ListActivity;
导入android.content.Intent;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
导入android.widget.Toast;
公共类listview扩展了ListActivity{
/**在首次创建活动时调用*/
创建公共空间(捆绑冰柱){
超级冰柱;
//setContentView(R.layout.main);
//创建一个字符串数组,将其放入我们的ListActivity
字符串[]名称=新字符串[]{“Linux”、“Windows7”、“Eclipse”、“Suse”,
“Ubuntu”、“Solaris”、“Android”、“iPhone”、“ClarkPhone”};
//使用您自己的布局并将适配器指向
//包含标签
this.setListAdapter(新的ArrayAdapter)(this,R.layout.rowlayout,
R.id.标签、名称);
}
@凌驾
受保护的void onListItemClick(列表视图l、视图v、整数位置、长id){
super.onListItemClick(左、右、位置、id);
//获取已单击的项目
对象o=this.getListAdapter().getItem(位置);
字符串关键字=o.toString();
Toast.makeText(您选择的“+”关键字,Toast.LENGTH\u LONG)
.show();
if(关键字.startsWith(“Windows7”)| |关键字.startsWith(“iPhone”)
||关键字.startsWith(“Solaris”)){
启动测试();
}
}
受保护的无效启动测试(){
意向i=新意向(此,home.class);
星触觉(i);
}
}
XML:


如果您的
活动
正在扩展
列表活动
类,请确保主布局文件必须包含一个
列表视图
元素,如下所示

<ListView android:id="@android:id/list" android:layout_height="wrap_content"
android:layout_width="match_parent" />

您的应用程序正在崩溃,因为您正在将一个适配器设置为ListView,而该适配器在布局文件中确实不存在


请参阅文档:

我遇到了类似的问题,因为我通过删除Java文件正在使用的元素来编辑XML文件。具体来说,

@InjectView(R.id.thing)

私人编辑


一旦我确保删除了调用的不存在的东西的所有实例,它就再次起作用了。只需将java与xml进行比较,确保没有不一致的地方。

您在布局中的
ListView
添加到了哪里?我同意@Vikas。从
ListActivity
的文档中:您自己的视图必须包含id为“@android:id/list”的ListView对象(如果它在代码中,则为list),谢谢您的帮助。它给了我我想要的东西。这显然是件奇怪的事情。如果我把android:id/list换成别的东西,它就不起作用了。谢谢这救了我一天:)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/main"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:cacheColorHint="#00000000"
    android:background="@drawable/bg">
</LinearLayout>
<ListView android:id="@android:id/list" android:layout_height="wrap_content"
android:layout_width="match_parent" />