0
My application has a button to delete all records from the database, I did the delete method and when I click the button it presents 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 someone help me?
Nathan, try inserting your xml here to better understand your question;
– viana
ready @seamusd....
– Nathan
This XML is in the file activity_result_simple.xml?
– ramaral
yes @ramaral...
– Nathan
Nathan, try to put this property on your button
android:clickable="true"
and check if it will continue giving error.– viana
@seamusd this is not necessary. Nathan the mistake is the one you posted? I see no reason for it.
– ramaral
Another thing, if you want to keep calling your
super.onResume()
within its function, try to insert it after its deletion. Abs.– viana
In fact, I did as @Leonardo Dias recommended, so my code it presents the error in this, as I posted below... even with his tip is not working, now why not know
– Nathan
@ramaral you’re absolutely right, the
clickable="true"
has no effect on this situation.– viana
Which version of appcompat-v7 you’re using?
– viana
min: 2.4.4 max: 6.0
– Nathan