在创建用于将数据写入数据库SQLlite
的新实例时出错这是我的数据库类的代码
package com.example.tttt;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class info {
public static final String key_nom = "nom_personne";
public static final String key_id = "id_personne";
public static final String key_prenom = "prenom_personne";
private static final String DATABASE_NAME = "infodb";
private static final String DATABASE_TABLE = "infotable";
private static final int DATABASE_VERSION = 1 ;
private DbHelper ourHelper;
private Context ourContext ;
private SQLiteDatabase ourDatabase ;
private static class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, DATABASE_NAME,null , DATABASE_VERSION );
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL( "CREATE TABLE " + DATABASE_TABLE + " (" +
key_id + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
key_nom + " TEXT NOT NULL, " +
key_prenom + " TEXT NOT NULL);"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public info open() throws SQLException{
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
public long createEntry(String name, String prenom) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(key_nom, name);
cv.put(key_prenom, prenom);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
在我写的课堂活动中
info entry = new info(this);
entry.open();
entry.createEntry(name , prenom );
entry.close();
我在info entry = new info(this);
上收到错误
消息错误:“构造函数信息(新的View.OnClickListener(){})未定义”
当我更改为信息entry = new info(intro.this);
我得到了:“构造函数信息(简介)未定义”
完整的介绍活动,我尝试从用户那里获取信息并将其发送到数据库
package com.example.tttt;
import com.example.tttt.R.layout;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.DropBoxManager.Entry;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class intro extends Activity {
Button sqlupdate,sqlview,sqlsearch;
EditText sqlnom, sqlprenom,sqltextsearch ;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.intro);
sqlnom = (EditText)findViewById(R.id.nomenter);
sqlprenom = (EditText)findViewById(R.id.prenomenter);
sqlupdate = (Button) findViewById(R.id.button1);
sqlview = (Button) findViewById(R.id.button2);
// sqlsearch = (Button) findViewById(R.id.button3);
// sqltextsearch = (EditText)findViewById(R.id.editText1);
sqlupdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
boolean diditwork = true ;
try{
String name = sqlnom.getText().toString();
String prenom = sqlprenom.getText().toString();
entry = new info();
entry.open();
entry.createEntry(name , prenom );
entry.close();
}catch (Exception e ){
diditwork = false ;
String error = e.toString();
Dialog d = new Dialog(intro.this);
d.setTitle(" merde ");
TextView tv = new TextView(intro.this);
tv.setText(error);
d.setContentView(tv);
d.show();
}finally{
if (diditwork){
Dialog d = new Dialog(intro.this);
d.setTitle(" okkk ");
TextView tv = new TextView(intro.this);
tv.setText(" ok yes ");
d.setContentView(tv);
d.show();
}
}
}
});
sqlview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent open = new Intent("android.intent.action.SQLVIEW");
startActivity(open);
}
});
sqlsearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
/*String s = sqltextsearch.getText().toString();
info se = new info();
se.open();
String returne = se.search();
*/
}
});
}
}
答案 0 :(得分:1)
你必须初始化没有参数的info类:
info entry = new info();
顺便说一句。在类名中通常写第一个字母大写:Info
此外。如果您在info
中没有更多代码,那么您的应用会在NullPointerException
点ourDatabase.insert(DATABASE_TABLE, null, cv);
答案 1 :(得分:0)
此类信息中没有构造函数,其参数类型为简介
如果你真的需要在Class Info中拥有类Intro的实例,请创建一个。
public Info(Intro intro){
}
否则在没有传递任何参数的情况下创建构造函数。
Info entry = new Info(this);
始终尝试遵循命名约定,它不是以您喜欢的任何方式编程的好方法。 http://www.oracle.com/technetwork/java/codeconv-138413.html