读取数据库的值显示到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;
}
}
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!
[解决办法]
问题并不在你贴出的代码层面啊
需要跟踪调试才行呢