[SOLVED]Android studio app crashing because of onClickListener | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

18th Aug 2020, 6:27 PM
Charlie Crease-huggett
11 Answers
+ 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.
18th Aug 2020, 10:59 PM
HBhZ_C
HBhZ_C - avatar
+ 3
You have to see logcat for errors or to build your project synchronize gradle will sometimes helps.
18th Aug 2020, 6:52 PM
HBhZ_C
HBhZ_C - avatar
+ 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.
18th Aug 2020, 7:07 PM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 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
18th Aug 2020, 11:08 PM
Charlie Crease-huggett
+ 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.
18th Aug 2020, 8:20 PM
Charlie Crease-huggett
+ 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
18th Aug 2020, 9:38 PM
Charlie Crease-huggett
+ 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
18th Aug 2020, 9:52 PM
Charlie Crease-huggett
+ 1
[SOLVED] I had put the code into the wrong class which is why it could not find the correct button.
19th Aug 2020, 12:24 AM
Charlie Crease-huggett
+ 1
Thesmallest thanks for that nice to meet group standards. :)
19th Aug 2020, 12:25 AM
Charlie Crease-huggett
0
Thanks for the advice, unfortunately it did not seem to work.
18th Aug 2020, 8:12 PM
Charlie Crease-huggett
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.
18th Aug 2020, 8:15 PM
Charlie Crease-huggett