Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not find a method onClick(View) in the activity class android.view.ContextThemeWrapper for onClick handler

I don't know whats happening in my application now.
I'm getting this message Could not find a method onClick(View) in the activity class android.view.ContextThemeWrapper for onClick handler
And everything is right, I think...
Here's my activity xml file:

<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:background="#9EC5E2"
    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/Theme.AppCompat.Light"
    tools:context="com.tumta.henrique.teste.view.LoginActivity">

    <LinearLayout
        android:id="@+id/layout_campos"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="80dp"
            android:text="@string/text_usuario"
            android:textSize="35sp" />

        <EditText
            android:id="@+id/edit_usuario"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="15" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="80dp"
            android:text="@string/text_senha"
            android:textSize="35sp" />

        <EditText
            android:id="@+id/edit_senha"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="15"
            android:password="true" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/layout_campos"
        android:layout_marginTop="50dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button_ok"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Ok"
            android:onClick="onClick"/>

        <Button
            android:id="@+id/button_cancelar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:onClick="onClick"
            android:text="Cancelar" />

    </LinearLayout>


</RelativeLayout>

And here's my Activity:

package com.tumta.henrique.teste.view;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.tumta.henrique.teste.R;
import com.tumta.henrique.teste.model.ConsultaLogin;

public class LoginActivity extends ActionBarActivity implements ConsultaLogin.ConsultaConcluidaLoginListener {


    EditText txtUsuario, txtSenha;
    ActionBar actionbar;
    Button btnOk, btnCancelar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new ConsultaLogin(this).execute();
        setContentView(R.layout.activity_login);
        txtUsuario = (EditText) findViewById(R.id.edit_usuario);
        txtSenha = (EditText) findViewById(R.id.edit_senha);
        actionbar = getSupportActionBar();
        actionbar.hide();
        btnOk = (Button) findViewById(R.id.button_ok);
        btnCancelar = (Button) findViewById(R.id.button_cancelar);
    }

    public void onClick(View view) {
        Toast.makeText(this, "test", Toast.LENGTH_SHORT);
        //ConsultaLogin.login = txtUsuario.getText().toString();
        //ConsultaLogin.senha = txtSenha.getText().toString();
        //new ConsultaLogin(this).execute();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_login, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onConsultaConcluida(String result) {
        if (result == null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Erro");
            builder.setMessage("Usuario ou Senha Incorreto!");
            builder.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    txtUsuario.getText().clear();
                    txtSenha.getText().clear();
                    txtUsuario.requestFocus();
                }
            });
        } else {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
        }
    }
}

I don't know whats wrong here. Can someone help me?


EDIT
My complete error stacktrace:

06-18 08:40:40.027  27976-27976/com.tumta.henrique.teste E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.tumta.henrique.teste, PID: 27976
    java.lang.IllegalStateException: Could not find a method onClickOk(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.Button with id 'button_ok'
            at android.view.View$1.onClick(View.java:4007)
            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)
     Caused by: java.lang.NoSuchMethodException: onClickOk [class android.view.View]
            at java.lang.Class.getMethod(Class.java:664)
            at java.lang.Class.getMethod(Class.java:643)
            at android.view.View$1.onClick(View.java:4000)
            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)
like image 293
henrique romao Avatar asked Jun 17 '15 20:06

henrique romao


2 Answers

Found the Solution.. I don't know why, but after doing some search I saw some posts saying that it's an issue on Android 5.0 or higher..
The solution was removing the theme tag from my layout.xml file.

like image 189
henrique romao Avatar answered Sep 29 '22 09:09

henrique romao


I got an Activity as a Dialog, so i can't remove the "theme" tag.

When extending Activity (not AppActivityCompat) i had to set a listener on my button, instead using the onClick provided by xml editor:

Button myBtn = findViewById(R.id.myButton);
myBtn.setOnClickListener((v) -> myMethod(v));

Hope it helps someone.

like image 41
Acauã Pitta Avatar answered Sep 29 '22 08:09

Acauã Pitta