我有一个程序从listview
填充SQLLiteDB
。初始加载有效,但是当我编辑一个条目时,程序崩溃了simplecursorAdapter = null
。
碎片:
public class TaskListFragment extends ListFragment
{
DBHelper dbHelper;
SimpleCursorAdapter simpleCursorAdapter;
Cursor cursor;
ListView listView;
static final int UPDATE_TASK = 1;
String[] from = {dbHelper.COL_2,dbHelper.COL_3};
int[] to = new int[]{R.id.taskName , R.id.taskDescription};
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.tasklistlayout, container, false);
listView = (ListView)rootView.findViewById(android.R.id.list);
setRetainInstance(true);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
dbHelper = new DBHelper(getActivity());
prepareCursor();
}
public void prepareCursor()
{
new Handler().post(new Runnable()
{
@Override
public void run()
{
cursor = dbHelper.getTasksForListView();
simpleCursorAdapter = new SimpleCursorAdapter (getActivity(),R.layout.taskrowlayout,cursor,from,to,0);
fillList();
}
});
}
public void updateList()
{
simpleCursorAdapter.notifyDataSetChanged();
fillList();
}
public void fillList()
{
setListAdapter(simpleCursorAdapter);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id)
{
Intent intent = new Intent(getActivity().getApplicationContext(), TaskInformation.class);
intent.putExtra("taskId", cursor.getInt(0));
intent.putExtra("taskName", cursor.getString(1));
intent.putExtra("taskDescription", cursor.getString(2));
intent.putExtra("taskStartDate", cursor.getString(3));
intent.putExtra("taskEndDate", cursor.getString(4));
startActivityForResult(intent, UPDATE_TASK);
}
}
MainActivity中的一段代码称之为:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
taskListFragment.updateList();
}
}
当我点击更新
时,TaskInformation类中的代码会运行 public void updateTask(View view)
{
task = new ArrayList<>();
task.add(0,String.valueOf(currentTaskId));
task.add(1,String.valueOf(taskName.getText()));
task.add(2,String.valueOf(taskDescription.getText()));
task.add(3,String.valueOf(startDate.getText()));
task.add(4,String.valueOf(endDate.getText()));
dbHelper.updateTask(task);
setResult(RESULT_OK, intent);
finish();
}
为什么更新时simpleCursorAdapter
总是NULL
?我想在更新时刷新/重新加载listview
。
EDIT ------------------------------
我尝试再次运行prepareCursor但它仍然为null,我似乎无法找出原因:(
编辑2 --------------------------------
我更进一步,似乎所需的context
adapter
已经消失。通过将方法更新为:
public void updateList(Context context)
{
simpleCursorAdapter = new SimpleCursorAdapter (context,R.layout.taskrowlayout,cursor,from,to,0);
simpleCursorAdapter.notifyDataSetChanged();
fillList();
}
只有这次cursor
为空。当我尝试使用它重新填充它时:
cursor = dbHelper.getTasksForListView();
我收到此错误:
05-18 07:30:59.478 7323-7323/com.owlfinity.zeepblok.taskjournal E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.owlfinity.zeepblok.taskjournal, PID: 7323
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65537, result=-1, data=Intent { cmp=com.owlfinity.zeepblok.taskjournal/.TaskInformation (has extras) }} to activity {com.owlfinity.zeepblok.taskjournal/com.owlfinity.zeepblok.taskjournal.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor com.owlfinity.zeepblok.taskjournal.DBHelper.getTasksForListView()' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor com.owlfinity.zeepblok.taskjournal.DBHelper.getTasksForListView()' on a null object reference
at com.owlfinity.zeepblok.taskjournal.TaskListFragment.updateList(TaskListFragment.java:61)
at com.owlfinity.zeepblok.taskjournal.MainActivity.onActivityResult(MainActivity.java:37)
at android.app.Activity.dispatchActivityResult(Activity.java:6428)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
这是getTaskForListView方法:
public Cursor getTasksForListView()
{
SQLiteDatabase db = this.getWritableDatabase();
Cursor result = db.rawQuery("SELECT * FROM " + TABLE_NAME,null);
return result;
}
编辑3 ----------------------
错误消失但仍然没有刷新我的ListView
:(这是代码
public void updateList(Context context)
{
dbHelper = new DBHelper(context);
cursor = dbHelper.getTasksForListView();
simpleCursorAdapter = new SimpleCursorAdapter (context,R.layout.taskrowlayout,cursor,from,to,0);
fillList();
}
public void fillList()
{
dbHelper = new DBHelper(getActivity());
setListAdapter(simpleCursorAdapter);
}
----编辑4
我的ListView
在更新方法中为空。似乎无法找到重建它的方法。
我认为问题在于这段代码:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.tasklistlayout, container, false);
listView = (ListView)rootView.findViewById(android.R.id.list);
setRetainInstance(false);
return rootView;
}
如何在更新方法中重新运行这段代码?
答案 0 :(得分:1)
您不需要制作新的处理程序。
您应该使用listView.post(Runnable)
答案 1 :(得分:0)
找到它,我有多个使用onActivityResult
的活动,并在那里重建cursor
和list
。将它们融合成一个并且它可以工作:D猜我的应用程序很困惑如何处理它。找到t因为请求代码在返回时是我从未设置过的数字。