读取数据库的值显示到ListView上
====================问题描述====================
我定义了一个了一个查询数据库的方法(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!
====================解决方案1====================
问题并不在你贴出的代码层面啊
需要跟踪调试才行呢
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。