gpt4 book ai didi

android - 在 Android 上使用列表和 ListView 时出现 JavaNullPointerException/Layout 错误

转载 作者:行者123 更新时间:2023-11-30 04:47:25 25 4
gpt4 key购买 nike

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

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

我想检索表的 6 列,用 id 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();
}
});
}
}

最佳答案

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

关于android - 在 Android 上使用列表和 ListView 时出现 JavaNullPointerException/Layout 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4528044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com