【问题标题】:Java Android exception, error content must have a ListView whose id is android.R.id.list. android:id="@android:id/list Does Not fix Issue [duplicate]Java Android异常,错误内容必须有一个id为android.R.id.list的ListView。 android:id="@android:id/list 无法解决问题 [重复]
【发布时间】:2015-05-17 15:21:45
【问题描述】:

我在启动代码时收到此消息,我尝试使用其他答案,但似乎没有一个对我有用,这是我的 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:id="@android:id/list 的 logcat 消息:

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

【问题讨论】:

  • 正如对重复问题的高度评价中所述,将android:id="@id/android:list" 更改为android:id="@android:id/list"
  • 无论我使用哪种方式都无法解决问题
  • 如果您将android:id="@android:id/list" 用于ListView,您将不会得到“内容必须有一个ID 为android.R.id.list 的ListView”。见this sample project。您的应用程序可能会遇到其他问题,但这些问题会导致 LogCat 中出现不同的错误消息和堆栈跟踪。
  • 你听说过谷歌搜索吗?
  • CommonsWare 您的建议有我的错误日志,是的,西蒙我已经尝试多次搜索但仍然没有查看

标签: java android xml listview r.java-file


【解决方案1】:

像这样在你的 xml 中重命名 ListView 的 id...

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

【讨论】:

  • 没有解决我在搜索其他问题时尝试过的问题
  • 还是同样的错误?或者错误现在改变了?如果没有尝试清理项目并重新安装
  • 我都收到同样的错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多