按下按钮在数据库中插入数据时出错

时间:2014-05-13 10:11:07

标签: android database

这是我在Androin的第一个应用程序,我不明白错误在哪里!当按下按钮在数据库中插入数据时,应用程序意外停止并且logcat为:

05-13 11:15:12.684: E/AndroidRuntime(15566): FATAL EXCEPTION: main
05-13 11:15:12.684: E/AndroidRuntime(15566): Process: com.n2d.survey, PID: 15566
05-13 11:15:12.684: E/AndroidRuntime(15566): java.lang.IllegalStateException: Could not find a method  Adddata(View) in the activity class com.n2d.survey.MainActivity for onClick handler on view class android.widget.Button with id 'button1'
05-13 11:15:12.684: E/AndroidRuntime(15566):    at android.view.View$1.onClick(View.java:3852)
05-13 11:15:12.684: E/AndroidRuntime(15566):    at android.view.View.performClick(View.java:4480)
05-13 11:15:12.684: E/AndroidRuntime(15566):    at android.view.View$PerformClick.run(View.java:18673)
05-13 11:15:12.684: E/AndroidRuntime(15566):    at android.os.Handler.handleCallback(Handler.java:733)
05-13 11:15:12.684: E/AndroidRuntime(15566):    at android.os.Handler.dispatchMessage(Handler.java:95)
05-13 11:15:12.684: E/AndroidRuntime(15566):    at android.os.Looper.loop(Looper.java:157)
05-13 11:15:12.684: E/AndroidRuntime(15566):    at android.app.ActivityThread.main(ActivityThread.java:5872)
05-13 11:15:12.684: E/AndroidRuntime(15566):    at java.lang.reflect.Method.invokeNative(Native Method)
05-13 11:15:12.684: E/AndroidRuntime(15566):    at java.lang.reflect.Method.invoke(Method.java:515)
05-13 11:15:12.684: E/AndroidRuntime(15566):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1069)
05-13 11:15:12.684: E/AndroidRuntime(15566):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:885)
05-13 11:15:12.684: E/AndroidRuntime(15566):    at dalvik.system.NativeStart.main(Native Method)
05-13 11:15:12.684: E/AndroidRuntime(15566): Caused by: java.lang.NoSuchMethodException:  Adddata [class android.view.View]
05-13 11:15:12.684: E/AndroidRuntime(15566):    at java.lang.Class.getConstructorOrMethod(Class.java:472)
05-13 11:15:12.684: E/AndroidRuntime(15566):    at java.lang.Class.getMethod(Class.java:857)
05-13 11:15:12.684: E/AndroidRuntime(15566):    at android.view.View$1.onClick(View.java:3845)
05-13 11:15:12.684: E/AndroidRuntime(15566):    ... 11 more

这是我的活动:

public class MainActivity extends Activity  {
    String zone,adress,number,families;
    TableRow tableRow;
    TextView textView,textView1,textView2,textView3,textView4,textView5,textView6,textView7;
    SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        db=openOrCreateDatabase("Survey",MODE_PRIVATE, null);
        db.execSQL("CREATE TABLE IF NOT EXISTS Rilevamenti(zone VARCHAR,adress VARCHAR,number VARCHAR,families VARCHAR);");

    }

public void Adddata(View view) {
    //riferimento alla TextEdit
    EditText edittext1=(EditText )findViewById(R.id.zone);
    EditText edittext2=(EditText )findViewById(R.id.adress);
    EditText edittext3=(EditText )findViewById(R.id.number);
    EditText edittext4=(EditText )findViewById(R.id.families);
    zone=edittext1.getText().toString();
    adress=edittext2.getText().toString();
    number=edittext3.getText().toString();
    families=edittext4.getText().toString();
    db.execSQL("INSERT INTO Rilevamenti VALUES('"+zone+"','"+adress+"','"+number+"'+'"+families+"');");
}



