Error creating onClick Android

Asked

Viewed 243 times

0

I have a button on Android where when clicking, will be directed to another screen. The structure of the page is like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#B4C4D9"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

<Button
            android:id="@+id/txtView3"
            android:layout_width="120dp"
            android:layout_height="80dp"
            android:layout_alignLeft="@+id/txtView1"
            android:layout_alignStart="@+id/txtView1"
            android:layout_below="@+id/txtView1"
            android:layout_marginTop="56dp"
            android:background="#5181a9"
            android:textColor="#B4C4D9"
            android:onClick="Cadastrar"
            android:drawableTop="@mipmap/ic_monitoria"
            android:text="@string/monitoria"
            android:textStyle="bold" />
 </RelativeLayout>

</LinearLayout>

The archive Register has already been created according to the image below:

inserir a descrição da imagem aqui

His XML is that way:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="br.com.projeto.acessosistema.Cadastrar">

</android.support.constraint.ConstraintLayout>

And Java:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Cadastrar extends AppCompatActivity {

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

Only when I click the button to go to the registration page, it says the app has stopped and when debugging, it returns the following error:

 FATAL EXCEPTION: main
Process: br.com.projeto.acessosistema, PID: 30190
java.lang.IllegalStateException: Could not find method Cadastrar(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'txtView3'
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
at android.view.View.performClick(View.java:5612)
at android.view.View$PerformClick.run(View.java:22285)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)

Does anyone know why this happens? I already created another button directed to another file and works normally, the problem only occurs with Registration.

1 answer

1

I managed to fix it. In the file where the button is, I created the method below:

public void Cadastrar(View view) {
    Intent cadastrar = new Intent(ConteudoSistema.this, Cadastrar.class);
    startActivity(cadastrar);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.