在用android studio开发app,现在用户注册登录系统连数据库,但是现在有问题,我想根据数据库里的用户表的不同用户类型,跳转不同activitiy,可是报错了,下面是代码 private TextView TVToRegister; private EditText username; private DBHelper demodb; SQLiteDatabase sqLiteDatabase; @Override TVToRegister = (TextView) findViewById(R.id.tv_register); submit = (Button) findViewById(R.id.btn_submit); username = (EditText) findViewById(R.id.edt_username); demodb= new DBHelper(this); } @Override if(username.getText().toString().equals(“”) || code.getText().toString().equals(“”)) boolean flag = demodb.login(UserName, Code); if(demodb.cate(sqLiteDatabase).getString(0).equals(“保修”)){ }else{ } DBHelper private final static String TABLE_NAME = “request_list”; private final static String TABLE_NAME0 = “user_info”; public DBHelper(Context context) { } @Override db.execSQL(sql_reporter); String sql = “CREATE TABLE ” + TABLE_NAME + } @Override public Cursor select() { public boolean login(String user_name,String password) SQLiteDatabase sdb=this.getReadableDatabase(); } public Cursor getInformation(SQLiteDatabase db) cursor=db.query(TABLE_NAME,projection,null,null,null,null,null,null); public Cursor cate(SQLiteDatabase db){ cursor= db.query(TABLE_NAME0,projection,selection,null,null,null,null); public long UserInfo_insert (String reporter_name,String reporter_email, String reporter_mobile, String user_name,String user_password,String user_type) ContentValues cv = new ContentValues(); cv.put(NAME0,reporter_name); long row = db.insert(TABLE_NAME0, null, cv); public long insert(String name,String title,String description,String department, String category) cv.put(NAME,name); cv.put(DEPARTMENT,department); long row = db.insert(TABLE_NAME, null, cv); ContentValues cv = new ContentValues(); cv.put(DEPARTMENT,department); 报错说红色的那两句指向空指针错误。请问该如何解决 |
|
#1 |
就是那会值还米回来啊。
|
#2 |
(“保修”).equals(demodb.cate(sqLiteDatabase).getString(0))反过来写,屏蔽空指针异常。具体的为什么为空好需要去看看。
|
#3 |
你能帮我看下吗?我实在不知道哪里错了该怎么改
|
#4 |
回复1楼: 那应该怎么改啊,我明明已经把数据添加到数据表里了,而且我导出数据库也看的到已经成功了,表里明明有数据的啊 |
#5 |
回复2楼: 你能帮我看下吗?我实在不知道哪里错了该怎么改 |
10分
#6 |
回复5楼: (demodb.cate(sqLiteDatabase).getString(0).equals(“保修”)你里面的sqLiteDatabase没有实例化吧!一直为空。 |
#7 |
回复6楼: 我加了句sqLiteDatabase = demodb.getReadableDatabase(); 然后又加了if(cursor.movetoFirst){}语句 求赐教? |
10分
#8 |
回复7楼: 下标越界了,你首先看看你加的if(cursor.movetoFirst){}语句中cursor是否有数据。同时把你修改之后的代码发出来看看。 |
#9 |
修改后的LoginActivity.class 修改部分用蓝色标记了
public class LoginActivity extends Activity implements View.OnClickListener { private TextView TVToRegister; private EditText username; private Spinner vill; private DBHelper demodb; SQLiteDatabase sqLiteDatabase; @Override TVToRegister = (TextView) findViewById(R.id.tv_register); submit = (Button) findViewById(R.id.btn_submit); username = (EditText) findViewById(R.id.edt_username); demodb= new DBHelper(this); vill = (Spinner) findViewById(R.id.spinner_VILL); } @Override String UserName = username.getText().toString(); if (username.getText().toString().equals(“”) || code.getText().toString().equals(“”)) { boolean flag = demodb.login(UserName, Code); Cursor cursor = demodb.cate(sqLiteDatabase,UserName); Log.i(“TAG”, “Joo”); } else { } 然后DBHelper我也修改了一下,修改部分我蓝色显示 private final static String TABLE_NAME = “request_list”; private final static String TABLE_NAME0 = “user_info”; public DBHelper(Context context) { } @Override db.execSQL(sql_reporter); String sql = “CREATE TABLE ” + TABLE_NAME + } @Override public Cursor select() { public boolean login(String user_name,String password) SQLiteDatabase sdb=this.getReadableDatabase(); } public Cursor getInformation(SQLiteDatabase db) cursor=db.query(TABLE_NAME,projection,null,null,null,null,null,null); public Cursor cate(SQLiteDatabase db, String user_name){ cursor= db.query(TABLE_NAME0,projection,selection,selection_args,null,null,null,null); public long UserInfo_insert (String reporter_name,String reporter_email, String reporter_mobile, String user_name,String user_password,String user_type) ContentValues cv = new ContentValues(); cv.put(NAME0,reporter_name); long row = db.insert(TABLE_NAME0, null, cv); public long insert(String name,String title,String description,String department, String category) cv.put(NAME,name); cv.put(DEPARTMENT,department); long row = db.insert(TABLE_NAME, null, cv); ContentValues cv = new ContentValues(); cv.put(DEPARTMENT,department); } |
10分
#10 |
回复9楼: if(cursor.moveToFirst){ |
#11 |
回复10楼: 这样的话程序倒没有报错,可就是直接跳转到reporter.class了,如果是这样的话说明他没有读到usertype那一列 |
#12 |
回复10楼: 我发现问题所在了!问题在于这一句 Log.i(“TAG”, “Joo”); |
#13 |
回复10楼: 我把那句else删掉了以后程序是对了,可是我关机又重启又不对了,页面还是不跳转了,但他还是读取到数据了。求解 |
10分
#14 |
跳转不了,当时程序运行到哪里了呢?多加几个log看看。
|
#15 |
回复14楼: 解决了,主要问题是改成UTF-8格式就好了,之前一直是GMK。 |