Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Android
Search Coderanch
Advance search
Google search
Register / Login
Win a copy of
Java Persistence with Spring Data and Hibernate
this week in the
Spring
forum!
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Ron McLeod
Tim Cooke
Paul Clapham
Liutauras Vilda
Sheriffs:
Junilu Lacar
Rob Spoor
Jeanne Boyarsky
Saloon Keepers:
Stephan van Hulst
Carey Brown
Tim Holloway
Piet Souris
Bartenders:
Forums:
Android
Beginning Java
Android Studio (Java) - counter doesn't update & app crash on empty input
Denis Neagu
Greenhorn
Posts: 7
posted 3 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
First time doing Android Studio and I encountered these errors where my counter will not increase and my app crashes on null input.
Any ideas? Tried for like an hour to get my head around it, but I just can't see why it's not doing it.
Edit:
Counter output should increase depending on how many times the same number is guessed
package com.example.dicerollertask; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import java.util.Random; import android.view.View; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends AppCompatActivity { public static Random rand = new Random(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void on_Click_Roll_Dice(View view){ int counter = 0; String errorMessage = "Invalid Entry"; String winMessage = "Same Number!"; String keepGoing = "Keep Going!"; TextView randomGeneratedOutput = this.findViewById(R.id.generatedNumberOutput); TextView outputMessage = this.findViewById(R.id.outputMessage); TextView outputCounter = this.findViewById(R.id.counter); EditText numberEntered = this.findViewById(R.id.enterInput); int randomGeneratedNumber = rand.nextInt((6-1)+1)+1; //Generate random number randomGeneratedOutput.setText(String.valueOf(randomGeneratedNumber)); //Display random number String userInput = numberEntered.getText().toString(); //Collect user input as string int num = Integer.parseInt(userInput); //Convert from users' input being string to integer if (num == randomGeneratedNumber){ outputMessage.setText(winMessage); counter ++; outputCounter.setText(String.valueOf(counter)); //ERROR HERE } else if (numberEntered.getText().toString().isEmpty()){ //ERROR HERE numberEntered.setError(errorMessage); } else { outputMessage.setText(keepGoing); } } }
img.png
Image of GUI
Denis Neagu
Greenhorn
Posts: 7
posted 3 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
not sure why it fixed it, but it worked
put counter as global var & created a public void for it then called it
Junilu Lacar
Sheriff
Posts: 17489
300
I like...
posted 3 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I doubt a global variable is the proper way to do this. See
https://developer.android.com/topic/libraries/architecture/saving-states
Also, this is redundant and looks confused:
int randomGeneratedNumber = rand.nextInt((6-1)+1)+1; //Generate random number
Not sure what you think you're doing there but it's equivalent to this:
int randomGeneratedNumber = rand.nextInt(6)+1; //Generate random number
It looks like it's time for me to write you a reality check! Or maybe a tiny ad!
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Calculator that accepts floating point values
Android code to add two numbers
virtual key board source code
Button with OnClickListener error
display in output is not seen for addition program
More...