-1

This is my first app in Androin and i don't understand where is the error! When pressing a button to insert data in a db, the app has stopped unexpectedly and a logcat of:

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

This is my activty:

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);
}
}

and my 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>

this is the manifest:

<?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>

now the logcat says:

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
  • Check the accepted answer in this [link](http://stackoverflow.com/questions/4153517/how-exactly-does-the-androidonclick-xml-attribute-differ-from-setonclicklistene) – Lal May 13 '14 at 10:15
  • did you have a `PlaceholderFragment`?, if not try removing `$PlaceholderFragment` from you layout file – tkanzakic May 13 '14 at 10:38
  • what is your minSDK post your manifest too.. – Lal May 13 '14 at 10:39
  • `android:onClick` is for API level 4 onwards, so if you're targeting < 1.6, then you can't use it. – Lal May 13 '14 at 10:42
  • i edit the answer! how i can know what is my API level? – user3631915 May 13 '14 at 10:48
  • Are you trying to insert any values into database?? – Lal May 13 '14 at 10:48
  • i insert the values, when i click on the create button the app crash! – user3631915 May 13 '14 at 10:49
  • `table Rilevamenti has 4 columns but 3 values were supplied` this is the error you are getting..Check if you are getting all the values..Try debugging.. – Lal May 13 '14 at 10:50
  • provide a column list with your insert statements. This is good practice regardless, because your applicaton will be more robust towards schema changes.Do it like this..`"INSERT INTO " + TABLE_NAME + " (Name, Street, Block, City) Values (" ...etc` – Lal May 13 '14 at 10:53
  • 1
    you miss a comma between this elements: `+number+"'+'"+families+"` – tkanzakic May 13 '14 at 10:53
  • Replace `+number+"'+'"+families+"` with `+number+"','"+families+"` – Lal May 13 '14 at 10:56

1 Answers1

0

Please Replace +number+"'+'"+families+" with +number+"','"+families+" in your insert statement

ALSO,KINDLY NOTE THIS POINT TOO

You'll save yourself a lot of hassle by setting the onClickListener programmatically (instead of via XML). This should help you do what you're trying to do:

Button button = findViewById(R.id.button);
button.setOnClickListener(
    new OnClickListener() {
        @Override
        public void onClick(View v) {
            /* DO SOMETHING UPON THE CLICK */
        }
    }
);
Lal
  • 14,726
  • 4
  • 45
  • 70