PC:Windows10 1607 AndroidStudio:2.2.3
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.
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)
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.
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 I'm trying to make right now goes back to the simplest one,
Call a specific method with onClick when you press the installed button
The called method just displays "Clicked" in Toast
There are only two points.
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.
I don't need the textView that is placed by default, so delete it.
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.
Make sure you can select it, but try building without selecting it (leave none).
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).
IDE is convenient, but I'm still scared because I don't know what works behind the scenes (I can imagine)
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.
There are two w, this w Ww that does not increase without permission
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.)
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.
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.
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. ..
It's scary to increase it without permission, so I'll try to increase it explicitly here. I made onButtonClick2.
Yes, that's right. It's good. Since it's a big deal, let's build with the newly created 2.
But after all, when I build and run it, the same error occurs. .. In the end, like this. ..
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.
Nobody loves the apps I made
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.
As a workaround mentioned in the above article, we have confirmed that it can be operated with the following correspondence.
this
do this
Then
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