【问题标题】:What sort of xml is the SimpleCursorAdapter expecting?SimpleCursorAdapter 期望什么样的 xml?
【发布时间】:2012-02-05 09:57:19
【问题描述】:

我在 AlertDialog 中显示联系人时遇到问题,我想我找到了 问题,但我仍然很困惑。我的代码中相关的可疑部分是:

SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this,
        android.R.layout.simple_list_item_1, mContacts,
        new String[] { ContactsContract.Contacts.DISPLAY_NAME },
        new int[] { android.R.id.text1 });

...现在“simple_list_item_1”被编译器接受(应用程序在模拟器中运行),但是当 我尝试运行这个 ListActivity,它会爆炸。

因此,在阅读了其他地方有关如何完成此任务的信息后,我的眼睛亮了起来 “simple_list_item_1” - 那在哪里?我没有创建任何 simple_list_item_1.xml 文件,并且它在我的 \res\layout 文件夹中不存在。所以我(在 Eclipse 中)右键单击“simple_list_item_1”并选择“打开声明”,它调用了一个错误消息,与我在调试透视图中看到的关于“类文件编辑器 | 找到源代码 | JAR 文件的消息非常相似或完全一样。 ...\android.jar 没有源附件。”

所以...假设这是问题所在(在我的 \res\layout 文件夹中没有“simple_list_item_1.xml”),那么该文件中的预期内容(我假设这只是我想要的 xml 定义)每个要显示的联系人)?

==============

这是一些 LogCat 数据:

这是 LogCat 中的最后几行(一旦到达 ListActivity (ContactsActivity),它似乎就死机/挂起:

com.aXX3AndSpace.KeepInTouch/.KeepInTouchActivity: +5s368ms
01-08 21:54:34.020: I/ActivityManager(61): Starting: Intent { cmp=com.aXX3AndSpace.KeepInTouch/.ContactsActivity } from pid 385

...这里是一次尝试运行中的所有错误消息(打开应用程序,尝试调用 ListActivity)。请注意末尾的“这应该永远不会发生”消息:

01-08 21:53:36.592: E/Zygote(33): setreuid() failed. errno: 2
01-08 21:53:46.423: E/Zygote(33): setreuid() failed. errno: 17
01-08 21:53:47.842: E/BatteryService(61): usbOnlinePath not found
01-08 21:53:47.842: E/BatteryService(61): batteryVoltagePath not found
01-08 21:53:47.842: E/BatteryService(61): batteryTemperaturePath not found
01-08 21:53:47.862: E/SurfaceFlinger(61): Couldn't open /sys/power/wait_for_fb_sleep or /sys/power/wait_for_fb_wake
01-08 21:53:48.082: E/SensorService(61): couldn't open device for module sensors (Invalid argument)
01-08 21:53:53.722: E/System(61): Failure starting core service
01-08 21:53:53.722: E/System(61): java.lang.SecurityException
01-08 21:53:53.722: E/System(61):   at android.os.BinderProxy.transact(Native Method)
01-08 21:53:53.722: E/System(61):   at android.os.ServiceManagerProxy.addService(ServiceManagerNative.java:146)
01-08 21:53:53.722: E/System(61):   at android.os.ServiceManager.addService(ServiceManager.java:72)
01-08 21:53:53.722: E/System(61):   at com.android.server.ServerThread.run(SystemServer.java:207)
01-08 21:53:53.742: E/EventHub(61): could not get driver version for /dev/input/mouse0, Not a typewriter
01-08 21:53:53.742: E/EventHub(61): could not get driver version for /dev/input/mice, Not a typewriter
01-08 21:53:54.032: E/SoundPool(61): error loading /system/media/audio/ui/Effect_Tick.ogg
01-08 21:53:54.032: E/SoundPool(61): error loading /system/media/audio/ui/KeypressStandard.ogg
01-08 21:53:54.032: E/SoundPool(61): error loading /system/media/audio/ui/KeypressSpacebar.ogg
01-08 21:53:54.042: E/SoundPool(61): error loading /system/media/audio/ui/KeypressDelete.ogg
01-08 21:53:54.042: E/SoundPool(61): error loading /system/media/audio/ui/KeypressReturn.ogg
01-08 21:53:54.703: E/ThrottleService(61): Could not open GPS configuration file /etc/gps.conf
01-08 21:53:57.172: E/logwrapper(158): executing /system/bin/tc failed: No such file or directory
01-08 21:53:57.242: E/logwrapper(159): executing /system/bin/tc failed: No such file or directory
01-08 21:53:57.282: E/logwrapper(160): executing /system/bin/tc failed: No such file or directory
01-08 21:54:00.074: E/jdwp(174): Failed sending reply to debugger: Broken pipe
01-08 21:54:00.712: E/Database(116): sqlite_config failed error_code = 21. THIS SHOULD NEVER occur.

【问题讨论】:

  • 如果您的光标投影不包含 _ID 列,可能会发生错误?它在场吗?请发布崩溃日志
  • 如何保存崩溃日志?我看到一个“保存选定的行”选项,但肯定有一种方法可以一次保存它们(全选),但我没有看到它......
  • 是的,我正在访问 _id 列: String[] projection = new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME }; ... int selectedId = mContacts.getInt(0); // _ID 列 ... ContactsContract.Data.CONTACT_ID + " = " + selectedId, null, null); ... ContactsContract.Data.CONTACT_ID + " = " + selectedId, null, null); ... ContactsContract.Data.CONTACT_ID + " = " + selectedId, null, null);
  • 单击任何日志行并按 Ctrl+A 应该选择所有日志
  • 好的,谢谢;奇怪的是在 LogCat 中右键单击时没有显示。

标签: android android-layout android-contacts android-activity android-alertdialog


【解决方案1】:

simple_list_item_1 是 SDK 中定义的标准布局,它基本上只是一个 ID 为“android:id/text1”的 TextView,这是您在视图标识符中提供的内容。

你有崩溃的堆栈跟踪吗?因为这很可能不是因为缺少 simple_list_item_1 布局。

【讨论】:

  • 当我选择“将选定项目导出到文本文件”时,没有任何内容被导出(从 LogCat);如何进行“全选”?
  • 这里有很多“细节”:stackoverflow.com/questions/8767184/…
【解决方案2】:

使用 Eclipse 向导创建一个新的 XML 布局文件。此文件的名称将替换您代码中的simple_list_item_1

在此布局中,创建一个TextView(或任何其他容器)元素并将其命名为text1

然后SimpleCursorAdaptor 将匹配您的联系信息和布局中的text1 字段

<?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/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

【讨论】:

  • 它有助于解决您的问题吗?
猜你喜欢
  • 2018-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-26
  • 1970-01-01
  • 2015-02-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多