在Android数据库中插入数据时出错

时间:2014-05-13 09:59:37

标签: java android button android-edittext android-sqlite

我正在尝试在数据库中插入一些数据。我有一个带有一些EditText和一个按钮的界面。我在editText中写了一些字符串,当我点击按钮时,我希望在数据库中插入这些数据。我尝试插入数据时出错。我告诉你我的代码。

界面的XML文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/black" >

    <EditText
        android:id="@+id/editTextNome"
        style="@style/EditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="19dp"
        android:ems="10"
        android:inputType="textPersonName" />

    <TextView
        android:id="@+id/textViewNome"
        style="@style/TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editTextNome"
        android:layout_alignBottom="@+id/editTextNome"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="43dp"
        android:text="@string/textViewNome" />

    <TextView
        android:id="@+id/textViewEta"
        style="@style/TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textViewSesso"
        android:layout_alignTop="@+id/editTextEta"
        android:text="@string/textViewEta" />

    <TextView
        android:id="@+id/textViewCF"
        style="@style/TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textViewEta"
        android:layout_alignTop="@+id/editTextCF"
        android:text="@string/textViewCF" />

    <TextView
        android:id="@+id/textViewCitta"
        style="@style/TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textViewCF"
        android:layout_alignTop="@+id/editTextCitta"
        android:text="@string/textViewCitta" />

    <TextView
        android:id="@+id/textViewIndirizzo"
        style="@style/TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textViewCitta"
        android:layout_alignTop="@+id/editTextIndirizzo"
        android:text="@string/textViewIndirizzo" />

    <TextView
        android:id="@+id/textViewTelefono"
        style="@style/TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textViewIndirizzo"
        android:layout_alignTop="@+id/editTextTelefono"
        android:text="@string/textViewTelefono" />

    <Button
        android:id="@+id/buttonAggiorna"
        style="@style/Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editTextTelefono"
        android:layout_below="@+id/editTextTelefono"
        android:layout_marginTop="57dp"
        android:text="@string/buttonAggiorna" />

    <EditText
        android:id="@+id/editTextEta"
        style="@style/EditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editTextNome"
        android:layout_below="@+id/radioGroup1"
        android:layout_marginTop="24dp"
        android:ems="10" />

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editTextEta"
        android:layout_alignRight="@+id/editTextCognome"
        android:layout_below="@+id/editTextCognome"
        android:layout_marginTop="18dp"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/radioButtonM"
            style="@style/Radio"
            android:layout_width="89dp"
            android:layout_height="wrap_content"
            android:text="@string/RadioButtonM" />

        <RadioButton
            android:id="@+id/radioButtonF"
            style="@style/Radio"
            android:layout_width="121dp"
            android:layout_height="wrap_content"
            android:text="@string/RadioButtonF" />

    </RadioGroup>

    <TextView
        android:id="@+id/textViewSesso"
        style="@style/TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textViewNome"
        android:layout_alignTop="@+id/radioGroup1"
        android:text="@string/textViewSesso" />

    <TextView
        android:id="@+id/textViewCognome"
        style="@style/TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textViewIndirizzo"
        android:layout_below="@+id/editTextNome"
        android:layout_marginTop="26dp"
        android:text="@string/textViewCognome" />

    <EditText
        android:id="@+id/editTextCognome"
        style="@style/EditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editTextNome"
        android:layout_alignTop="@+id/textViewCognome"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/editTextCF"
        style="@style/EditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editTextEta"
        android:layout_below="@+id/editTextEta"
        android:layout_marginTop="22dp"
        android:ems="10" />

    <EditText
        android:id="@+id/editTextCitta"
        style="@style/EditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editTextCF"
        android:layout_below="@+id/editTextCF"
        android:layout_marginTop="21dp"
        android:ems="10" />

    <EditText
        android:id="@+id/editTextIndirizzo"
        style="@style/EditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editTextCitta"
        android:layout_below="@+id/editTextCitta"
        android:layout_marginTop="17dp"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editTextTelefono"
        style="@style/EditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editTextIndirizzo"
        android:layout_below="@+id/editTextIndirizzo"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:inputType="phone" />

</RelativeLayout>

接口的java文件(只看onClick事件):

