Android Studio shows only one method for button onClick and Java throws an exception and ruins the app

environment

PC:Windows10 1607 AndroidStudio:2.2.3

Own skills

An infrastructure server engineer who usually plays around with Linux servers (for the time being). The client side is almost empty. I am new to development using IDE and Java itself, and I am studying from scratch while reading the introductory book.

What you are trying to do

Creating and learning apps that run on Android (Ultimately, I want to run the application built on the PC on the Android device and communicate with my own server to do this and that)

Be in trouble

When I press the installed button and try to execute the method with onClick, the application crashes. It doesn't always fall, and sometimes it works well if you mess around with it on the IDE.

Below, specific symptoms and procedures

I am making a simple operation check application with Android Studio. While reading a book, try arranging buttons and text, call a method with onClick, do some processing, I was able to do everything like displaying it in Toast and rewriting the text (I was able to do it ...). The reference book is here

After that, I am addicted to the following procedure in the situation where I am recoding from scratch. ..

What you are trying to make

What I'm trying to make right now goes back to the simplest one,

There are only two points.

Implementation procedure

  1. Create a new Project. The project name used to confirm the reproduction this time is "TestApp9". Also, the Minimum SDK starts with API Level: 15 and Activity starts with Empty Activity.

  2. I don't need the textView that is placed by default, so delete it.

    1. Select Button from Widgets on activity_main.xml and drop it to the upper left as appropriate. Set the ID of the installed button to "buttonTest", add a resource to text, set the ID of the added resource to "buttonText", and set the value to "test".
  3. Edit MainActivity.java and add the following contents.

MainActivity.java


~~~The import part is omitted~~~

public class MainActivity extends AppCompatActivity {

    Button buttonTtest; //Postscript

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

        buttonTtest = (Button) findViewById(R.id.buttonTest); //Postscript

    }

    //Add a method that describes the content you want to move with onClick
    public void onButtonTestClick(View view) { 
        Toast.makeText(this, "clicked", Toast.LENGTH_SHORT).show();
    }
}

In addition to onCreate written from the beginning, I am writing a new added part.

  1. In this state, return to activity_main.xml again and specify the newly created onButtonTestClick in the onClick property.

AndroidStduio1.png

Make sure you can select it, but try building without selecting it (leave none).

  1. Build and confirm that the app is launched on the virtual device and the buttons are arranged as intended.

AndroidStduio2.png

Of course, when I press the "TEST" button, nothing happens (as an aside, I'm wondering if the Value of @ string / buttonTest is in lowercase but it's in uppercase. Another button. I wonder if lowercase letters can be used if it is styled).

  1. Next, specify the method you created earlier for onClick.

AndroidStduio3.png

IDE is convenient, but I'm still scared because I don't know what works behind the scenes (I can imagine)

  1. Build, run, and press the button you have installed.

AndroidStduio4.png

Let's go! Ruined s (abbreviated below Will be displayed and the app will stop working. At this time, the following Java exception is output in red in the Run column of Android Studio (is it?).

E/AndroidRuntime: FATAL EXCEPTION: main
                  java.lang.IllegalStateException: Could not find method onButtonTestClick (MainActivity)(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'buttonTest'
                      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:4084)
                      at android.view.View$PerformClick.run(View.java:16966)
                      at android.os.Handler.handleCallback(Handler.java:615)
                      at android.os.Handler.dispatchMessage(Handler.java:92)
                      at android.os.Looper.loop(Looper.java:137)
                      at android.app.ActivityThread.main(ActivityThread.java:4745)
                      at java.lang.reflect.Method.invokeNative(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:511)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                      at dalvik.system.NativeStart.main(Native Method)
Application terminated.

IllegalStateException sounds unpleasant. I don't remember doing anything that made me angry, but I'll check the error details for the time being.

