读取数据库的值显示到ListView上

时间:2022-06-21 11:16:37
我定义了一个了一个查询数据库的方法(selectAllRecrod),将查询到的值保存在一个List<String>中,使用单元测试,这个方法可行,能执行出结果来。ListView我用的是ArrayAdapter适配器,自己写数据可以用,但是一使用selectAllRecrod方法返回的List就报错,空指针异常。我想知道如果要查询数据库,是用的适配器是不是必须为SimpleCursorAdapter,我想应该不是吧。
java代码如下:
public class TowFragment extends Fragment
{

private ListView listview = null;
private MyDao myDao = null;
private List<String> listData = null;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.tow, container, false);
listview = (ListView) view.findViewById(R.id.listview);
myDao = new MyDao(getActivity());
listData = myDao.selectedAllRecord();
listview.setAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_expandable_list_item_1, listData));
return view;
}
}


报错的Log如下:

06-21 07:48:17.486: E/AndroidRuntime(16659): FATAL EXCEPTION: main
06-21 07:48:17.486: E/AndroidRuntime(16659): java.lang.NullPointerException
06-21 07:48:17.486: E/AndroidRuntime(16659):         at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at android.widget.AbsListView.obtainView(AbsListView.java:2159)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at android.widget.ListView.onMeasure(ListView.java:1158)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at android.view.View.measure(View.java:15518)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at android.view.View.measure(View.java:15518)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at android.view.View.measure(View.java:15518)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1389)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at android.view.View.measure(View.java:15518)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at android.view.View.measure(View.java:15518)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at android.view.View.measure(View.java:15518)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at android.view.View.measure(View.java:15518)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at android.view.Choreographer.doCallbacks(Choreographer.java:562)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at android.view.Choreographer.doFrame(Choreographer.java:532)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at android.os.Handler.handleCallback(Handler.java:725)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at android.os.Handler.dispatchMessage(Handler.java:92)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at android.os.Looper.loop(Looper.java:137)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at android.app.ActivityThread.main(ActivityThread.java:5041)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at java.lang.reflect.Method.invokeNative(Native Method)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at java.lang.reflect.Method.invoke(Method.java:511)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-21 07:48:17.486: E/AndroidRuntime(16659):         at dalvik.system.NativeStart.main(Native Method)
06-21 07:48:21.626: E/InputDispatcher(296): channel '41055180 com.ccniit.zhh.main/com.ccniit.zhh.main.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!

4 个解决方案

#1


求不沉下去啊、

#2


问题并不在你贴出的代码层面啊
需要跟踪调试才行呢

#3


引用 2 楼 helinherong_fan 的回复:
问题并不在你贴出的代码层面啊
需要跟踪调试才行呢

谢谢了,我自己找到错误了。插入数据的时候不小心插入了两个null值,又不知道ListView不能显示null值,所以弄了一个下午,删除了就好了。

#4


你好,我也有这样的想法,也是用Fragment,可是真的不会,从数据库到适配器种种,来显示数据,你解决这个问题了吗。。?

#1


求不沉下去啊、

#2


问题并不在你贴出的代码层面啊
需要跟踪调试才行呢

#3


引用 2 楼 helinherong_fan 的回复:
问题并不在你贴出的代码层面啊
需要跟踪调试才行呢

谢谢了,我自己找到错误了。插入数据的时候不小心插入了两个null值,又不知道ListView不能显示null值,所以弄了一个下午,删除了就好了。

#4


你好,我也有这样的想法,也是用Fragment,可是真的不会,从数据库到适配器种种,来显示数据,你解决这个问题了吗。。?