我正在尝试从数据库中填充ListVIEw,并使用花哨的删除按钮调整每一行.所以我创建了列表Activity和自定义SimpleCursorAdapter.
这是主要的ListVIEw活动:
public class EditEntrIEs extends ListActivity { Cursor cursor; DBAdapter db = new DBAdapter(this); private static String[] FROM = { _ID, score_ColUMN, "date(time, 'localtime')", }; private static int[] TO = { R.ID.rowID, R.ID.Title, R.ID.time, }; public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.entrIEs_List); try { cursor = getEvents(); showEvents(cursor); } finally { db.close(); } } private voID showEvents(Cursor cursor) { // Set up data binding SMSimpleCursorAdapter adapter = new SMSimpleCursorAdapter(this, R.layout.entrIEs_List_item, cursor, FROM, TO); setlistadapter(adapter); } private Cursor getEvents() { db.open(); cursor = db.getAllscores(); startManagingCursor(cursor); return cursor; } voID delRow() { // this is method for deleting row by _ID db.open(); db.deletescore(3); // here will be TAG with _ID from adapter, db.close(); // for Now I just use hardcoded _ID }}
这是自定义的SimpleCursorAdapter:
public class SMSimpleCursorAdapter extends SimpleCursorAdapter{ Cursor c; Context context; Activity activity; public SMSimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) { super(context, layout, c, from, to); this.c = c; this.context=context; this.activity=(Activity) context; } EditEntrIEs dbDel = new EditEntrIEs(); //from prevIoUs code sample @OverrIDe public VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent) { if(convertVIEw == null) convertVIEw = VIEw.inflate(context, R.layout.entrIEs_List_item, null); VIEw row = convertVIEw; c.movetoposition(position); TextVIEw score = (TextVIEw) convertVIEw.findVIEwByID(R.ID.Title); TextVIEw time = (TextVIEw) convertVIEw.findVIEwByID(R.ID.time); TextVIEw ID = (TextVIEw) convertVIEw.findVIEwByID(R.ID.rowID); ID.setText(c.getString(0)); time.setText(c.getString(2)); score.setText(c.getString(1)); String daTag = c.getString(1); Imagebutton delbutton = (Imagebutton) convertVIEw.findVIEwByID(R.ID.delbutton); delbutton.setFocusable(true); delbutton.setClickable(true); //delbutton.setTag(daTag); delbutton.setonClickListener(new OnClickListener() { //Click Listener fro delete button @OverrIDe public voID onClick(VIEw vIEw) { dbDel.delRow(); //this is "delete row" method from previos code sample } }); return(row); }}
当我点击删除按钮时,所有这些东西都给了我NullPointerException.
我是androID的新手,并且假设我错过了一些明显的东西.
logcat的:
ERROR/AndroIDRuntime(14027): FATAL EXCEPTION: main
ERROR/AndroIDRuntime(14027): java.lang.NullPointerException
ERROR/AndroIDRuntime(14027): at androID.content.Contextwrapper.openorCreateDatabase(Contextwrapper.java:203)
ERROR/AndroIDRuntime(14027): at androID.database.sqlite.sqliteOpenHelper.getWritableDatabase(sqliteOpenHelper.java:98)
ERROR/AndroIDRuntime(14027): at namespace.DBAdapter.open(DBAdapter.java:66)
ERROR/AndroIDRuntime(14027): at namespace.EditEntrIEs.delRow(EditEntrIEs.java:50)
ERROR/AndroIDRuntime(14027): at namespace.SMSimpleCursorAdapter$1.onClick(SMSimpleCursorAdapter.java:64)
ERROR/AndroIDRuntime(14027): at androID.vIEw.VIEw.performClick(VIEw.java:2414)
ERROR/AndroIDRuntime(14027): at androID.vIEw.VIEw$PerformClick.run(VIEw.java:8838)
ERROR/AndroIDRuntime(14027): at androID.os.Handler.handleCallback(Handler.java:587)
ERROR/AndroIDRuntime(14027): at androID.os.Handler.dispatchMessage(Handler.java:92)
ERROR/AndroIDRuntime(14027): at androID.os.Looper.loop(Looper.java:123)
ERROR/AndroIDRuntime(14027): at androID.app.ActivityThread.main(ActivityThread.java:4680)
ERROR/AndroIDRuntime(14027): at java.lang.reflect.Method.invokeNative(Native Method)
ERROR/AndroIDRuntime(14027): at java.lang.reflect.Method.invoke(Method.java:521)
ERROR/AndroIDRuntime(14027): at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
ERROR/AndroIDRuntime(14027): at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:616)
ERROR/AndroIDRuntime(14027): at dalvik.system.NativeStart.main(Native Method)
解决方法:
问题出在您的SMSimpleCursorAdapter代码中:
EditEntrIEs dbDel = new EditEntrIEs(); //from prevIoUs code sample
您创建一个新对象但它不是托管活动(例如,不会调用onCreate方法).当您尝试第二次(从您的适配器)创建它时,NPE可能来自您的DBAdapter.
快速解决:
EditEntrIEs dbDel; //from prevIoUs code samplepublic SMSimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) { super(context, layout, c, from, to); this.c = c; this.context = context; this.activity = (Activity) context; this.dbDel = (EditEntrIEs) context;}
如果您不想只是快速修复,以下将是一个更好的解决方案:
>将delRow,getEvents方法移动到DBAdapter类中
>修改SMSimpleCursorAdapter的构造函数以提供DBAdapter类(在ListActivity中创建).
以上是内存溢出为你收集整理的android – 自定义SimpleCursorAdapter,数据库查询和NullPointerException全部内容,希望文章能够帮你解决android – 自定义SimpleCursorAdapter,数据库查询和NullPointerException所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)