Проблемы с ListView: android.content.res.Resources $ NotFoundException: идентификатор ресурса # 0x102000a, тип # 0x12 недопустим - PullRequest
0 голосов
/ 04 октября 2019

Я пытаюсь создать простую игру в жанре android-раннер с автомобилями, список автомобилей будет с активным фоном и списком в нем. Представление списка, имеющее значок и строку (значок транспортного средства и его название) Когда я пытаюсь реализовать представление списка, я получаю следующее исключение:

android.content.res.Resources$NotFoundException: Resource ID #0x102000a type #0x12 is not valid

Я искал ответы по stackoverflow и didnне нашел ни одного. Попытался изменить ресурсы, удалите setContentView ().

car_list.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp" >
    <ImageView
        android:id="@+id/logo"
        android:layout_width="100px"
        android:layout_height="100px"
        android:layout_marginLeft="5px"
        android:layout_marginRight="20px"
        android:layout_marginTop="5px"
        android:src="@drawable/bike" >
    </ImageView>
    <TextView
        android:layout_marginStart="100dp"
        android:id="@+id/label"
        android:text="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textSize="20sp"
        android:textAlignment="center">
    </TextView>
</LinearLayout>

activity_cars.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bgpick"
    >
    <TextView
        android:layout_marginTop="20dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:layout_width="350dp"
        android:layout_height="wrap_content"
        android:text="Pick a vehicle"
        android:textStyle="bold"
        android:textColor="@color/colorWhite"
        android:textSize="40dp"
        android:background="@drawable/btns"
        android:layout_marginBottom="40dp"
        />
    <ListView
        android:layout_gravity="center"
        android:id="@android:id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>

ListCarsActivity.java:

public class ListCarsActivity extends Activity {

    private CarsArrayAdapter carsArrayAdapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cars);
        final ListView listView=findViewById(android.R.id.list);
        String[] CARS = new String[] { "Sport","Tank","Space Ship","Mercedes" };
        final ArrayList<String> list=new ArrayList<String>();
        for(int i=0;i<CARS.length;++i)
            list.add(CARS[i]);

        final StableArrayAdapter adapter = new StableArrayAdapter (this,android.R.id.list,list);
        listView.setAdapter(adapter);
    }
    private class StableArrayAdapter extends ArrayAdapter<String> {
        HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

        public StableArrayAdapter(Context context, int textViewResourceId,
                                  List<String> objects) {
            super(context, textViewResourceId, objects);
            for (int i = 0; i < objects.size(); ++i) {
                mIdMap.put(objects.get(i), i);
            }
        }

        @Override
        public long getItemId(int position) {
            String item = getItem(position);
            return mIdMap.get(item);
        }
        @Override
        public boolean hasStableIds() {
            return true;
        }
    }
}

Понятия не имею, почему я получаю исключение, я сделал тройную уверенность в том, что все ресурсы расположены и имеют одинаковые имена. Спасибо за помощь.

1 Ответ

0 голосов
/ 04 октября 2019
setContentView(R.layout.activity_cars);

до

setContentView(R.layout.car_activity);

и когда я создаю идентификатор для просмотра

android:id="@android:id/list"

меняется с

и этот тоже

android:id="@+id/list"

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