简体   繁体   中英

Expected resource type to be one of id, id

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
                )
        );
    }
}

The following line

ListView listView = (ListView) findViewById(R.layout.activity_criminals_list);

give a an error: Expected resource type to be one of id, id ...

In another activity when i use the findViewById method it works...

My api version is 23!

In this line you are passing the id of the whole layout:

ListView listView = (ListView) findViewById(R.id.activity_criminals_list);

And you try to init ListView with it's ID, whose name is different from the layout name:

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

ListView listView = (ListView) findViewById(R.id.list);

you are assigning id activity_criminals_list to LinearLayout in your XML and in java class you are trying to cast as ListView

change your layout to

<?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>

You're trying to pass a whole layout to listview which isn't possible. The solution is to change the R.layout.activity_criminals_list into a view id. Something like this R.id.myList and change your listview reference code to ListView listView = (ListView) findViewById(R.id.myList);

Complete solution add this to activity_criminals_list.xml

 <ListView
        android:id="@+id/myList"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

Then add this to CriminalsList.java

ListView listView = (ListView) findViewById(R.id.myList);

Please add:

<ListView
        android:id="@+id/activity_criminals_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

You forgot to add id to ListView, in the xml.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM