单击按钮将数据保存到SQLite时,Android应用程序崩溃

时间:2015-02-05 12:02:11

标签: android sqlite

我有一个简单的应用程序,用户输入他们的详细信息并单击“保存”按钮,然后使用SQLite保存数据,但是当我单击保存按钮时,应用程序崩溃说“不幸的是应用程序已停止”。我认为我的SQL语句是正确的,我认为错误可能与按钮点击方法有关吗?我真的看不出我做错了什么。

MyDBHandler类

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

public class MyDBHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "detailsDB.db";
public static final String TABLE_DETAILS = "details";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_FIRSTNAME = "firstname";
public static final String COLUMN_SURNAME = "surname";
public static final String COLUMN_PHONE = "phone";
public static final String COLUMN_EMAIL = "email";
public static final String COLUMN_ADDRESS1 = "address1";
public static final String COLUMN_ADDRESS2 = "address2";

// Pass database information along to superclass
public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String query = " CREATE TABLE " + TABLE_DETAILS + "("
            + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + COLUMN_FIRSTNAME + " TEXT, "
            + COLUMN_SURNAME + " TEXT, "
            + COLUMN_PHONE + " TEXT, "
            + COLUMN_EMAIL + " TEXT, "
            + COLUMN_ADDRESS1 + " TEXT, "
            + COLUMN_ADDRESS2 + " TEXT "
            + ");";
    db.execSQL(query);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL(" DROP TABLE IF EXISTS " + TABLE_DETAILS);
    onCreate(db);
}

//Add a new row to the database
public void addDetails(Details details){
    ContentValues values = new ContentValues();
    values.put(COLUMN_FIRSTNAME, details.getFirstname());
    values.put(COLUMN_SURNAME, details.getSurname());
    values.put(COLUMN_PHONE, details.getPhone());
    values.put(COLUMN_EMAIL, details.getEmail());
    values.put(COLUMN_ADDRESS1, details.getAddress1());
    values.put(COLUMN_ADDRESS2, details.getAddress2());
    SQLiteDatabase db = getWritableDatabase();
    db.insert(TABLE_DETAILS, null, values);
    db.close();
}
}

详细信息类

public class Details {
int _id;
String firstname;
String surname;
String phone;
String email;
String address1;
String address2;

// Empty constructor
public Details(String s){
}

public void set_id(int _id) {
    this._id = _id;
}

public void setFirstname(String firstname) {
    this.firstname = firstname;
}

public void setSurname(String surname) {
    this.surname = surname;
}

public void setPhone(String phone) {
    this.phone = phone;
}

public void setEmail(String email) {
    this.email = email;
}

public void setAddress1(String address1) {
    this.address1 = address1;
}

public void setAddress2(String address2) {
    this.address2 = address2;
}







public int get_id() {
    return _id;
}

public String getFirstname() {
    return firstname;
}

public String getSurname() {
    return surname;
}

public String getPhone() {
    return phone;
}

public String getEmail() {
    return email;
}

public String getAddress1() {
    return address1;
}

public String getAddress2() {
    return address2;
}
}

MainActivity类

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

TextView firstName;
EditText editTextName;

TextView textView5;
EditText editTextSurname;

TextView textView4;
EditText editTextMobile;

TextView textView2;
EditText editTextEmail;

TextView textView3;
EditText editTextAddress1;

TextView textView6;
EditText editTextAddress2;

MyDBHandler dbHandler;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    firstName = (TextView) findViewById(R.id.firstName);
    editTextName = (EditText) findViewById(R.id.editTextName);
    textView5 = (TextView) findViewById(R.id.textView5);
    editTextSurname = (EditText) findViewById(R.id.editTextSurname);
    textView4 = (TextView) findViewById(R.id.textView4);
    editTextMobile = (EditText) findViewById(R.id.editTextMobile);
    textView2 = (TextView) findViewById(R.id.textView2);
    editTextEmail = (EditText) findViewById(R.id.editTextEmail);
    textView3 = (TextView) findViewById(R.id.textView3);
    editTextAddress1 = (EditText) findViewById(R.id.editTextAddress1);
    textView6 = (TextView) findViewById(R.id.textView6);
    editTextAddress2 = (EditText) findViewById(R.id.editTextAddress2);

    dbHandler = new MyDBHandler(this, null, null, 1);
    //printDatabase();
}


//Add details to the database
public void addButtonClicked(View view){
    Details details = new Details("");
    details.setFirstname(editTextName.getText().toString());
    details.setSurname(editTextSurname.getText().toString());
    details.setPhone(editTextMobile.getText().toString());
    details.setEmail(editTextEmail.getText().toString());
    details.setAddress1(editTextAddress1.getText().toString());
    details.setAddress2(editTextAddress2.getText().toString());
    dbHandler.addDetails(details);
    //printDatabase();
}


}

ActivityMain xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="600dp"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:theme="@style/AppTheme"
    android:touchscreenBlocksFocus="false">


<!-- First name -->

    <TextView
        android:id="@+id/firstName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/editTextName"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:text="@string/firstname"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editTextName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="5dp"
        android:layout_marginStart="82dp"
        android:layout_marginLeft="90dp"
        android:ems="10"
        android:paddingTop="25dp"
        android:inputType="text" >
    </EditText>



<!-- Surname -->

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/address2"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_marginTop="33dp"
        android:layout_below="@+id/editTextAddress1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <EditText
        android:id="@+id/editTextAddress2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="text"
        android:layout_alignTop="@+id/textView6"
        android:layout_alignLeft="@+id/editTextAddress1"
        android:layout_alignStart="@+id/editTextAddress1" />




<!-- Mobile Number -->

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/editTextSurname"
        android:layout_alignLeft="@+id/firstName"
        android:layout_alignStart="@+id/firstName"
        android:text="@string/surname"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editTextSurname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editTextMobile"
        android:layout_alignStart="@+id/editTextMobile"
        android:layout_below="@+id/editTextName"
        android:layout_marginTop="22dp"
        android:ems="10"
        android:inputType="text" />





<!-- Email Address -->


    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/editTextEmail"
        android:layout_alignLeft="@+id/firstName"
        android:layout_alignStart="@+id/firstName"
        android:text="@string/email"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:inputType="textEmailAddress" />

    <EditText
        android:id="@+id/editTextEmail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/editTextMobile"
        android:layout_alignLeft="@+id/editTextMobile"
        android:layout_below="@+id/editTextMobile"
        android:layout_marginTop="22dp"
        android:ems="10"
        android:inputType="textEmailAddress" />





<!-- Address 1 -->

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editTextAddress1"
        android:layout_alignBottom="@+id/editTextAddress1"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/editTextEmail"
        android:text="@string/address1"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editTextAddress1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/editTextName"
        android:layout_alignEnd="@+id/editTextName"
        android:layout_below="@+id/editTextEmail"
        android:layout_marginTop="30dp"
        android:ems="10"
        android:inputType="text" />




<!-- Address 2 -->

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/editTextEmail"
        android:layout_alignLeft="@+id/textView5"
        android:layout_alignStart="@+id/textView5"
        android:text="@string/phone"
        android:textAppearance="?android:attr/textAppearanceMedium" />


    <EditText
        android:id="@+id/editTextMobile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editTextName"
        android:layout_alignStart="@+id/editTextName"
        android:layout_below="@+id/editTextSurname"
        android:layout_marginTop="22dp"
        android:ems="10"
        android:inputType="phone">
    </EditText>




    <Button
        android:id="@+id/addButtonClicked"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="run"
        android:text="@string/save"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />


</RelativeLayout>
</ScrollView>

错误记录抱歉它有点凌乱

02-05 12:13:14.366  25875-25875/com.example.brian.organdonorapp E/AndroidRuntime﹕     FATAL EXCEPTION: main
Process: com.example.brian.organdonorapp, PID: 25875
java.lang.IllegalStateException: Could not find a method run(View) in the activity class com.example.brian.organdonorapp.MainActivity for onClick handler on view class android.widget.Button with id 'addButtonClicked'
        at android.view.View$1.onClick(View.java:3828)
        at android.view.View.performClick(View.java:4456)
        at android.view.View$PerformClick.run(View.java:18465)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5086)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NoSuchMethodException: run [class android.view.View]
        at java.lang.Class.getConstructorOrMethod(Class.java:472)
        at java.lang.Class.getMethod(Class.java:857)
        at android.view.View$1.onClick(View.java:3821)
        at android.view.View.performClick(View.java:4456)
        at android.view.View$PerformClick.run(View.java:18465)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5086)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
       at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:2)

android:onClick="run"

这使得框架在活动中查找方法run(View)但是没有。你可能想要

android:onClick="addButtonClicked"

答案 1 :(得分:0)

像我一样,你有一些困惑。您在xml中的按钮:

   <Button
    android:id="@+id/addButtonClicked"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="run"
    android:text="@string/save"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />

你在Activity中的方法命名为:addButtonClicked(View view);你刚刚想到了方法和id。此方法只需重命名为t:run(View view)或您必须将属性更改为android:onClick="addButtonClicked"