public void Showdata(View view)
{
    Cursor c=db.rawQuery("SELECT * from Rilevamenti", null);
    int count= c.getCount();
    c.moveToFirst();
    TableLayout tableLayout = new TableLayout(getApplicationContext());
    tableLayout.setVerticalScrollBarEnabled(true);

    tableRow = new TableRow(getApplicationContext());
    textView = new TextView(getApplicationContext());
    textView.setText("Zona");
    textView.setTextColor(Color.RED);
    textView.setTypeface(null, Typeface.BOLD);
    textView.setPadding(20, 20, 20, 20);
    tableRow.addView(textView);

    textView5 = new TextView(getApplicationContext());
    textView5.setText("Indirizzo");
    textView5.setTextColor(Color.RED);
    textView5.setTypeface(null, Typeface.BOLD);
    textView5.setPadding(20, 20, 20, 20);
    tableRow.addView(textView5);

    textView6 = new TextView(getApplicationContext());
    textView6.setText("Numero civico");
    textView6.setTextColor(Color.RED);
    textView6.setTypeface(null, Typeface.BOLD);
    textView6.setPadding(20, 20, 20, 20);
    tableRow.addView(textView6);

    textView7 = new TextView(getApplicationContext());
    textView7.setText("Numero di famiglie");
    textView7.setTextColor(Color.RED);
    textView7.setTypeface(null, Typeface.BOLD);
    textView7.setPadding(20, 20, 20, 20);
    tableRow.addView(textView7);

    tableLayout.addView(tableRow);
        for (Integer j = 0; j < count; j++)
        {
            tableRow = new TableRow(getApplicationContext());
            textView1 = new TextView(getApplicationContext());
            textView1.setText(c.getString(c.getColumnIndex("zone")));
            textView2 = new TextView(getApplicationContext());
            textView2.setText(c.getString(c.getColumnIndex("adress")));
            textView3 = new TextView(getApplicationContext());
            textView3.setText(c.getString(c.getColumnIndex("number")));
            textView4 = new TextView(getApplicationContext());
            textView4.setText(c.getString(c.getColumnIndex("families")));

            textView1.setPadding(20,20,20,20);
            textView2.setPadding(20,20,20,20);
            textView3.setPadding(20,20,20,20);
            textView4.setPadding(20,20,20,20);

            tableRow.addView(textView1);
            tableRow.addView(textView2);
            tableRow.addView(textView3);
            tableRow.addView(textView4);

            tableLayout.addView(tableRow);
            c.moveToNext();

        }
        setContentView(tableLayout);
db.close();
}

//funzione per uscire App
public void close(View view)
{
    System.exit(0);
}
}

和我的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.n2d.survey.MainActivity$PlaceholderFragment" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="zona" />

    <EditText
        android:id="@+id/zone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/zone"
        android:layout_marginTop="16dp"
        android:text="Ti rovi in (via/piazza/...)" />

    <EditText
        android:id="@+id/adress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:ems="10" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/adress"
        android:layout_below="@+id/adress"
        android:layout_marginTop="14dp"
        android:text="Numero civico" />

    <EditText
        android:id="@+id/number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_below="@+id/textView3"
        android:ems="10" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/number"
        android:layout_centerVertical="true"
        android:text="Numero di famiglie" />

    <EditText
        android:id="@+id/families"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView4"
        android:layout_below="@+id/textView4"
        android:ems="10"
        android:inputType="number" />

    <Button
        android:id="@+id/bottone_crea"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView3"
        android:layout_below="@+id/families"
        android:layout_marginTop="28dp"
        android:onClick="Adddata"
        android:text="Crea" />

    <Button
        android:id="@+id/bottone_mostra"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="25dp"
        android:onClick="Showdata"
        android:text="Mostra tabella" />

    <Button
        android:id="@+id/bottone_chiudi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/bottone_crea"
        android:layout_alignBottom="@+id/bottone_crea"
        android:layout_marginLeft="23dp"
        android:layout_toRightOf="@+id/textView2"
        android:onClick="close"
        android:text="Chiudi" />

</RelativeLayout>

这是清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.n2d.survey"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.n2d.survey.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

现在logcat说:

05-13 12:44:06.315: E/SQLiteLog(25213): (1) table Rilevamenti has 4 columns but 3 values were supplied
05-13 12:44:06.325: W/dalvikvm(25213): threadid=1: thread exiting with uncaught exception (group=0x416dee18)
05-13 12:44:06.335: E/AndroidRuntime(25213): FATAL EXCEPTION: main
05-13 12:44:06.335: E/AndroidRuntime(25213): Process: com.n2d.survey, PID: 25213
05-13 12:44:06.335: E/AndroidRuntime(25213): java.lang.IllegalStateException: Could not execute method of the activity
05-13 12:44:06.335: E/AndroidRuntime(25213):    at android.view.View$1.onClick(View.java:3865)
05-13 12:44:06.335: E/AndroidRuntime(25213):    at android.view.View.performClick(View.java:4480)
05-13 12:44:06.335: E/AndroidRuntime(25213):    at android.view.View$PerformClick.run(View.java:18673)
05-13 12:44:06.335: E/AndroidRuntime(25213):    at android.os.Handler.handleCallback(Handler.java:733)
05-13 12:44:06.335: E/AndroidRuntime(25213):    at android.os.Handler.dispatchMessage(Handler.java:95)
05-13 12:44:06.335: E/AndroidRuntime(25213):    at android.os.Looper.loop(Looper.java:157)
05-13 12:44:06.335: E/AndroidRuntime(25213):    at android.app.ActivityThread.main(ActivityThread.java:5872)
05-13 12:44:06.335: E/AndroidRuntime(25213):    at java.lang.reflect.Method.invokeNative(Native Method)
05-13 12:44:06.335: E/AndroidRuntime(25213):    at java.lang.reflect.Method.invoke(Method.java:515)
05-13 12:44:06.335: E/AndroidRuntime(25213):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1069)
05-13 12:44:06.335: E/AndroidRuntime(25213):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:885)
05-13 12:44:06.335: E/AndroidRuntime(25213):    at dalvik.system.NativeStart.main(Native Method)
05-13 12:44:06.335: E/AndroidRuntime(25213): Caused by: java.lang.reflect.InvocationTargetException
05-13 12:44:06.335: E/AndroidRuntime(25213):    at java.lang.reflect.Method.invokeNative(Native Method)
05-13 12:44:06.335: E/AndroidRuntime(25213):    at java.lang.reflect.Method.invoke(Method.java:515)
05-13 12:44:06.335: E/AndroidRuntime(25213):    at android.view.View$1.onClick(View.java:3860)
05-13 12:44:06.335: E/AndroidRuntime(25213):    ... 11 more
05-13 12:44:06.335: E/AndroidRuntime(25213): Caused by: android.database.sqlite.SQLiteException: table Rilevamenti has 4 columns but 3 values were supplied (code 1): , while compiling: INSERT INTO Rilevamenti VALUES('zbz','',''+'');
05-13 12:44:06.335: E/AndroidRuntime(25213):    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
05-13 12:44:06.335: E/AndroidRuntime(25213):    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:917)
05-13 12:44:06.335: E/AndroidRuntime(25213):    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:528)
05-13 12:44:06.335: E/AndroidRuntime(25213):    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
05-13 12:44:06.335: E/AndroidRuntime(25213):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
05-13 12:44:06.335: E/AndroidRuntime(25213):    at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
05-13 12:44:06.335: E/AndroidRuntime(25213):    at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1728)
05-13 12:44:06.335: E/AndroidRuntime(25213):    at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1659)
05-13 12:44:06.335: E/AndroidRuntime(25213):    at com.n2d.survey.MainActivity.Adddata(MainActivity.java:48)
05-13 12:44:06.335: E/AndroidRuntime(25213):    ... 14 more

1 个答案:

答案 0 :(得分:0)

请在插入声明中将+number+"'+'"+families+"替换为+number+"','"+families+"

另外,请注意这一点

通过以编程方式设置onClickListener(而不是通过XML),您可以省去很多麻烦。这可以帮助您完成您尝试做的事情:

Button button = findViewById(R.id.button);
button.setOnClickListener(
    new OnClickListener() {
        @Override
        public void onClick(View v) {
            /* DO SOMETHING UPON THE CLICK */
        }
    }
);