Java NullPointerException / Layout在Android上使用列表和ListView时出错

时间:2010-12-24 19:35:57

标签: android sqlite android-layout android-widget

嘿,我正在尝试在Android上实现ListView,它将打印从SQLite数据库中检索到的数据。所以我想从表中检索很多列并将其添加到列表中,因此我将此列表打印为ListView。

为此我创建了一个方法,它将在一个单独的类中选择表中的所有列,然后我将在ListActivity中打印ListView。

我想检索表的6列,用ids 2,4,5,6,7,9表示。

但它显示了很多错误:

12-24 19:19:04.066: ERROR/AndroidRuntime(22630): FATAL EXCEPTION: main
12-24 19:19:04.066: ERROR/AndroidRuntime(22630): java.lang.NullPointerException
12-24 19:19:04.066: ERROR/AndroidRuntime(22630):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355)
12-24 19:19:04.066: ERROR/AndroidRuntime(22630):     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
12-24 19:19:04.066: ERROR/AndroidRuntime(22630):     at android.widget.AbsListView.obtainView(AbsListView.java:1418)
12-24 19:19:04.066: ERROR/AndroidRuntime(22630):     at android.widget.ListView.makeAndAddView(ListView.java:1745)
12-24 19:19:04.066: ERROR/AndroidRuntime(22630):     at android.widget.ListView.fillDown(ListView.java:670)
12-24 19:19:04.066: ERROR/AndroidRuntime(22630):     at android.widget.ListView.fillFromTop(ListView.java:727)
12-24 19:19:04.066: ERROR/AndroidRuntime(22630):     at android.widget.ListView.layoutChildren(ListView.java:1598)
12-24 19:19:04.066: ERROR/AndroidRuntime(22630):     at android.widget.AbsListView.onLayout(AbsListView.java:1248)
12-24 19:19:04.066: ERROR/AndroidRuntime(22630):     at android.view.View.layout(View.java:7175)
12-24 19:19:04.066: ERROR/AndroidRuntime(22630):     at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
12-24 19:19:04.066: ERROR/AndroidRuntime(22630):     at android.view.View.layout(View.java:7175)
12-24 19:19:04.066: ERROR/AndroidRuntime(22630):     at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
12-24 19:19:04.066: ERROR/AndroidRuntime(22630):     at android.view.View.layout(View.java:7175)
12-24 19:19:04.066: ERROR/AndroidRuntime(22630):     at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
12-24 19:19:04.066: ERROR/AndroidRuntime(22630):     at android.view.View.layout(View.java:7175)
12-24 19:19:04.066: ERROR/AndroidRuntime(22630):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
12-24 19:19:04.066: ERROR/AndroidRuntime(22630):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
12-24 19:19:04.066: ERROR/AndroidRuntime(22630):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
12-24 19:19:04.066: ERROR/AndroidRuntime(22630):     at android.view.View.layout(View.java:7175)
12-24 19:19:04.066: ERROR/AndroidRuntime(22630):     at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
12-24 19:19:04.066: ERROR/AndroidRuntime(22630):     at android.view.View.layout(View.java:7175)
12-24 19:19:04.066: ERROR/AndroidRuntime(22630):     at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
12-24 19:19:04.066: ERROR/AndroidRuntime(22630):     at android.view.View.layout(View.java:7175)
12-24 19:19:04.066: ERROR/AndroidRuntime(22630):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
12-24 19:19:04.066: ERROR/AndroidRuntime(22630):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
12-24 19:19:04.066: ERROR/AndroidRuntime(22630):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
12-24 19:19:04.066: ERROR/AndroidRuntime(22630):     at android.view.View.layout(View.java:7175)
12-24 19:19:04.066: ERROR/AndroidRuntime(22630):     at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
12-24 19:19:04.066: ERROR/AndroidRuntime(22630):     at android.view.View.layout(View.java:7175)
12-24 19:19:04.066: ERROR/AndroidRuntime(22630):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
12-24 19:19:04.066: ERROR/AndroidRuntime(22630):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
12-24 19:19:04.066: ERROR/AndroidRuntime(22630):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-24 19:19:04.066: ERROR/AndroidRuntime(22630):     at android.os.Looper.loop(Looper.java:123)
12-24 19:19:04.066: ERROR/AndroidRuntime(22630):     at android.app.ActivityThread.main(ActivityThread.java:3647)
12-24 19:19:04.066: ERROR/AndroidRuntime(22630):     at java.lang.reflect.Method.invokeNative(Native Method)
12-24 19:19:04.066: ERROR/AndroidRuntime(22630):     at java.lang.reflect.Method.invoke(Method.java:507)
12-24 19:19:04.066: ERROR/AndroidRuntime(22630):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-24 19:19:04.066: ERROR/AndroidRuntime(22630):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-24 19:19:04.066: ERROR/AndroidRuntime(22630):     at dalvik.system.NativeStart.main(Native Method)

