0

我在创建用于将数据写入数据库 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); 消息错误:“构造函数信息(new 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();
       */     




        }
    });


    }


}
4

2 回答 2

1

您必须在没有参数的情况下初始化 info 类:

info entry = new info();

顺便提一句。类名中第一个字母大写是很常见的:Info

此外。如果您的应用程序中没有更多代码,则会在此处info抛出NullPointerExceptionourDatabase.insert(DATABASE_TABLE, null, cv);

于 2013-08-15T14:28:30.500 回答
0

在 Class Info中,没有带有Intro类型参数的构造函数

如果您确实需要在 Class Info 中拥有类 Intro 的实例,请创建一个。

public Info(Intro intro){
    }

否则创建一个没有传递任何参数的构造函数。

 Info entry = new Info(this);

始终尝试遵循命名约定,这不是以您喜欢的任何方式进行编程的好方法。http://www.oracle.com/technetwork/java/codeconv-138413.html

于 2013-08-15T14:58:17.450 回答