【问题标题】:SimpleAdapter with layout and SQLite crashing on startup Android带有布局和 SQLite 的 SimpleAdapter 在启动 Android 时崩溃
【发布时间】:2014-08-11 19:22:16
【问题描述】:

我正在尝试使用列表在 android 中制作应用程序!

我正在从我的数据库中获取我想要的数据,现在我试图放入一个列表视图中

我尝试按照教程进行操作,但效果很好。

现在我正在为自己构建一个应用程序,我的应用程序在启动时崩溃这是我的活动代码

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        this.setContentView(R.layout.activity_stats);
        setContentView(R.layout.activity_stats);
        ListView listContent = (ListView)findViewById(R.id.listShit);

        ArrayList<HashMap<String, String>> shitList =  dbTools.getAllShits();
          dbTools.getAllShits();


        if(shitList.size()!=0) {



            ListAdapter adapter = new SimpleAdapter( stats.this,shitList, R.id.shit_entry, new String[] { "shitId","earned"}, new int[] {R.id.shitId, R.id.earned});


            setListAdapter(adapter);
        }

日志猫

8-11 21:29:47.718  29617-29617/jmsbrk.crappapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: jmsbrk.crappapp, PID: 29617
    java.lang.RuntimeException: Unable to start activity ComponentInfo{jmsbrk.crappapp/jmsbrk.crappapp.stats}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2340)
            at android.app.ActivityThread.access$800(ActivityThread.java:157)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5293)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
            at android.app.ListActivity.onContentChanged(ListActivity.java:243)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:343)
            at android.app.Activity.setContentView(Activity.java:1973)
            at jmsbrk.crappapp.stats.onCreate(stats.java:36)
            at android.app.Activity.performCreate(Activity.java:5389)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2340)
            at android.app.ActivityThread.access$800(ActivityThread.java:157)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5293)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

  • 贴日志猫哥!!!
  • 感谢第一个问题的提示!
  • 使用 SimpleCursorAdapter,而不是 SimpleAdapter
  • 在尝试使用 SimpleCursorAdapter 时,我无法将其导入项目中
  • "你的内容必须有一个 id 属性为 'android.R.id.list' 的 ListView"

标签: android listadapter simpleadapter


【解决方案1】:

如果您要扩展 ListActivity,Android 默认会尝试搜索 ID 为“@android:id/list”的 Listview。确保您正在设置此项。

一个基本的例子是:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-06
    • 1970-01-01
    • 1970-01-01
    • 2020-09-19
    相关资源
    最近更新 更多