我正在尝试使用Cursor从表中检索一些数据,以便在TextView中显示它,但是我收到了一个错误。
请小心帮助我。
这是我的代码
public class DisplayBudget extends AppCompatActivity implements View.OnClickListener {
SQLiteDatabase db;
TextView bname,name,limit;
Button del;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.budget_details);
bname = (TextView) findViewById(R.id.textViewbname);
name = (TextView) findViewById(R.id.textViewname);
limit = (TextView) findViewById(R.id.textViewlimit);
del = (Button) findViewById(R.id.button2);
del.setOnClickListener(this);
DBController dbController = new DBController(this);
dbController.openToDisplay();
Cursor cursor = db.rawQuery(" select *from "+DBHelper.TABLE_NAME+ " where id = 1 ",null);
cursor.moveToFirst();
name.append(cursor.getString(cursor.getColumnIndex(DBHelper.OWNER_NAME)));
bname.append(cursor.getString(cursor.getColumnIndex(DBHelper.BUDGET_NAME)));
limit.append(cursor.getString(cursor.getColumnIndex(DBHelper.BUDGET_LIMIT)));
cursor.close();
}
这是LogCat
12-25 00:39:46.440 30123-30123/com.example.rajkumar.budget W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb0d40b20)
12-25 00:39:46.440 30123-30123/com.example.rajkumar.budget E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.rajkumar.budget, PID: 30123
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rajkumar.budget/com.example.rajkumar.budget.DisplayBudget}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.rajkumar.budget.DisplayBudget.onCreate(DisplayBudget.java:28)
at android.app.Activity.performCreate(Activity.java:5231)
答案 0 :(得分:2)
您获得了NPE
,因为<{1}}位于
db=null
您初始化 db 的位置?对于未来的问题,也请参考 Cursor cursor = db.rawQuery(" select * from "+DBHelper.TABLE_NAME+ " where id = 1 ",null);
回答。
答案 1 :(得分:1)
在* and from
之间占用空间,以便您的查询看起来像
Cursor cursor = db.rawQuery("select * from "+DBHelper.TABLE_NAME+ " where id = 1 ",null);
答案 2 :(得分:0)
你只是改变了,所以你不能得到错误: -
String dataquery =&#34;从表名中选择*,其中id =&#34; + ID; 的
的光标游标= db.rawQuery(dataquery,null);
有用的
答案 3 :(得分:0)
试试这个。
DBController dbController = new DBController(this);
SQLiteDatabase db = createDatabase.open();
Cursor cursor = db.rawQuery(" select * from "+DBHelper.TABLE_NAME+ " where id = 1 ",null);
if(cursor.moveToFirst()){
name.append(cursor.getString(cursor.getColumnIndex(DBHelper.OWNER_NAME)));
bname.append(cursor.getString(cursor.getColumnIndex(DBHelper.BUDGET_NAME)));
limit.append(cursor.getString(cursor.getColumnIndex(DBHelper.BUDGET_LIMIT))); }