在sqlite数据库错误中插入数据

时间:2012-07-30 06:39:33

标签: android sqlite logcat

我试图从Android应用程序中的表单中获取答案。这是我使用的代码:

       package com.stage.sondage;



      import android.app.Activity;
      import android.content.Intent;
      import android.database.sqlite.SQLiteException;
      import android.os.Bundle;
      import android.view.View;
      import android.view.View.OnClickListener;
      import android.widget.Button;
      import android.widget.EditText;
      import android.widget.RadioButton;
      import android.widget.RadioGroup;
      import android.widget.Toast;


      public class Questionnaire extends Activity {



//Création d'une instance de ma classe SurveyAdapter
     SurveyAdapter survey=new SurveyAdapter(this);
     Appreciation appreciation=new Appreciation();

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.questions);


    // We get a reference on the radio-group widget
    RadioGroup mRadioGroup1 = ( RadioGroup )findViewById( R.appreciation.rep1 );
    RadioGroup mRadioGroup2 = ( RadioGroup )findViewById( R.appreciation.rep2 );
    RadioGroup mRadioGroup3 = ( RadioGroup )findViewById( R.appreciation.rep3 );
    RadioGroup mRadioGroup4 = ( RadioGroup )findViewById( R.appreciation.rep4 );
    RadioGroup mRadioGroup5 = ( RadioGroup )findViewById( R.appreciation.rep5 );

    RadioButton b1 = (RadioButton)findViewById(mRadioGroup1.getCheckedRadioButtonId());
    appreciation.setRep1((String) b1.getText());
    RadioButton b2 = (RadioButton)findViewById(mRadioGroup1.getCheckedRadioButtonId());
    appreciation.setRep1((String) b2.getText());
    RadioButton b3 = (RadioButton)findViewById(mRadioGroup1.getCheckedRadioButtonId());
    appreciation.setRep1((String) b3.getText());
    RadioButton b4 = (RadioButton)findViewById(mRadioGroup1.getCheckedRadioButtonId());
    appreciation.setRep1((String) b4.getText());
    RadioButton b5 = (RadioButton)findViewById(mRadioGroup1.getCheckedRadioButtonId());
    appreciation.setRep1((String) b5.getText());

    EditText zone = (EditText) findViewById(R.appreciation.zone);
    appreciation.setZone(zone.getText().toString());

    appreciation.setLatitude(0);
    appreciation.setLongitude(0);

    OnClickListener ButtonEnregistrer = new OnClickListener(){

        public void onClick(View actuelView) {
            //On ouvre la base de données pour écrire dedans
            survey.open();

            //On insère le livre que l'on vient de créer
            survey.insertAppreciation(appreciation);

            //on ferme la base de données
            survey.close();
            Intent intent = new Intent(Questionnaire.this,Questionnaire.class);
            startActivity(intent);
                }           
    };

    Button enregistrer = ( Button )findViewById( R.appreciation.enregistrer);
    enregistrer.setOnClickListener(ButtonEnregistrer);  

}       

}

这是调查适配器类的代码:

      package com.stage.sondage;

      import android.content.ContentValues;
      import android.content.Context;
      import android.database.Cursor;
      import android.database.sqlite.SQLiteDatabase;
      import android.database.sqlite.SQLiteDatabase.CursorFactory;


      public class SurveyAdapter {



        //nom de la table
  public static final String APPRECIATION_TABLE_NAME = "Appreciation";

  public static final String COLUMN_IDAPPRECIATION = "IDAPPRECIATIONL";
      public static final int NUM_COLUMN_IDAPPRECIATION = 0;
      public static final String COLUMN_ZONE = "ZONE";
  public static final int NUM_COLUMN_ZONE = 1;
  public static final String COLUMN_LATITUDE = "LATITUDE";
  public static final int NUM_COLUMN_LATITUDE = 2;
  public static final String COLUMN_LONGITUDE = "LONGITUDE";
  public static final int NUM_COLUMN_LONGITUDE = 3;
  public static final String COLUMN_REP1 = "REP1";
  public static final int NUM_COLUMN_REP1 = 4;
  public static final String COLUMN_REP2 = "REP2";
  public static final int NUM_COLUMN_REP2 = 5;
  public static final String COLUMN_REP3= "REP3";
  public static final int NUM_COLUMN_REP3 = 6;
  public static final String COLUMN_REP4= "REP4";
  public static final int NUM_COLUMN_REP4 = 7;
  public static final String COLUMN_REP5= "REP5";
  public static final int NUM_COLUMN_REP5 = 8;

  private SQLiteDatabase bdd;

  private AppreciationOpenHelper maBaseSQLite;

  public SurveyAdapter(Context context){
    //On créer la BDD et sa table
    maBaseSQLite = new AppreciationOpenHelper(context, null);
  }

  public void open(){
    //on ouvre la BDD en écriture
    bdd = maBaseSQLite.getWritableDatabase();
  }

  public void close(){
    //on ferme l'accès à la BDD
    bdd.close();
  }

  public SQLiteDatabase getBDD(){
    return bdd;
  }

  public long insertAppreciation(Appreciation app){
    //Création d'un ContentValues (fonctionne comme une HashMap)
    ContentValues values = new ContentValues();
    //on lui ajoute une valeur associé à une clé (qui est le nom de la colonne        dans laquelle on veut mettre la valeur)
    values.put(COLUMN_ZONE, app.getZone());
    values.put(COLUMN_LATITUDE, app.getLatitude());
    values.put(COLUMN_LONGITUDE, app.getLongitude());
    values.put(COLUMN_REP1, app.getRep1());
    values.put(COLUMN_REP2, app.getRep2());
    values.put(COLUMN_REP3, app.getRep3());
    values.put(COLUMN_REP4, app.getRep4());
    values.put(COLUMN_REP5, app.getRep5());



    //on insère l'objet dans la BDD via le ContentValues
    return bdd.insert(APPRECIATION_TABLE_NAME, null, values);
}




}

