如何解决活动类中的“无法归因”错误?

时间:2019-03-28 16:08:23

标签: java android

我正在SQLite上建立数据库,但是在Main Activity中添加数据库时出错。我确定我在建立数据库方面做得很好。

试图更改活动名称-未更改。尝试对getActivity()进行更改-出现了新错误。

主要活动中的代码(有错误的部分):

private AutoCompleteTextView mEmailView;
private EditText mPasswordView;
private View mProgressView;
private View mLoginFormView;

DBHelper dbHelper;

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

    mEmailView = (AutoCompleteTextView) findViewById(R.id.email);
    populateAutoComplete();

    mPasswordView = (EditText) findViewById(R.id.password);
    mPasswordView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
            if (id == EditorInfo.IME_ACTION_DONE || id == EditorInfo.IME_NULL) {
                attemptLogin();
                return true;
            }
            return false;
        }
    });

    Button mEmailSignInButton = (Button) findViewById(R.id.email_sign_in_button);
    mEmailSignInButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            attemptLogin();
        }
    });

    mLoginFormView = findViewById(R.id.login_form);

    mProgressView = findViewById(R.id.login_progress);
    dbHelper = new DBHelper(this);
}

...

数据库代码(在我的项目中称为DBHelper):

package com.gov.work;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {

    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "contactDb";
    public static final String TABLE_CONTACTS = "contacts";

    public static final String KEY_ID = "_id";
    public static final String KEY_MAIL = "mail";
    public static final String KEY_PASSWORD = "password";

    public DBHelper(Context context, String name,  int version) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + TABLE_CONTACTS + "(" + KEY_ID + " integer primary key,"+ KEY_MAIL + " text," + KEY_PASSWORD + " text" + ")");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("drop table if exists " + TABLE_CONTACTS);

        onCreate(db);
    }
}

当我尝试运行项目时,它给了我编译错误。

2 个答案:

答案 0 :(得分:0)

您可以使用getActivity()代替

dbHelper = new DBHelper(this);

dbHelper = new DBHelper(getActivity());

答案 1 :(得分:0)

这是因为您尝试使用以下方法实例化DBHelper

dbHelper = new DBHelper(this);

但是您的DBHelper中没有匹配的构造函数。您只有以下构造函数:

public DBHelper(Context context, String name,  int version) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

因此,您需要使用DBHelper作为其唯一参数来向Context添加一个构造函数,以解决您的问题,如下所示:

public DBHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}