我想用SQLiteDataBase中的数据填充我的ListView,但是我的光标出错了。我是android编程的初学者,请帮忙。谢谢。 我正在使用片段。 。 这是我的片段代码:
package com.android.jayzero2013.estudiante;
import android.database.Cursor;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class deleteSchedulePage extends Fragment {
private ListView listview;
scheduleDataBase mydb;
View viewPage;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
viewPage= inflater.inflate(R.layout.delete_schedule_layout,container,false);
listview=(ListView)viewPage.findViewById(R.id.deleteSchedList);
return viewPage;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mydb=new scheduleDataBase(getActivity());
getDb();
}
public void getDb(){
Cursor get = mydb.getalldata();
String[] fields = new String[]{mydb.Col1, mydb.Col2};
int[] textViewId = new int[]{R.id.itemNumTExt, R.id.SubjectCodeTextView};
SimpleCursorAdapter myCursorAdapter;
myCursorAdapter = new SimpleCursorAdapter(getActivity().getBaseContext(), R.layout.sched_list, get, fields, textViewId, 0);
listview = (ListView) viewPage.findViewById(R.id.deleteSchedList);
listview.setAdapter(myCursorAdapter);
}
}
这是我的SQLiteDatabase代码:
package com.android.jayzero2013.estudiante;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by jayzero2013 on 23/11/2015.
*/
public class scheduleDataBase extends SQLiteOpenHelper {
public static final String Database_Name="Schedule.db";
public static final String Table_Name="My_Schedule";
public static final String Col1="Number";
public static final String Col2="Subject_Code";
public static final String Col3="Subject_Description";
public static final String Col4="Type";
public static final String Col5="Room";
public static final String Col6="Day";
public static final String Col7="TimeStart";
public static final String Col8="TimeEnd";
private SQLiteDatabase db;
public static final String [] ALL_KEYS=new String[]{Col1, Col2};
public scheduleDataBase(Context context) {
super(context, Database_Name, null, 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("create table " + Table_Name + " (" +
Col1 + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
Col2 + " TEXT NOT NULL, " + Col3 + " TEXT, " +
Col4 + " TEXT NOT NULL, " + Col5 + " TEXT, " +
Col6 + " TEXT NOT NULL, " + Col7 + " TEXT NOT NULL, " +
Col8 + " TEXT NOT NULL)");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + Table_Name);
onCreate(sqLiteDatabase);
}
public boolean insertData(String sbjCD, String sbjDsc, String room, String typ, String day, String tymstrt, String tymnd){
SQLiteDatabase sqLiteDatabase=this.getReadableDatabase();
ContentValues content=new ContentValues();
content.put(Col2, sbjCD);
content.put(Col3, sbjDsc);
content.put(Col4, typ);
content.put(Col5, room);
content.put(Col6, day);
content.put(Col7, tymstrt);
content.put(Col8, tymnd);
long result=sqLiteDatabase.insert(Table_Name, null, content);
if(result==-1){
return false;
}
else{
return true;
}
}
public Cursor getalldata(){
String where= null;
Cursor c=db.query(true, Table_Name, ALL_KEYS,where,null,null,null,null,null);
if(c!=null){
c.moveToFirst();
}
return c;
}
}
这是来自LogCat的错误消息:
11-27 23:57:52.758 10346-10346/com.android.jayzero2013.estudiante E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.android.jayzero2013.estudiante.scheduleDataBase.getalldata(scheduleDataBase.java:76)
at com.android.jayzero2013.estudiante.deleteSchedulePage.getDb(deleteSchedulePage.java:42)
at com.android.jayzero2013.estudiante.deleteSchedulePage.onActivityCreated(deleteSchedulePage.java:37)
at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1973)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1103)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1259)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1624)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
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:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
您的db对象为null,在使用它之前,您应该调用它:
db = getWriteabledatabase();