I keep getting "Application not responding" when trying to implement a CountDownTimer. Here's what I have:
private TextView mTextField;
private EditText secText;
private int secs;
private Button buttonStart, buttonStop;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextField = (TextView) findViewById(R.id.textView1);
secText = (EditText) findViewById(R.id.segText);
buttonStart = (Button) findViewById(R.id.buttonStart);
buttonStop = (Button) findViewById(R.id.buttonStop);
secs = Integer.parseInt(secText.getText().toString());
final CountDownTimer count = new CountDownTimer((secs*1000), 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("Seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("FINISHED!");
}
};
The problem is with this line when it tries to parse, if I put a number it works:
secs = Integer.parseInt(secText.getText().toString());
Make sure you know the sequence of actions you need to perform. Here's some pseudo code
if buttonstart_clicked
get text from sectext
if text is empty
show error
else if text is notANumber
show error
else
parse text AND start timer
You forgot your starting point is the button click, not the start of the application, because your edittext is still empty by then.
You need to handle all the cases when trying to get the text from the EditText.
What if the EditText is empty?
What if the value in the EditText is not an Integer? ("1bs25", or "hello")
You need to consider these things and handle them properly before trying to parse the value into an Integer and start the timer.
Related
I'm making an app and I am trying to make a settings panel where all of your data from signing up is stored. I have a change button next to a TextView and when you click on that it takes you to a page called email where you can change your email you signed up with. There is an EditText and in the code I have a string that stores the data of what is in the EditText. It is supposed to set the email text to the EditText and go back to the settings page. But it goes back to the settings page and the email stays the same.
Code:
private TextView SetEmail;
private Button ChangeEmail;
private EditText newEmail;
private Button Save;
ChangeEmail.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.email);
newEmail = (EditText) findViewById(R.id.EmailChange);
Save = (Button) findViewById(R.id.Save);
emailerror = (TextView) findViewById(R.id.emailerror);
Save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String newEmails = newEmail.getText().toString();
String Text = SetEmail.getText().toString();
if (newEmails.contains("#") && newEmails.contains(".com") || newEmails.contains(".net") || newEmails.contains(".global")) {
SetEmail.setText(newEmails);
setContentView(R.layout.settings);
} else {
emailerror.setVisibility(View.VISIBLE);
}
Thank you for helping me out
p.s. I am working in one class.
I suppose your "page called email" is a different Activity, so you open that Activity using "startActivityForResult()" and when you want to close that "email Activity" you have to execute "setResult(resultCode, Intent)".
Link: https://developer.android.com/training/basics/intents/result
I have quite a simple application. However, after I clik on the button, app crashes. Tried to debug it and the problem seems to be in first 3 row of the onClick method. Once I tried to get there values manually, not via those edit boxes, everything went smoothly. Any ideas please?
public class MainActivity extends AppCompatActivity {
EditText editText_pocetKM;
EditText editText_spotreba;
EditText editText_cenaPHM;
TextView textView_spotrebaO;
TextView textView_cenaO;
DecimalFormat df = new DecimalFormat("0.00##");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText_pocetKM = (EditText) findViewById(R.id.editText1_pocetKM);
editText_spotreba = (EditText) findViewById(R.id.editText_Spotreba);
editText_cenaPHM = (EditText) findViewById(R.id.editText1_cenaPHM);
textView_spotrebaO = (TextView) findViewById(R.id.textView_spotrebaO);
textView_cenaO = (TextView) findViewById(R.id.textView_cenaO);
}
public void onClick(View v) {
Double pocetKm = Double.parseDouble(editText_pocetKM.getText().toString());
Double spotreba = Double.parseDouble(editText_spotreba.getText().toString());
Double cenaPHM = Double.parseDouble(editText_cenaPHM.getText().toString());
Double spotrebaO = spotreba * pocetKm / 100;
Double cenaO = spotrebaO * cenaPHM;
textView_cenaO.setText("Cena za spotřebované palivo bude "+ df.format(cenaO) + " Kč");
textView_spotrebaO.setText("Celkem bude spotřebováno "+ df.format(spotrebaO) + " litrů paliva");
}
}
You didn't provide the logcat of your crash report. If the logcat was provided we could be certain of your exact problem. But anyway, as you've got rid of your crash by removing the first three lines of your onClick function, I suppose you're setting invalid inputs in your EditText.
You're parsing the text entered in the EditText to double which will fail if the input is not a valid double string. For example, it'll parse 11.01 fine when it'll throw an exception while parsing Hello.
So to check if the application is crashing for a parsing error, you might consider surrounding them with a try/catch block like this.
try {
Double pocetKm = Double.parseDouble(editText_pocetKM.getText().toString());
Double spotreba = Double.parseDouble(editText_spotreba.getText().toString());
Double cenaPHM = Double.parseDouble(editText_cenaPHM.getText().toString());
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "Parsing error", Toast.LENGTH_LONG).show();
}
How can i put a function regarding this one on java android fire-base. I am creating a chat app and i have a existing fire-base structure like this.let say i send a message "Hello".
Chat:
Author:"name"
Chat:"Hello"
I want to put push a function that it will count the number of message in every user. therefore it will be like this. let say i send a message saying hi? and Be right back
Chat:
Author:"name"
Chat:"hi?"
numMessages: 1
Chat:
Author:"name"
Chat:"Be right back"
numMessages: 2
I think it is if statement count++? but i try everything regarding this.ugh.
Java and Android masters hope you can give me some suggestions sir.
Global int variable: int counter = 0;
In your onClick add: counter++;
Set counter to 0 whenever you want, if not it will count on and on - and is only set to 0 when your activity is recreated.
If you want to send it to firebase, you'll need to add the counter there to, and simply send it with your messages,update your Message.class with an int named "counter" (similiar to the Strings "author" and "message",send it will be sent and shown in your Chat.
Does this answer your question?
Update:
First, go into Message.class and add:
int counter;
public int getCounter(){
return counter;
}
public void setCounter(int counter){
this.counter = counter;
}
If you want to show the counter in your View, you can simply add another TextView in your fragment_message.xml or you can add it programatically in your message, in your MessageAdapter.java add in populateView:
//if you added an extra TextView in your fragment_message.xml
TextView counter = (TextView) view.findViewById(R.id.yourcounterid);
counter.setText(message.getCounter());
//if you want to add the number in your messageView:
messageView.setText(message.getMessage() + " " + message.getCounter());
So to your MainActivity.java:
public class MainActivity extends AppCompatActivity{
private EditText editText;
private ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
Firebase.setAndroidContext(this);
final Firebase firebase = new Firebase("https://samp-is.firebaseio.com/");
editText = (EditText) findViewById(R.id.editText);
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(new MessageAdapter(this, Message.class, R.layout.fragment_message, firebase.child("chat")));
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter++;
Message message = new Message();
message.setMessage(editText.getText().toString());
editText.setText("");
message.setAuthor("Name");
message.setCounter(counter);
firebase.child("chat").push().setValue(message);
}
});
}
}
If I remember correctly, Firebase should add a Child "counter" to its database and update it with your counter value.
Does it work?
I know this is a simpleton question, but I am not going to school for java, just learning it online.
how to a have a textview with an initial value of 0. and then everytime you press a button it ads 25 points to the score board.
At first I wanted the button press to add a random number between 42-57 to the score board.
And then how to do convert that int or long to a string to make it fit into a textview and keep the current score, and then add a new score.
EDIT: ok so someone said I should post the code so here it is.. where do i put this..
TextView txv182 = (TextView) findViewById(R.id.welcome);
txv182.setText(toString(finalScore));
Because when I do it, I get an error: The method toString() in the type Object is not applicable for the arguments (int)
public class MainActivity extends Activity {
// Create the Chartboost object
private Chartboost cb;
MediaPlayer mp = new MediaPlayer();
SoundPool sp;
int counter;
int db1 = 0;
Button bdub1;
TextView txv182;
int finalScore;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txv182 = (TextView) findViewById(R.id.welcome);
finalScore = 100;
sp = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
db1 = sp.load(this, R.raw.snd1, 1);
bdub1 = (Button) findViewById(R.id.b4DUB1);
bdub1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (db1 != 0)
sp.play(db1, 1, 1, 0, 0, 1);
txv182.setText(finalScore);
}
});
First you need to store your score to certain integer variable say score and set it to any initial value you want and use.
TextView tv = (TextView) findViewById(R.id.myTextView);
tv.setText(toString(score));
you do not need to initialize the textview with value just in onclick() of button do score+=25and add text to your textview as above.
hope this helps
It's actually like this..
pernts+=25;
txv182.setText(String.valueOf(pernts));
in android, after the
public class MainActivity extends Activity {
but before..
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
i wrote..
int pernts;
String strI;
so then after the above #Override majigy, i wrote..
strI = "" + pernts;
pernts = 0;
because when i wrote it like.. int pernts = 0; it never worked, forcing me to add final and what not..
any way.. How to convert an integer value to string? anSwered the question..
and so the end was like this
pernts+=25;
txv182.setText(String.valueOf(pernts));
i figured out you have to add a value to the int variable not the string.. i kept wanting to add 25 and i was getting 25252525252525.., instead of 25 50 75 100.. kinda cool actually.. separating the logic of how to "add" 25 .. anyway thanks.. SOF!
30 minutes later... found this.. How can I generate random number in specific range in Android?
..
import android.app.Activity;
public class InsMenu extends Activity {
static TextView txv182;
static int pernts;
Button bdub1;
public static void addPernts() {
int min = 37;
int max = 77;
Random r = new Random();
int i1 = r.nextInt(max - min + 1) + min;
pernts+=i1;
txv182.setText(String.valueOf(pernts));
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.BLAHBLAH);
bdub1 = (Button) findViewById(R.id.b4DUB1);
txv182 = (TextView) findViewById(R.id.welcome);
bdub1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
addPernts();
}
--- i created my first objekt thanks to this too.. http://www.tutorialspoint.com/java/java_methods.htm ..
I write program using 2-textview and one button to get textcontent of specfic website you write it in the first text view , and put text content in the second textview ,,,
But it dont work , the failure may be in this part of code - i checked every thing , it works alone good-
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText Enter = (EditText) findViewById(R.id.Enter);
CharSequence web = Enter.getText();
// CharSequence web = "http://www.google.com" ;
final String link = web.toString() ;
Button Press = (Button)findViewById(R.id.Press);
Press.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v ) {
Toast.makeText(MainActivity.this, link ,Toast.LENGTH_LONG ).show();
new DownloadTextTask().execute(
link);
}
});
}