这是ApprecationOpenHelper类的代码:

     package com.stage.sondage;

     import android.content.Context;
     import android.database.sqlite.SQLiteDatabase;
     import android.database.sqlite.SQLiteOpenHelper;
     import android.database.sqlite.SQLiteDatabase.CursorFactory;

     public class AppreciationOpenHelper extends SQLiteOpenHelper {

 //version de la base de données
    private static final int DATABASE_VERSION = 1;
    //le chemin de la base de donnée
    private static String DB_PATH = "/data/data/com.stage.sondage/databases/";
    //nom de la base
    private static final String SURVEY_BASE_NAME="survey.db";
    //nom de la table
    public static final String APPRECIATION_TABLE_NAME = "Appreciation";
    // Description des colonnes
    public static final String COLUMN_IDAPPRECIATION = "IDAPPRECIATIONL";
    public static final String COLUMN_ZONE = "ZONE";
    public static final String COLUMN_LATITUDE = "LATITUDE";
    public static final String COLUMN_LONGITUDE = "LONGITUDE";
    public static final String COLUMN_REP1 = "REP1";
    public static final String COLUMN_REP2 = "REP2";
    public static final String COLUMN_REP3= "REP3";
    public static final String COLUMN_REP4= "REP4";
    public static final String COLUMN_REP5= "REP5";

    // Requête SQL pour la création da la base
    private static final String REQUETE_CREATION_BDD = "CREATE TABLE "+ APPRECIATION_TABLE_NAME + " (" + COLUMN_IDAPPRECIATION
            + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_ZONE
            + " TEXT NOT NULL, " +COLUMN_LATITUDE
            + " DOUBLE NOT NULL, " +COLUMN_LONGITUDE
            + " DOUBLE NOT NULL, " + COLUMN_REP1 + " TEXT NOT NULL, "
            + COLUMN_REP2 + " TEXT NOT NULL,"+COLUMN_REP3 + " TEXT NOT NULL,"
            +COLUMN_REP4+"TEXT NOT NULL,"+COLUMN_REP5 +" TEXT NOT NULL);";

    public AppreciationOpenHelper(Context context, CursorFactory factory) {
        super(context, SURVEY_BASE_NAME, factory, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
            }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL(REQUETE_CREATION_BDD);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        if (newVersion > DATABASE_VERSION) {
            db.execSQL("DROP TABLE " + APPRECIATION_TABLE_NAME + ";");
            onCreate(db);
        }


    }

        }

