【问题标题】:Android SimpleCursorAdapter ListView ErrorAndroid SimpleCursorAdapter ListView 错误
【发布时间】:2013-04-15 20:25:46
【问题描述】:

我一直在搞乱 SimpleCursorAdapter 和 Listview 并得到不同的结果。起初我在我的列表视图上得到了一些结果,但是布局并不好,所以我决定修改它。然而,这似乎毁了一切,现在 APP 不断崩溃并出现空指针异常类型错误,我似乎无法弄清楚。

我的列表视图的 XML

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/linearLayout1" >





        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true" >

        </ListView>

    </RelativeLayout>

</RelativeLayout>

</ScrollView> 

我的 ListActivity 活动类

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.playerdata);
    //list = (ListView) findViewById(R.id.list);

    mDbHelper = new PlayerDbAdapter(this);
    mDbHelper.open();
    fillData();
    registerForContextMenu(getListView());
}

 private void fillData() {
    Cursor players = mDbHelper.fetchAllPlayers();
    startManagingCursor(players);

    // Create an array to specify the fields we want to display in the list (only TITLE)
    String[] from = new String[]{PlayerDbAdapter.KEY_TITLE,PlayerDbAdapter.KEY_BODY};


    // and an array of the fields we want to bind those fields to (in this case just text1)
    int[] to = new int[]{R.id.playerPosition,R.id.playerName};

    // Now create a simple cursor adapter and set it to display
    SimpleCursorAdapter playerlist = 
        new SimpleCursorAdapter(this, R.layout.playerinfo , players, from, to);
    setListAdapter(playerlist);
}

LogCat

04-15 20:09:07.326: E/AndroidRuntime(600): FATAL EXCEPTION: main
04-15 20:09:07.326: E/AndroidRuntime(600): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{your.dissertation.project/your.dissertation.project.PlayersActivity}: java.lang.NullPointerException
04-15 20:09:07.326: E/AndroidRuntime(600):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1544)
04-15 20:09:07.326: E/AndroidRuntime(600):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
04-15 20:09:07.326: E/AndroidRuntime(600):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-15 20:09:07.326: E/AndroidRuntime(600):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
04-15 20:09:07.326: E/AndroidRuntime(600):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-15 20:09:07.326: E/AndroidRuntime(600):  at android.os.Looper.loop(Looper.java:123)
04-15 20:09:07.326: E/AndroidRuntime(600):  at android.app.ActivityThread.main(ActivityThread.java:3647)
04-15 20:09:07.326: E/AndroidRuntime(600):  at java.lang.reflect.Method.invokeNative(Native Method)
04-15 20:09:07.326: E/AndroidRuntime(600):  at java.lang.reflect.Method.invoke(Method.java:507)
04-15 20:09:07.326: E/AndroidRuntime(600):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-15 20:09:07.326: E/AndroidRuntime(600):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-15 20:09:07.326: E/AndroidRuntime(600):  at dalvik.system.NativeStart.main(Native Method)
04-15 20:09:07.326: E/AndroidRuntime(600): Caused by: java.lang.NullPointerException
04-15 20:09:07.326: E/AndroidRuntime(600):  at android.app.Activity.setContentView(Activity.java:1657)
04-15 20:09:07.326: E/AndroidRuntime(600):  at android.app.ListActivity.ensureList(ListActivity.java:312)
04-15 20:09:07.326: E/AndroidRuntime(600):  at android.app.ListActivity.getListView(ListActivity.java:297)
04-15 20:09:07.326: E/AndroidRuntime(600):  at your.dissertation.project.PlayersActivity.<init>(PlayersActivity.java:27)
04-15 20:09:07.326: E/AndroidRuntime(600):  at java.lang.Class.newInstanceImpl(Native Method)
04-15 20:09:07.326: E/AndroidRuntime(600):  at java.lang.Class.newInstance(Class.java:1409)
04-15 20:09:07.326: E/AndroidRuntime(600):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
04-15 20:09:07.326: E/AndroidRuntime(600):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1536)
04-15 20:09:07.326: E/AndroidRuntime(600):  ... 11 more

我的光标类

 public Cursor fetchAllPlayers()
  {
    return this.mDb.query("players", new String[] { "_id", "Player_Name", "Player_Position" }, null, null, null, null, null);
  }

【问题讨论】:

  • 请发布整个 logcat。
  • 用整个 logcat 更新了它

标签: android android-listview simplecursoradapter


【解决方案1】:

你已经接近了,但我发现了几个问题:

  1. startManagingCursor 已弃用。您应该改用 CursorLoader。这在 Android 培训课程 Loading Data in the Background 中有记录。这不会导致您的 RuntimeException,但这是一种糟糕的编程习惯。
  2. ListView 和 SimpleCursorAdapter 的设置需要工作。您犯了一个错误,这在使用 SimpleCursorAdapter 的人中很常见。构造函数的参数是
SimpleCursorAdapter(上下文,item_layout_file,光标,from_columns,to_fields)

在哪里

  • 上下文是一个上下文对象
  • item_layout_file 是一个 layout.xml 文件,其中包含列表视图的一个项目或行的布局。在您的情况下,此布局文件将包含一个 TextView(或两个?),而 不是 ListView。您正在创建一个绑定到 ListView 的对象,而不是 ListView 本身。
  • Cursor是包含数据的Cursor
  • from_columns 是一个包含列名的字符串数组
  • to_fields 是一个整数数组,包含项目布局文件中视图对象的资源 ID。

此外,还有这些要求:

  • from_columns 中的元素数必须与to_fields 中的元素数相同。 我怀疑这是导致错误的原因
  • 光标必须包含名为“_ID”的列。

但是,from_columns 不必包含光标中的所有列。

【讨论】:

  • 你说的很有道理,我打算再试一次。非常感谢。但是我的开发目标是 9 并且我注意到 CursorLoader 是在 11 中实现的?无论如何,谢谢,我想我现在会坚持使用 Simple。将让您了解最新进展。
  • 所以我设法纠正了 from_column 和 to_field 违规行为,还创建了一个新的 xml 文件,其中包含两个文本视图,我将其链接到 to_feild 但是我仍然收到 nullexception 错误。也许它与最后一个要点有关,我不太明白您所说的 ID 列是什么意思。用我的游标类更新了代码,它有 ID 列。
【解决方案2】:

更改
String[] from = new String[]{PlayerDbAdapter.KEY_TITLE,PlayerDbAdapter.KEY_BODY};

String[] from = new String[]{PlayerDbAdapter.KEY_TITLE};

【讨论】:

  • 我纠正了 to_feild 有一个额外的列,因为我需要两者。仍然有同样的问题。
  • 重新发布您错误地实例化 SimpleCursorAdapter 的代码。
  • 更新了新的适配器代码。仍然出现 nullpointexeption 错误。
【解决方案3】:

您的ListView 应该有一个ID android:id="@android:id/list" 而不是android:id="@+id/list"

【讨论】:

  • 但是如果我这样做,我该如何在 SimpleCursorAdapter 上调用它?因为它不再识别列表了。
  • 你指的是list = (ListView) findViewById(R.id.list)这一行吗?应该是list = (ListView) findViewById(android.R.id.list)
猜你喜欢
  • 2014-04-12
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多