java.lang.IllegalStateException: Could not find method onButtonTestClick (MainActivity)(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'buttonTest'

It says Could not find method, so I can't find the method you specified for onClick (゚ Д ゚) Gol', isn't it? I think. Even if I couldn't find it, it was recognized by the IDE and I could select it from the drop-down list. .. I will do it a little.

After that, it's written a lot, but honestly I don't understand. Since there is View.Java: 4084, it is certain that the Iloilo problem occurred behind the error as a result of the error, not the error in the part I wrote. I think the important thing is the first error.

Well, I was able to do it once, why? I'll play with it a little.

  1. Check the value of the onClick attribute. Then.

AndroidStduio5.png

There are two w, this w Ww that does not increase without permission

  1. I'm scared, so I'll try to make one method dead.

AndroidStduio6.png

I will comment it out. (If you select Ctrl + Alt with Sublime Text's multi-cursor feeling, a lot of code will be generated and I'm scared.)

  1. Confirm that it disappeared from the onClick attribute.

AndroidStduio7.png

Yes, I'm gone. If you build a mobile app, you will naturally have an app with buttons that do not respond at all. I returned to the beginning.

  1. Try enabling the method again. Uncomment the previous comment.

AndroidStduio9.png

It seems that View and Toast were automatically deleted from import when commented out. It's convenient, but it's too convenient. If you press Ctrl + Alt when it is red, it will be imported automatically, so import it again.

  1. Double-check that you can select the method.

AndroidStduio11.png

The one that was two has returned to one. Is it a place where you can feel at ease for the time being? So, when I was messing around here several times today, I rebuilt it like this and it worked fine! !! There was a case, but if I check it now, it will not work with the same error. ..

  1. Really increase the number of methods.

AndroidStduio13.png

It's scary to increase it without permission, so I'll try to increase it explicitly here. I made onButtonClick2.

  1. Make sure you can select.

AndroidStduio14.png

Yes, that's right. It's good. Since it's a big deal, let's build with the newly created 2.

  1. Let's go! Ruined y (abbreviated below

But after all, when I build and run it, the same error occurs. .. In the end, like this. ..

AndroidStduio15.png

That's why I'm making a fool of wwwwwwww

E/AndroidRuntime: FATAL EXCEPTION: main
                  java.lang.IllegalStateException: Could not find method onButtonTestClick2 (MainActivity)(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'buttonTest'
                      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:4084)
                      at android.view.View$PerformClick.run(View.java:16966)
                      at android.os.Handler.handleCallback(Handler.java:615)
                      at android.os.Handler.dispatchMessage(Handler.java:92)
                      at android.os.Looper.loop(Looper.java:137)
                      at android.app.ActivityThread.main(ActivityThread.java:4745)
                      at java.lang.reflect.Method.invokeNative(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:511)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                      at dalvik.system.NativeStart.main(Native Method)
Application terminated.

The error message is also properly displayed as 2.

Conclusion

Nobody loves the apps I made

Google

If you google near the error message, ↓ this area will be hit.

stackoverflow(1) => Isn't it a typo? Pointed out. This time, I'm selecting a method from the Android Studio GUI, so I don't think that's the case. ..

stackoverflow(2) => It's like doing various things and doing various things would be cool. I haven't done that much this time ...

teratail => Oh, it looks like this person. Somehow, the answer is harsh. w Do you understand the meaning of the onClick attribute? => I don't think I understand it at all, I'm sure, maybe. ..

stackexchange => Oh, this isn't it? I don't know what it is, but I found something that looks like it! !!

Update your Gradle to lastest version first and try again. I hope that can be solved your issue by this solution.

Well, it's not good to raise the mon that came in without permission. .. I don't want to do it. .. For the time being.

Conclusion again

As a workaround mentioned in the above article, we have confirmed that it can be operated with the following correspondence.

this AndroidStudio16.png

do this AndroidStudio17.png

Then AndroidStudio18.png

Stunningly, Toast was displayed. The method specified by onClick was written as "onButtonTestClick (MainActivity)", and the back (MainActivity) was Irane (I'm gonna get in without permission!). .. Well, I still don't understand the meaning of proliferation. ..

If you have any knowledge about this matter, we would appreciate it if you could give us your opinion. m (_ _) m

Recommended Posts

Android Studio shows only one method for button onClick and Java throws an exception and ruins the app
[Android Studio] Set an arbitrary image for the application background [Java]
Impressions and doubts about using java for the first time in Android Studio
[Android] Change the app name and app icon for each Flavor
[Android Studio] [For beginners] Let's roughly explain the screen and directories
Introduction to Android App Development 1 Installing JDK and Android Studio for mac
Creating an app and deploying it for the first time on heroku
The Android app crashes. Just click a button and it will fall.