以下是选择数据的方法代码。

public List<String> selectAll() {

    List<String> list1 = new ArrayList<String>();
    List<String> list2 = new ArrayList<String>();
    List<String> list3 = new ArrayList<String>();
    List<String> list4 = new ArrayList<String>();
    List<String> list5 = new ArrayList<String>();
    List<String> list6 = new ArrayList<String>();

    Cursor cursor = this.db.query(TABLE_NAME, null, null, null, null, null,
            "duration desc");

    if (cursor.moveToFirst()) {
        do {

            list1.add(cursor.getString(2));
            list2.add(cursor.getString(4));
            list3.add(cursor.getString(5));
            list4.add(cursor.getString(6));
            list5.add(cursor.getString(7));
            list6.add(cursor.getString(9));

            list1.addAll(list2);
            list1.addAll(list3);
            list1.addAll(list4);
            list1.addAll(list5);
            list1.addAll(list6);

        } while (cursor.moveToNext());
        Log.i(TAG, "After cursor.moveToNext()");
    }
    if (cursor != null && !cursor.isClosed()) {
        cursor.close();
    }
    Log.i(TAG, "Before selectAll returnment");
    return list1;
}

这是ListActivity类的代码:

public class RatedCalls extends ListActivity {

private static final String LOG_TAG = "RatedCallsActivity";
private CallDataHelper cdh;
StringBuilder sb = new StringBuilder();
OpenHelper openHelper = new OpenHelper(RatedCalls.this);

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    Log.i(LOG_TAG, "calling from onCreate()");

    cdh = new CallDataHelper(this);

    Log.i(LOG_TAG, "--->>> before calling the service");
    startService(new Intent(this, RatedCallsService.class));
    Log.i(LOG_TAG, "Service called.");
    Log.i(LOG_TAG, "--->>> after calling the service");
    fillList();

}

public void fillList() {

    List<String> ratedCalls = this.cdh.selectAll();

    setListAdapter(new ArrayAdapter<String>(this, R.layout.listitem,
            ratedCalls));

    ListView lv = getListView();
    lv.setTextFilterEnabled(true);

    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // When clicked, show a toast with the TextView text
            Toast.makeText(getApplicationContext(),
                    ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
        }
    });
}
    }

4 个答案:

答案 0 :(得分:1)

问题是在数据库中,如果列值为空,则游标返回null,因此,这就是JavaNullPointerException。因此,如果没有要在数据库中插入的联系人姓名,我插入了一个空字符串。这样可行!

答案 1 :(得分:0)

空指针异常意味着某些内容未正确初始化。我已经能够从错误日志中解读它似乎是在某处创建列表的地方,但我只看到了通用的android类,在你的代码中没有任何内容。不确定这是否会有所帮助,但看起来错误堆栈的一部分缺失,它可能非常有用,所以......

答案 2 :(得分:0)

非常确定你的NPE在这里

setListAdapter(new ArrayAdapter<String>(this, R.layout.listitem, ratedCalls));

至少这是我对logcat的猜测。 R.layout.listitem是否为null?

为什么只使用ArrayAdapter。您可能应该继承CursorAdapter。听起来你想要的东西比ArrayAdapter给你的更复杂。

答案 3 :(得分:0)

请更改您的查询。您读取未使用的数据。我不知道确实存在多少行和列,但您可能会看到性能稍有增加。

此致 HJW