package com.example.prenotazione_esame;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.*;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class Profilo extends Activity {


    private LoginDataBase dbLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int id = getIntent().getExtras().getInt("id");
        setContentView(R.layout.profilo);
        LoginDataBase dbLogin = new LoginDataBase(this);
        SQLiteDatabase db = dbLogin.getWritableDatabase();
        String query = " SELECT * FROM T_PROFILO WHERE _id_L=?";
        String id2 = String.valueOf(id);
        String[] selectionArgs = {id2};
        Cursor cursor = db.rawQuery(query, selectionArgs);

        if(cursor.moveToFirst()){
            EditText Nome = (EditText) findViewById(R.id.editTextNome);
            String nome2 = cursor.getString(1);
            Nome.setText(nome2);

            EditText Cognome = (EditText) findViewById(R.id.editTextCognome);
            String cognome2 = cursor.getString(2);
            Cognome.setText(cognome2);

            RadioButton RadioM = (RadioButton) findViewById(R.id.radioButtonM);
            RadioButton RadioF = (RadioButton) findViewById(R.id.radioButtonF);
            String sesso = cursor.getString(3);
            if(sesso.equalsIgnoreCase("M")){
                RadioM.setChecked(true);
                RadioF.setChecked(false);
            }
            else{
                RadioM.setChecked(false);
                RadioF.setChecked(true);
            }

            EditText Età = (EditText) findViewById(R.id.editTextEta);
            String età2 = cursor.getString(4);
            Età.setText(età2);

            EditText CF = (EditText) findViewById(R.id.editTextCF);
            String CF2 = cursor.getString(5);
            CF.setText(CF2);

            EditText Città = (EditText) findViewById(R.id.editTextCitta);
            String città2 = cursor.getString(6);
            Città.setText(città2);

            EditText Indirizzo = (EditText) findViewById(R.id.editTextIndirizzo);
            String indirizzo2 = cursor.getString(7);
            Indirizzo.setText(indirizzo2);

            EditText Telefono = (EditText) findViewById(R.id.editTextTelefono);
            String telefono2 = cursor.getString(8);
            Telefono.setText(telefono2);
        }
        cursor.close();
        Button button_aggiorna = (Button) findViewById(R.id.buttonAggiorna);
        button_aggiorna.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                insertprofilo();
            }
        });

    }
    private void insertprofilo(){
        int id = getIntent().getExtras().getInt("id");
        ContentValues values = new ContentValues();

        EditText Nome = (EditText) findViewById(R.id.editTextNome);
        values.put("NOME", Nome.getText().toString());

        EditText Cognome = (EditText) findViewById(R.id.editTextCognome);
        values.put("COGNOME", Cognome.getText().toString());

        RadioGroup radiogroup = (RadioGroup) findViewById(R.id.radioGroup1);
        int radio_id = radiogroup.getCheckedRadioButtonId();
        RadioButton radio = (RadioButton) findViewById(radio_id);
        String sesso = radio.getText().toString();
        values.put("CITTA", sesso);

        EditText Età = (EditText) findViewById(R.id.editTextEta);
        values.put("ETA", Età.getText().toString());

        EditText CF = (EditText) findViewById(R.id.editTextCF);
        values.put("CF", CF.getText().toString());

        EditText Città = (EditText) findViewById(R.id.editTextCitta);
        values.put("CITTA", Città.getText().toString());

        EditText Indirizzo = (EditText) findViewById(R.id.editTextIndirizzo);
        values.put("INDIRIZZO", Indirizzo.getText().toString());

        EditText Telefono = (EditText) findViewById(R.id.editTextTelefono);
        values.put("TELEFONO", Telefono.getText().toString());

        values.put("_id_L", id); //Foreign key

        SQLiteDatabase db = dbLogin.getWritableDatabase();
        long id2 = db.insert("T_LOGIN", null, values);
    }
}

最后是我的数据库:

