0
[SOLVED]Android studio app crashing because of onClickListener
I can get the code to run if I write a function and call in xml i.e. android:onClick = "Funct" however when I set the onclicklistener in Java it crashes. Am I missing anything? https://code.sololearn.com/ceJX7A4ftr4b/?ref=app
11 Réponses
+ 3
The eventlistener do not recognize the button object on witch it will be set just check if the button is declared with id and initialized (Button) = btn.findViewById(....) we can not help you if there is no full code.Else just research in stackoverflow or google the error message.
+ 3
You have to see logcat for errors or to build your project synchronize gradle will sometimes helps.
+ 3
Charlie Crease-huggett , Write code in code playground and then link it to your question.
I can't see implementation of OnClickListener(). We'll need to see complete code. As suggested earlier also post messages from logcat. + Check if you have really added `android:id` attributes to views in xml file.
+ 2
Did I not link the code correctly? Anyway your right I realized at that exact moment as you've messaged. I'm putting the code in the wrong class and it cannot find it within the layout... I'm sorry about that
+ 1
HBhZ_C synced the gradle as recommended still not working. I have tried log.d but as soon as my app tries to get to the activity before it will crash before.
+ 1
Yes just checked, the app works well when you set in XML , I just want to do it this way to re use activities, i.e. I don't want to create a separate activity for each mathematics subject I create
+ 1
Just found this error message when running in debug mode. Does this help?
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mathematics_educational, PID: 23508
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mathematics_educational/com.example.mathematics_educational.Sel_Chap}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
+ 1
[SOLVED] I had put the code into the wrong class which is why it could not find the correct button.
+ 1
Thesmallest thanks for that nice to meet group standards. :)
0
Thanks for the advice, unfortunately it did not seem to work.
0
🇮🇳Omkar🕉 I checked the XML files they did all contain ids. I have attached my code, I did not realise it did not go through fully. I would be very appreciative if you could highlight my mistake as I've spent a considerable amount of time.