Delete onClick error

0

My application has a button to delete all records from the database, I made the delete method and when I click the button it shows me the error.

LogCat:

   E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.allsport.miyonic.allsport, PID: 1526
                  java.lang.IllegalStateException: Could not find method del(View) in a parent or ancestor Context for android:onClick
     

attribute defined on view class   android.support.v7.widget.AppCompatButton with id 'btndeletar'                             at android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.resolveMethod (AppCompatViewInflater.java:325)                             at android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick (AppCompatViewInflater.java:284)                             at android.view.View.performClick (View.java:4780)                             at android.view.View $ PerformClick.run (View.java:19866)                             at android.os.Handler.handleCallback (Handler.java:739)                             at android.os.Handler.dispatchMessage (Handler.java:95)                             at android.os.Looper.loop (Looper.java:135)                             at android.app.ActivityThread.main (ActivityThread.java:5254)                             at java.lang.reflect.Method.invoke (Native Method)                             at java.lang.reflect.Method.invoke (Method.java:372)                             at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:903)                             at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:698)

DbHelper:

package Base;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;

public class DbHelper extends SQLiteOpenHelper {

    private static final String NAME_BASE = "Resultados";
    private static final int VERSION_BASE = 1;

    public DbHelper(Context context) {

        super(context, NAME_BASE, null, VERSION_BASE);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sqlCreateTableResultado = "CREATE TABLE resultado("
                + "id INTEGER PRIMARY KEY AUTOINCREMENT,"
                + "TimeCasa TEXT,"
                + "TimeFora TEXT,"
                + "GolsCasa INTEGER,"
                + "GolsFora INTEGER"+")";

        db.execSQL(sqlCreateTableResultado);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String sqlDropTableResultado = "DROP TABLE resultado";

        db.execSQL(sqlDropTableResultado);

        onCreate(db);

    }

    public void insertResultado(Esporte resultado){
        SQLiteDatabase db = getWritableDatabase();

        ContentValues valores = new ContentValues();
        valores.put("TimeCasa", resultado.getNomeTimeUm());
        valores.put("TimeFora", resultado.getNomeTimeDois());
        valores.put("GolsCasa", resultado.getValorUm());
        valores.put("GolsFora", resultado.getValorDois());

        db.insert("resultado", null, valores);

        db.close();
    }

    public List<Esporte> selectTodosResult(){
        List<Esporte> listResult = new ArrayList<Esporte>();
        SQLiteDatabase db = getReadableDatabase();

        String sqlSelectTodosResult = "SELECT * FROM resultado";

        Cursor c = db.rawQuery(sqlSelectTodosResult, null);

        if (c.moveToFirst()){
            do {
                Esporte onde = new Esporte();
                onde.setId(c.getInt(0));
                onde.setNomeTimeUm(c.getString(1));
                onde.setNomeTimeDois(c.getString(2));
                onde.setValorUm(c.getInt(3));
                onde.setValorDois(c.getInt(4));

                listResult.add(onde);
            }
            while (c.moveToNext());
        }

        db.close();
        return listResult;
     }

    public void delete(){
        SQLiteDatabase d = getWritableDatabase();
        d.execSQL("DELETE FROM resultado");
        d.close();
    }
}

activity code:

package com.allsport.miyonic.allsport;

        import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;
        import android.view.View;
        import android.widget.ArrayAdapter;
        import android.widget.Button;
        import android.widget.ListView;
        import java.util.List;

        import Base.DbHelper;
        import Base.Esporte;

        import static android.os.FileObserver.DELETE;

public class ResultSimples extends AppCompatActivity {

    private ListView lista;
    private Button apagar;



  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_result_simples);

    lista = (ListView) findViewById(R.id.ListaTimes);
    apagar = (Button) findViewById(R.id.btndeletar);

    apagar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DbHelper dd = new DbHelper(ResultSimples.this);
            dd.delete();
        }
    });
}

    @Override
    public void onResume(){
        super.onResume();

        DbHelper dbhe = new DbHelper(this);
        List<Esporte> listaResultPartida = dbhe.selectTodosResult();

        ArrayAdapter<Esporte> adp = new ArrayAdapter<Esporte>(this, android.R.layout.simple_list_item_1, listaResultPartida);

        lista.setAdapter(adp);
    }

    public void del(View view){
        super.onResume();

        DbHelper dd = new DbHelper(this);
        dd.delete();
    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<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.allsport.miyonic.allsport.ResultadoSimples"
                android:background="#003366">

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:id="@+id/ListaTimes"
    android:textColor="#fff"
    android:layout_marginTop="90dp" />

<TextView
    android:text="Resultado das Partidas"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="100px"
    android:id="@+id/textView"
    android:textColor="#fff"
    android:textSize="29dp"
    android:textStyle="normal|italic"
    android:textAlignment="center" />

<Button
    android:text="Deletar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/btndeletar"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="11dp"
    android:onClick="del"/>

</RelativeLayout>

Could anyone help me?

    
asked by anonymous 25.10.2016 / 21:25

2 answers

0

It seems like you're putting onClick on the button's XML, correct?

Try removing this onClick from the XML and do so:

apagar = (Button) findViewById(R.id.btndeletar);
apagar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DbHelper dd = new DbHelper(this);
            dd.delete();
        }
 });

So you have the guarantee that your button is pointed to the correct place, in the correct Activity.

    
25.10.2016 / 21:34
0

In your xml switch the following property of RelativeLayout :

DE:

tools:context="com.allsport.miyonic.allsport.ResultadoSimples"

TO:

tools:context="com.allsport.miyonic.allsport.ResultSimples"

Suggestion :

There is no need to call super.onResume(); within your public void del(View view) method

    
31.10.2016 / 17:38