<TextView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="4" android:text="ВЫВОД" android:id="@+id/rezult_ravno"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="ВЫВОД" android:onClick="rezult"/> public void rezult() { TextView rezultText = findViewById(R.id.rezult_ravno); rezultText.setText("Вылетает"); } 

After clicking the Button that runs the rezult () method, the program crashes (

 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.azeroth.calculator, PID: 10272 java.lang.IllegalStateException: Could not find method rezult(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:424) at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:381) at android.view.View.performClick(View.java:4802) at android.view.View$PerformClick.run(View.java:20101) at android.os.Handler.handleCallback(Handler.java:810) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:189) at android.app.ActivityThread.main(ActivityThread.java:5532) 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:950) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745) 
  • We read logs, if it is not clear what is written, then we copy them into the question. - Enikeyschik
  • To put it mildly, a newcomer: P If this is what was meant, then here) - Dmitry Vysotsky
  • Show the click handler code - McDaggen

2 answers 2

I see that you hung up the result method in xml ( android:onClick="rezult" ). If you suspend a method like this, the result method should look like this:

 public void rezult(View view) { TextView rezultText = findViewById(R.id.rezult_ravno); rezultText.setText("Вылетает"); } 

Those. on input, accept the View parameter. So it is not recommended, but it is not convenient. Remove this line from xml. In the onCreate method, hang up the handler with your method, here’s an example in Russian https://startandroid.ru/ru/uroki/vse-uroki-spiskom/16-urok-9-obrabotchiki-sobytij-na-primere-button. html See also urcs for binding and kotlin.

    You need to change the TextView rezultText = findViewById(R.id.rezult_ravno);

    on TextView rezultText = (TextView) findViewById(R.id.rezult_ravno);

    Need to be a little more attentive))

    • )) it is not necessary to change this way (TextView) and any twist elements are no longer necessary to indicate so. - McDaggen