簡體   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