繁体   English   中英

创建新实例SQLITE时出错

[英]Getting an error while creating new instance SQLITE

创建新实例以将数据写入数据库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);出错info entry = new info(this); 消息错误:“构造函数info(new View.OnClickListener(){})未定义”

当我更改为info 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();
       */     




        }
    });


    }


}

您必须初始化不带参数的info类:

info entry = new info();

顺便说一句 在类名中,通常将首字母大写为: Info

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

这里在类信息中 ,没有类型为Intro的 构造函数

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

public Info(Intro intro){
    }

否则,创建不带任何参数的构造函数。

 Info entry = new Info(this);

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM