我在表格中有数据。当我从它设置适配器时,应用程序崩溃,但如果我从服务器加载信息,那么一切都很好。我在设置listview的适配器时得到NullPointerException。尽管如此,我做的事情与桌子是空的完全一样。
// check if database is empty
cursor = sh.getReadableDatabase().rawQuery(
"SELECT * FROM " + PetopenTable.TABLE_NAME, null);
if (cursor.getCount() < 1) {
//cursor is empty
tableHasData = false;
} else {
tableHasData = true;
}
if (tableHasData) {
// we have data in database, show it, then get info
// from server and reload the UI
Log.d("Cursor", "PetOpenFragment; cursor is not empty");
prepareListFromCursor(cursor);
// initialize the list
lview3 = (ListView) getActivity().findViewById(R.id.listView1);
adapter = new ListViewCustomAdapter(getActivity(), itemList);
// here adapter is not null
if (adapter != null) {
Log.d("Adapter in Petopen", "is not null");
lview3.setAdapter(adapter); // line 94
} else {
Log.d("ADapter in petopen", "is null");
}
// do not show the loading message as we can fill it instantly from database
// now get data from server to update
new getPetopenUsers().execute((Void) null);
} else {
// show loading and load from server, database is empty
Log.d("Cursor", "PetOpenFragment; cursor is empty");
new getPetopenUsers().execute((Void) null);
mDialog.setMessage("Loading...");
mDialog.setCancelable(false);
mDialog.show();
}
...
// inside prepareListFromCursor I initialize the itemlist
itemList = new ArrayList<Object>();
...
//when i get data from server, i initialize same as when there is data in table
lview3 = (ListView) getActivity().findViewById(R.id.listView1);
adapter = new ListViewCustomAdapter(getActivity(), itemList);
lview3.setAdapter(adapter);
Logcat错误:
ATAL EXCEPTION: main
java.lang.NullPointerException
at com.petcial.petopen.fragments.petOpenFragment.onCreateView(petOpenFragment.java:94)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
at android.os.Handler.handleCallback(Handler.java)
at android.os.Handler.dispatchMessage(Handler.java)
at android.os.Looper.loop(Looper.java)
at android.app.ActivityThread.main(ActivityThread.java)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
at dalvik.system.NativeStart.main(Native Method)
更新
以下是我如何填充itemList,我在解析表
中的信息后调用此方法 private void AddObjectToList(String image, String name, String status,
String distance, String location, String[] petImage,
String[] petName) {
// TODO Auto-generated method stub
bean = new ItemBean();
bean.setProfileImage(image);
bean.setName(name);
bean.setStatus(status);
bean.setDistance(distance);
bean.setLocation(location);
bean.setPetImage(petImage);
bean.setPetName(petName);
itemList.add(bean);
}
答案 0 :(得分:0)
最后,我尝试将整个数据库中的数据加载到AsyncTask中,并且工作正常。如果有人遇到同样的问题,你必须这样做:
创建一个AsyncTask,它是从onCreate或onViewCreated调用的(在我的例子中,我是片段) 在doInBackground方法中我没有任何东西,而是在onPostExecute方法中,我调用处理游标中所有数据的所有方法并初始化列表和适配器。
如果某人有更好的解决方法如何在AsyncTask中进行此操作,请回答此帖。