我正在创建一个名为Mycontacts的应用程序。与youtube(https://www.youtube.com/watch?v=38DOncHIazs)上的视频相同。

但这getWritableDatabase()导致错误


  非静态方法'getWritableDatabase()'不能从静态上下文中引用


public class addcontacts extends AppCompactActivity
{

    EditText conname, conmobno, conemail;
    Context context = this;
    userDbHelper userdbhelp;
    SQLiteDatabase sqLiteDatabase;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addcontacts);
        conname = (EditText) findViewById(R.id.Name);
        conmobno = (EditText) findViewById(R.id.Contno);
        conemail = (EditText) findViewById(R.id.Eid);
    }

    public void addContest(View view)  {
        String name = conname.getText().toString();
        String mobno = conmobno.getText().toString();
        String conem = conemail.getText().toString();
        userdbhelp = new userDbHelper(context);
        sqLiteDatabase = userDbHelper.getWritableDatabase();
        userdbhelp.addinformation(name,mobno,conem, sqLiteDatabase);
        Toast.makeText(getBaseContext(),"data saved",Toast.LENGTH_SHORT).show();
        userdbhelp.close();
    }
}

最佳答案

更改

userdbhelp = new userDbHelper(context);
sqLiteDatabase = userDbHelper.getWritableDatabase();




userdbhelp = new userDbHelper(context);
sqLiteDatabase = userdbhelp.getWritableDatabase();


您需要在实例userdbhelp上而不是在类userDbHelper上调用该方法。

10-01 23:56