这是课程欣赏:               package com.stage.sondage;

           public class Appreciation {

        private int idappreciation;
        private String zone;
        private double latitude;
        private double longitude;
        private String rep1;
        private String rep2;
        private String rep3;
        private String rep4;
        private String rep5;

        public Appreciation(String zone, double latitude, double longitude, String rep1, String rep2, String rep3,String rep4,String rep5){

    this.zone=zone;
    this.latitude=latitude;
    this.longitude=longitude;
    this.rep1=rep1;
    this.rep2=rep2;
    this.rep3=rep3;
    this.rep4=rep4;
    this.rep5=rep5;     
    }
    public Appreciation() {
    // TODO Auto-generated constructor stub
        }
    public int getIdappreciation() {
    return idappreciation;
     }
    public void setIdappreciation(int idappreciation) {
    this.idappreciation = idappreciation;
     }
    public String getZone() {
    return zone;
     }
    public void setZone(String zone) {
    this.zone = zone;
     }
    public String getRep1() {
    return rep1;
      }
    public void setRep1(String rep1) {
    this.rep1 = rep1;
     }
    public String getRep2() {
    return rep2;
     }
    public void setRep2(String rep2) {
    this.rep2 = rep2;
     }
    public String getRep3() {
    return rep3;
     }
    public void setRep3(String rep3) {
    this.rep3 = rep3;
     }
    public String getRep4() {
    return rep4;
     }
    public void setRep4(String rep4) {
    this.rep4 = rep4;
     }
    public String getRep5() {
    return rep5;
     }
    public void setRep5(String rep5) {
    this.rep5 = rep5;
     }
    public double getLatitude() {
    return latitude;
     }
    public void setLatitude(double latitude) {
    this.latitude = latitude;
     }
    public double getLongitude() {
    return longitude;
     }
    public void setLongitude(double longitude) {
    this.longitude = longitude;
     }


            }

和finnaly这里是我在午餐时得到的logcat错误

    07-30 06:33:14.220: E/AndroidRuntime(656): Uncaught handler: thread main exiting due to uncaught exception
    07-30 06:33:14.300: E/AndroidRuntime(656): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.stage.sondage/com.stage.sondage.Questionnaire}: java.lang.NullPointerException
    07-30 06:33:14.300: E/AndroidRuntime(656):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
    07-30 06:33:14.300: E/AndroidRuntime(656):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
    07-30 06:33:14.300: E/AndroidRuntime(656):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
    07-30 06:33:14.300: E/AndroidRuntime(656):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
    07-30 06:33:14.300: E/AndroidRuntime(656):  at android.os.Handler.dispatchMessage(Handler.java:99)
    07-30 06:33:14.300: E/AndroidRuntime(656):  at android.os.Looper.loop(Looper.java:123)
    07-30 06:33:14.300: E/AndroidRuntime(656):  at android.app.ActivityThread.main(ActivityThread.java:4363)
    07-30 06:33:14.300: E/AndroidRuntime(656):  at java.lang.reflect.Method.invokeNative(Native Method)
    07-30 06:33:14.300: E/AndroidRuntime(656):  at java.lang.reflect.Method.invoke(Method.java:521)
    07-30 06:33:14.300: E/AndroidRuntime(656):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
    07-30 06:33:14.300: E/AndroidRuntime(656):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
    07-30 06:33:14.300: E/AndroidRuntime(656):  at dalvik.system.NativeStart.main(Native Method)
    07-30 06:33:14.300: E/AndroidRuntime(656): Caused by: java.lang.NullPointerException
    07-30 06:33:14.300: E/AndroidRuntime(656):  at com.stage.sondage.Questionnaire.onCreate(Questionnaire.java:54)
    07-30 06:33:14.300: E/AndroidRuntime(656):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    07-30 06:33:14.300: E/AndroidRuntime(656):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
    07-30 06:33:14.300: E/AndroidRuntime(656):  ... 11 more

谢谢你的帮助

2 个答案:

答案 0 :(得分:0)

错误在(com.stage.sondage.Questionnaire.onCreate(Questionnaire.java:54)),你能说明这是哪一行吗?

另外一件事,在以下部分mRadioGroup应该改变吗? mRadioGroup1,mRadioGroup2,..等:

RadioButton b1 = (RadioButton)findViewById(mRadioGroup1.getCheckedRadioButtonId());
appreciation.setRep1((String) b1.getText());
RadioButton b2 = (RadioButton)findViewById(mRadioGroup1.getCheckedRadioButtonId());
appreciation.setRep1((String) b2.getText());
RadioButton b3 = (RadioButton)findViewById(mRadioGroup1.getCheckedRadioButtonId());
appreciation.setRep1((String) b3.getText());
RadioButton b4 = (RadioButton)findViewById(mRadioGroup1.getCheckedRadioButtonId());
appreciation.setRep1((String) b4.getText());
RadioButton b5 = (RadioButton)findViewById(mRadioGroup1.getCheckedRadioButtonId());
appreciation.setRep1((String) b5.getText());

答案 1 :(得分:0)

      after seeing your log there is null pointer exception in below class

       **public class Questionnaire extends Activity**  ..at line nuber 53 or 54 
        you may to see that line your class and debugg and see which object value
         coming as null and fix it