Hey guys i am making an android app using Android Studio, but now i have some problems. I want to the user turn to different activity according to its different user type("guest, teacher...etc) but when i write code and run it, Android Studio logcats says "java.lang.NullpointExceprion" in these two class
DBHelper.class
public class DBHelper extends SQLiteOpenHelper{
private final static String DATABASE_NAME = "demo.db";
private final static int DATABASE_VERSION = 1;
private final static String TABLE_NAME = "request_list";
public final static String ID = "ID";
public final static String NAME = "Name";
public final static String TITLE = "Title";
public final static String DESCRIPTION = "Description";
public final static String DEPARTMENT = "Department";
public final static String CATEGORY = "Category";
private final static String TABLE_NAME0 = "user_info";
private final static String USERID = "ID";
public final static String USERNAME0 = "Username";
public final static String PASSWORD0 = "Password";
public final static String NAME0 = "Name";
public final static String EMAIL0 = "Email";
public final static String MOBILE0 ="Mobile";
public final static String WRITE = "Write";
public final static String READ ="Read";
public final static String VIEW ="View";
public final static String TYPE = "Type";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
Log.e("Database operations", "Database created/opened");
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql_reporter = "CREATE TABLE " + TABLE_NAME0 +
" (" + USERID + " INTEGER primary key autoincrement, " +
"" + NAME0 + " text, " +
"" + EMAIL0 + " text, " +
"" + MOBILE0 + " text, " +
"" + TYPE + " text, "+
"" + USERNAME0 + " text, "+
"" + PASSWORD0 + " text, "+
"" + WRITE + " text, "+
"" + READ + " text, "+
"" + VIEW + " text);";
db.execSQL(sql_reporter);
Log.e("Database operations", "Reporter Table created/opened");
String sql = "CREATE TABLE " + TABLE_NAME +
" (" + ID + " INTEGER primary key autoincrement, " +
"" + NAME + " text, " +
"" + TITLE + " text, " +
"" + CATEGORY + " text, " +
"" + DEPARTMENT + " text, " +
"" + DESCRIPTION + " text);";
db.execSQL(sql);
Log.e("Database operations", "Require list Table created/opened");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = "DROP TABLE IF EXISTS " + TABLE_NAME;
db.execSQL(sql);
onCreate(db);
}
public Cursor select() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db
.query(TABLE_NAME, null, null, null, null, null, null);
return cursor;
}
public boolean login(String user_name,String password)
{
SQLiteDatabase sdb=this.getReadableDatabase();
String sql="select * from user_info where Username=? and Password=?";
Cursor cursor=sdb.rawQuery(sql, new String[]{user_name, password});
if(cursor.moveToFirst()==true){
cursor.close();
return true;
}
return false;
}
public Cursor getInformation(SQLiteDatabase db)
{
Cursor cursor;
String[] projection = {ID,NAME,TITLE,DEPARTMENT,CATEGORY};
cursor=db.query(TABLE_NAME,projection,null,null,null,null,null,null);
return cursor;
}
public Cursor cate(SQLiteDatabase db){
Cursor cursor;
String[] projection = {TYPE};
String selection = "Type=?";
***cursor= db.query(TABLE_NAME0,projection,selection,null,null,null,null);***
return cursor;
}
//‘ˆº”≤Ÿ◊˜
public long UserInfo_insert (String reporter_name,String reporter_email, String reporter_mobile, String user_name,String user_password,String user_type)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(NAME0,reporter_name);
cv.put(EMAIL0,reporter_email);
cv.put(MOBILE0,reporter_mobile);
cv.put(TYPE,user_type);
cv.put(USERNAME0,user_name);
cv.put(PASSWORD0,user_password);
long row = db.insert(TABLE_NAME0, null, cv);
return row;
}
public long insert(String name,String title,String description,String department, String category)
{
SQLiteDatabase db = this.getWritableDatabase();
/* ContentValues */ ContentValues cv = new ContentValues();
cv.put(NAME,name);
cv.put(TITLE, title);
cv.put(DEPARTMENT,department);
cv.put(CATEGORY,category);
cv.put(DESCRIPTION,description);
long row = db.insert(TABLE_NAME, null, cv);
return row;
}
//…æ≥˝≤Ÿ◊˜
public void delete(int id)
{
SQLiteDatabase db = this.getWritableDatabase();
String where = ID + " = ?";
String[] whereValue ={ Integer.toString(id) };
db.delete(TABLE_NAME, where, whereValue);
}
//–fi∏ƒ≤Ÿ◊˜
public void update(int id, String title,String name,String department, String category,String description)
{
SQLiteDatabase db = this.getWritableDatabase();
String where = ID + " = ?";
String[] whereValue = { Integer.toString(id) };
ContentValues cv = new ContentValues();
cv.put(NAME,name);
cv.put(TITLE, title);
cv.put(DESCRIPTION,description);
cv.put(DEPARTMENT,department);
cv.put(CATEGORY,category);
db.update(TABLE_NAME, cv, where, whereValue);
}
}
And LoginActivity.class
public class LoginActivity extends Activity implements View.OnClickListener {
private TextView TVToRegister;
private Button submit;
private EditText username;
private EditText code;
private DBHelper demodb;
private Cursor curosr;
private Context mContext;
SQLiteDatabase sqLiteDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
TVToRegister = (TextView) findViewById(R.id.tv_register);
TVToRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent ToRegister = new Intent(LoginActivity.this,RegisterActivity.class);
startActivity(ToRegister);
}
});
submit = (Button) findViewById(R.id.btn_submit);
submit.setOnClickListener(this);
username = (EditText) findViewById(R.id.edt_username);
code = (EditText) findViewById(R.id.edt_code);
demodb= new DBHelper(this);
sqLiteDatabase = demodb.getReadableDatabase();
}
@Override
public void onClick(View v) {
String UserName = username.getText().toString();
String Code = code.getText().toString();
if (username.getText().toString().equals("") || code.getText().toString().equals("")) {
Toast.makeText(getApplicationContext(), "user name and password can not be emprty!", Toast.LENGTH_SHORT).show();
} else {
boolean flag = demodb.login(UserName, Code);
if (flag) {
***if (demodb.cate(sqLiteDatabase).getString(3).equals("student"))*** {
Intent reporter = new Intent(LoginActivity.this, reporter_new_request.class);
startActivity(reporter);
}
if (demodb.cate(sqLiteDatabase).getString(3).equals("teacher")) {
Intent reparier1 = new Intent(LoginActivity.this, reparier.class);
startActivity(reparier1);
} else {
Log.i("TAG", "Joo");
Toast.makeText(LoginActivity.this, "JA", Toast.LENGTH_LONG).show();
Intent login = new Intent(LoginActivity.this, reporter.class);
startActivity(login);
}
} else {
Log.i("TAG", "Ei");
Toast.makeText(LoginActivity.this, "NEJ", Toast.LENGTH_LONG).show();
}
}
}
}
The wrong code is marked. Could anyone help me?
Aucun commentaire:
Enregistrer un commentaire