"申请被停止......"在SQLiteOpenHelper创建期间

时间:2015-09-24 07:11:00

标签: android sqlite

当我想要开始一个新的活动时,我遇到了一个问题,它包含对扩展SQLiteOpenHelper的对象的引用。

这就是我开展活动的方式:

// Listener for button
class LoginButtonListener implements View.OnClickListener {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(v.getContext(), MasterUserActivity.class);
        v.getContext().startActivity(intent);

    }
}

我的新活动:

// I will be implementing ListView in here
public class MasterUserActivity extends ListActivity  {

    private DBHandler dbHandler = new DBHandler(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.master_user);

        Toast toast = Toast.makeText(MasterUserActivity.this, "It works :)", Toast.LENGTH_LONG);
        toast.show();

    }

}

我的DBHandler课程:

public class DBHandler extends SQLiteOpenHelper {

    public DBHandler(Context contex) {
        super(contex, "bankapplication.db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase database) {
        String query = "CREATE TABLE users ( login TEXT, password TEXT, name TEXT, "
                + "phone TEXT, country TEXT, balance TEXT)";
        database.execSQL(query);
    }

    @Override
    public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
        String query = "DROP TABLE IF EXISTS users";
        database.execSQL(query);
        onCreate(database);
    }
}

我在评论private DBHandler dbHandler = new DBHandler(this);

时工作正常

这里有什么问题?

1 个答案:

答案 0 :(得分:2)

此行导致错误:

private DBHandler dbHandler = new DBHandler(this);

在声明变量时不能调用new,而是在活动的onCreate方法中初始化。