我在启动代码时收到此消息,我尝试使用其他答案,但似乎没有对我有用,这是我的contents.xml的Xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/GamesURL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/GameName"
android:layout_alignLeft="@+id/GameName"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignRight="@+id/GameName"
android:layout_alignStart="@+id/GameName"
android:layout_below="@+id/GameName"
android:text="URL" />
<ImageView
android:id="@+id/GamesImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:contentDescription="GameDiscrip"
android:src="@drawable/destiny" />
<TextView
android:id="@+id/GameName"
style="GameName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/ivCard"
android:layout_marginLeft="22dp"
android:layout_marginStart="22dp"
android:layout_toEndOf="@+id/GamesImage"
android:layout_toRightOf="@+id/GamesImage"
android:text="Game_name" />
<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="multipleChoice"
android:drawSelectorOnTop="false" />
然后这是我的UseCustomAdapter.java代码:
public class UseCustomAdapter extends ListActivity {
ArrayList<Games> Games = new ArrayList<Games>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
registerForContextMenu(getListView());
setContentView(R.layout.scrollable_contents);
ScrollView scrollable_contents = (ScrollView) findViewById(R.id.scrollableContents);
getLayoutInflater().inflate(R.layout.contents, scrollable_contents);
Games.add(new Games("Destiny", "Xbox360",
"http://www.graingergames.co.uk/destiny"));
Games.add(new Games("Saints Row", "Xbox360",
"http://www.graingergames.co.uk/games/xbox-360/xt1490-saints-row-iv"));
Games.add(new Games("Batman", "Xbox360",
"http://www.graingergames.co.uk/games/xbox-360/xt1508-batman-arkham-origins"));
Games.add(new Games("BattleField Hardline", "Xbox360",
"http://www.graingergames.co.uk/games/xbox-360/xt1663-battlefield-hardline"));
Games.add(new Games("Dynasty Warriors 8", "Xbox360",
"http://www.graingergames.co.uk/games/xbox-360/xt1501-dynasty-warriors-8"));
Games.add(new Games("DragonBall Xenoverse", "Xbox360",
"http://www.graingergames.co.uk/games/xbox-360/xt1677-dragon-ball-xenoverse"));
ListView lv = getListView();
CustomAdapter adapter = new CustomAdapter(this, Games);
setListAdapter(adapter);
}
protected void onListItemClick(ListView lv, View v, int position, long id) {
super.onListItemClick(lv, v, position, id);
Games G = Games.get(position);
Toast.makeText(this, "User selected: " + G.getGamesTitle(),
Toast.LENGTH_SHORT).show();
}
我的代码运行正常,直到我开始添加滚动视图,但添加它不应返回此类异常,因为它与实际列表属性无关,如果您需要查看我的其他java或xml文件代码参考然后只是问谢谢。
更新
android:id="@id/android:list"
到android:id="@android:id/list
都无法解决错误。
这是我收到的针对android的idcat消息:id =“@ android:id / list:
03-15 09:40:18.050: E/AndroidRuntime(961): FATAL EXCEPTION: main
03-15 09:40:18.050: E/AndroidRuntime(961): Process: com.Lewis.c3383583task2, PID: 961
03-15 09:40:18.050: E/AndroidRuntime(961): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Lewis.c3383583task2/com.Lewis.c3383583task2.UseCustomAdapter}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
03-15 09:40:18.050: E/AndroidRuntime(961): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-15 09:40:18.050: E/AndroidRuntime(961): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-15 09:40:18.050: E/AndroidRuntime(961): at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-15 09:40:18.050: E/AndroidRuntime(961): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-15 09:40:18.050: E/AndroidRuntime(961): at android.os.Handler.dispatchMessage(Handler.java:102)
03-15 09:40:18.050: E/AndroidRuntime(961): at android.os.Looper.loop(Looper.java:136)
03-15 09:40:18.050: E/AndroidRuntime(961): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-15 09:40:18.050: E/AndroidRuntime(961): at java.lang.reflect.Method.invokeNative(Native Method)
03-15 09:40:18.050: E/AndroidRuntime(961): at java.lang.reflect.Method.invoke(Method.java:515)
03-15 09:40:18.050: E/AndroidRuntime(961): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-15 09:40:18.050: E/AndroidRuntime(961): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-15 09:40:18.050: E/AndroidRuntime(961): at dalvik.system.NativeStart.main(Native Method)
03-15 09:40:18.050: E/AndroidRuntime(961): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
03-15 09:40:18.050: E/AndroidRuntime(961): at android.app.ListActivity.onContentChanged(ListActivity.java:243)
03-15 09:40:18.050: E/AndroidRuntime(961): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:293)
03-15 09:40:18.050: E/AndroidRuntime(961): at android.app.Activity.setContentView(Activity.java:1929)
03-15 09:40:18.050: E/AndroidRuntime(961): at com.Lewis.c3383583task2.UseCustomAdapter.onCreate(UseCustomAdapter.java:31)
03-15 09:40:18.050: E/AndroidRuntime(961): at android.app.Activity.performCreate(Activity.java:5231)
03-15 09:40:18.050: E/AndroidRuntime(961): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-15 09:40:18.050: E/AndroidRuntime(961): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-15 09:40:18.050: E/AndroidRuntime(961): ... 11 more
答案 0 :(得分:1)
在xml中重命名ListView
的ID,就像这样...
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>