更新时间:2023-12-01 13:46:16
您在您的参考 listNotes
函数有一个错误。 执行
没有一个叫listNotes的功能,所以这就是正在恢复为空
您需要引用其中listNotes出现的实际类,如 Controls.listNotes();
I am trying to populate listview within a fragment class. I am trying to query that for every one student he has many toDo's s. When the user is logged on I want to retrieve all his ToDo's he uniquely added and only data that belongs to him will be shown.
Here is my Controls for notes to retrieve all notes
private DatabaseHelper help;
private SQLiteDatabase db;
public Cursor listNotes() {
Cursor c = db.query(help.NOTE_TABLE, new String[]{help.COLUMN_TITLE,help.COLUMN_BODY, help.COLUMN_DATE}, help.COLUMN_ID + " = ?", new String[]{String.valueOf(help.NOTES_ID)} , null, null, null);
if(c != null) {
c.moveToFirst();
}
return c;
}
My databasehelper is the class that only creates tables. Why is it when I try to get the data it is a null pointer, here is my populateList method.
public void populateList(){
Cursor cursor = execute.listNotes();
//Mapping the fields cursor to text views
String[] fields = new String[]{help.COLUMN_TITLE,help.COLUMN_BODY, help.COLUMN_DATE};
int [] text = new int[] {R.id.item_title,R.id.item_body, R.id.item_date};
adapter = new SimpleCursorAdapter(getActivity(),R.layout.list_layout,cursor, fields, text,0);
//Calling list object instance
listView = (ListView) getView().findViewById(android.R.id.list);
listView.setAdapter(adapter);
}
LogCat
java.lang.NullPointerException
at com.example.app.Notes.populateList(Notes.java:49)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
at android.support.v4.view.ViewPager$3.run(ViewPager.java:249)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:524)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
You have an error in your reference to the listNotes
function. execute
doesn't have a function called listNotes, so that is what is returning as null
You need to reference the actual class in which listNotes appears, e.g. Controls.listNotes();