您的内容必须有一个 id 属性为“android.R.id.list”的 ListView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11050817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Your content must have a ListView whose id attribute is 'android.R.id.list'
提问by Isa Kuru
I have created an xml file like this:
我创建了一个这样的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list" >
</ListView>
and an activity:
和一项活动:
public class ExampleActivity extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainlist);
}
}
As you see, I have not done anything else. But I'am getting the error:
如您所见,我没有做任何其他事情。但我收到错误:
Your content must have a ListView whose id attribute is 'android.R.id.list'
您的内容必须有一个 id 属性为“android.R.id.list”的 ListView
Even though I have the android:id="@+id/list"
line in my xml.
即使android:id="@+id/list"
我的 xml 中有一行。
What is the problem?
问题是什么?
回答by Andro Selva
Rename the id of your ListView like this,
像这样重命名你的 ListView 的 id,
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
Since you are using ListActivity
your xml file must specify the keyword androidwhile mentioning to a ID.
由于您使用ListActivity
的是 xml 文件,因此必须在提及 ID 时指定关键字android。
If you need a custom ListView
then instead of Extending a ListActivity
, you have to simply extend an Activity
and should have the same id without the keyword android.
如果您需要自定义ListView
而不是扩展 a ListActivity
,您必须简单地扩展 anActivity
并且应该具有相同的 id ,而没有关键字android。
回答by Praveenkumar
You should have one listview in your mainlist.xml
file with id as @android:id/list
您的mainlist.xml
文件中应该有一个列表视图,其 id 为@android:id/list
<ListView
android:id="@android:id/list"
android:layout_height="wrap_content"
android:layout_height="fill_parent"/>
回答by Aamirkhan
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
this should solve your problem
这应该可以解决您的问题
回答by Kevin
Exact way I fixed this based on feedback above since I couldn't get it to work at first:
我根据上面的反馈修复了这个问题的确切方法,因为我一开始无法让它工作:
activity_main.xml:
活动_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/list"
>
</ListView>
MainActivity.java:
主活动.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addPreferencesFromResource(R.xml.preferences);
preferences.xml:
首选项.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:key="upgradecategory"
android:title="Upgrade" >
<Preference
android:key="download"
android:title="Get OnCall Pager Pro"
android:summary="Touch to download the Pro Version!" />
</PreferenceCategory>
</PreferenceScreen>
回答by Vicky
Inherit ActivityClass instead of ListActivityyou can resolve this problem.
Inherit ActivityClass 而不是ListActivity可以解决这个问题。
public class ExampleActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainlist);
}
}
回答by Makvin
<ListView android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:scrollbars="vertical"/>
回答by RudyF
One other thing that affected me: If you have multiple test devices, make sure you are making changes to the layout used by the device. In my case, I spent a while making changes to xmls in the "layout" directory until I discovered that my larger phone (which I switched to halfway through testing) was using xmls in the "layout-sw360dp" directory. Grrr!
影响我的另一件事:如果您有多个测试设备,请确保您正在更改设备使用的布局。就我而言,我花了一段时间对“layout”目录中的 xmls 进行更改,直到我发现我的较大手机(我在测试中途切换到)正在使用“layout-sw360dp”目录中的 xmls。呸!