我正在尝试用汽车创建简单的Android跑步游戏,汽车列表将处于活动中,其中具有背景和listview。 带有图标和字符串的列表视图(车辆及其名称的图标) 当我尝试实现listview时,出现以下异常:
android.content.res.Resources$NotFoundException: Resource ID #0x102000a type #0x12 is not valid
我一直在寻找关于stackoverflow的答案,但没有找到答案。 试图更改资源,请删除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;
}
}
}
我不知道为什么要收到异常,我三重确保所有资源都位于同一名称下。 谢谢您的协助。
答案 0 :(得分:0)
wsUrl
到
static async getInitialProps({ req }) {
if (req) {
// WRONG: IN THIS CASE `wsUrl` value is undefiend
const { wsUrl } = req;
return { collections: req.user.collections, email: req.user.id, wsUrl };
}
// IN THIS CASE I AM GETTING THE `wsUrl` value
const { wsUrl } = await getWebSocketUrl();
return {
wsUrl
};
}
以及当我为视图创建ID
setContentView(R.layout.activity_cars);
更改
这也是
setContentView(R.layout.car_activity);