java.lang.NullPointerException Android develop

来源:互联网 发布:mac ai的zh cn汉化包 编辑:程序博客网 时间:2024/05/18 16:18
在用android studio开发app,现在用户注册登录系统连数据库,但是现在有问题,我想根据数据库里的用户表的不同用户类型,跳转不同activitiy,可是报错了,下面是代码
LoginActivity
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);
        curosr=demodb.select();

    }


    @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(0).equals("保修")){
                    Intent reporter = new Intent(LoginActivity.this,reporter_new_request.class);
                    startActivity(reporter);
                }
                if(demodb.cate(sqLiteDatabase).getString(0).equals("维修")){
                    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();
            }
        }

    }
}


DBHelper
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);
    }
    //修改操作
    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);
    }

报错说红色的那两句指向空指针错误。请问该如何解决
0 0
原创粉丝点击