activity_criminals_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_criminals_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.yourivanmill.csi.CriminalsList">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
CriminalsList.java
package com.yourivanmill.csi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class CriminalsList extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_criminals_list);
//final String[] criminals = getResources().getStringArray()
final String[] crimials = { "Charles Zwolsman", "Etienne Urka", "Cor Van Hout" };
ListView listView = (ListView) findViewById(R.id.activity_criminals_list);
listView.setAdapter(
new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
crimials
)
);
}
}
以下一行
ListView listView = (ListView) findViewById(R.layout.activity_criminals_list);
给出一个错误:预期的资源类型是id,id ...
之一在我使用findViewById方法的另一个活动中,它可以工作......
我的api版本是23!
答案 0 :(得分:3)
在这一行中,您传递的是整个布局的ID:
ListView listView = (ListView) findViewById(R.id.activity_criminals_list);
您尝试使用其ID来初始化ListView
,其名称与布局名称不同:
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
ListView listView = (ListView) findViewById(R.id.list);
答案 1 :(得分:1)
您要将 activity_criminals_list 分配给XML和您尝试投射为 ListView
的XML类中的 LinearLayout将布局更改为
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.yourivanmill.csi.CriminalsList">
<ListView
android:id="@+id/activity_criminals_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
答案 2 :(得分:0)
您尝试将整个布局传递给listview,这是不可能的。解决方案是将R.layout.activity_criminals_list
更改为视图ID。
类似这样R.id.myList
并将listview引用代码更改为ListView listView =(ListView)findViewById(R.id.myList);
完整的解决方案 将其添加到activity_criminals_list.xml
<ListView
android:id="@+id/myList"
android:layout_width="match_parent"
android:layout_height="match_parent" />
然后将其添加到CriminalsList.java
ListView listView = (ListView) findViewById(R.id.myList);
答案 3 :(得分:-1)
请添加:
<ListView
android:id="@+id/activity_criminals_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
您忘记在xml中为ListView添加id。