package com.example.prenotazione_esame;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class LoginDataBase extends SQLiteOpenHelper {
    private static final String DB_NAME="Login_DB";
    private static final int DB_VERSION=1;

    public LoginDataBase(Context context){
        super(context,DB_NAME,null,DB_VERSION);
    }
    @Override 
     public void onCreate(SQLiteDatabase db) { 
        String sql="";
        sql+= "CREATE TABLE T_LOGIN (";
        sql+= " _id_LOGIN INTEGER PRIMARY KEY AUTOINCREMENT,";
        sql+= " USERNAME TEXT NOT NULL,";
        sql+= " PASSWORD TEXT NOT NULL";
        sql+=")";
        db.execSQL(sql);

        String sql2="";
        sql2+= "CREATE TABLE T_PROFILO (";
        sql2+= " _id_PROFILO INTEGER PRIMARY KEY AUTOINCREMENT,";
        sql2+= " NOME TEXT NOT NULL,";
        sql2+= " COGNOME TEXT NOT NULL,";
        sql2+= " ETA TEXT NOT NULL,";
        sql2+= " SESSO TEXT(1) NOT NULL,";
        sql2+= " CODICE_FISCALE TEXT NOT NULL,";
        sql2+= " CITTA TEXT NOT NULL,";
        sql2+= " INDIRIZZO TEXT NOT NULL,";
        sql2+= " TELEFONO TEXT NOT NULL,";
        sql2+= " _id_L INTEGER NOT NULL,";
        sql2+= " FOREIGN KEY(_id_L) REFERENCES T_LOGIN(_id_LOGIN)";
        sql2+= ")";
        db.execSQL(sql2);
     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
        // Aggiornamento delle tabelle 
     } 
}

我的logcat显示了这个问题:

05-13 09:48:21.899: E/AndroidRuntime(324): FATAL EXCEPTION: main
05-13 09:48:21.899: E/AndroidRuntime(324): java.lang.NullPointerException
05-13 09:48:21.899: E/AndroidRuntime(324):  at com.example.prenotazione_esame.Profilo.insertprofilo(Profilo.java:122)
05-13 09:48:21.899: E/AndroidRuntime(324):  at com.example.prenotazione_esame.Profilo.access$0(Profilo.java:89)
05-13 09:48:21.899: E/AndroidRuntime(324):  at com.example.prenotazione_esame.Profilo$1.onClick(Profilo.java:84)
05-13 09:48:21.899: E/AndroidRuntime(324):  at android.view.View.performClick(View.java:2408)
05-13 09:48:21.899: E/AndroidRuntime(324):  at android.view.View$PerformClick.run(View.java:8816)
05-13 09:48:21.899: E/AndroidRuntime(324):  at android.os.Handler.handleCallback(Handler.java:587)
05-13 09:48:21.899: E/AndroidRuntime(324):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-13 09:48:21.899: E/AndroidRuntime(324):  at android.os.Looper.loop(Looper.java:123)
05-13 09:48:21.899: E/AndroidRuntime(324):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-13 09:48:21.899: E/AndroidRuntime(324):  at java.lang.reflect.Method.invokeNative(Native Method)
05-13 09:48:21.899: E/AndroidRuntime(324):  at java.lang.reflect.Method.invoke(Method.java:521)
05-13 09:48:21.899: E/AndroidRuntime(324):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-13 09:48:21.899: E/AndroidRuntime(324):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-13 09:48:21.899: E/AndroidRuntime(324):  at dalvik.system.NativeStart.main(Native Method)
05-13 09:48:25.059: I/Process(324): Sending signal. PID: 324 SIG: 9

2 个答案:

答案 0 :(得分:1)

首先,您已为Database

声明了变量
 private LoginDataBase dbLogin;

所以不需要像

那样再次声明它
 LoginDataBase dbLogin = new LoginDataBase(this);

因此您需要将其更改为

 dbLogin = new LoginDataBase(this);

最后检查密钥"id"是否与您发送密码的位置匹配。

 int id = getIntent().getExtras().getInt("id");

答案 1 :(得分:0)

onCreate()中的问题 第4行

LoginDataBase dbLogin = new LoginDataBase(this);
        SQLiteDatabase db = dbLogin.getWritableDatabase();

这里你将dbLogin初始化为局部变量,但你正在接受它

private LoginDataBase dbLogin;

类成员并在

中使用它
SQLiteDatabase db = dbLogin.getWritableDatabase();
        long id2 = db.insert("T_LOGIN", null, values);

所以这里空指针异常。 请替换

 LoginDataBase dbLogin = new LoginDataBase(this);
            SQLiteDatabase db = dbLogin.getWritableDatabase();

通过

            dbLogin = new LoginDataBase(this);
            SQLiteDatabase db = dbLogin.getWritableDatabase();