Android Process closed onClick - android

I am trying to make a score increment when an answer is shown as correct in a textfield. The code is as follows:
Variable is declared:
static int score = 0;
Method:
public void score(){
if(check.getText().equals("Correct")){
score++;
}
Score.setText(String.valueOf(score));
}
The Score is a textfield and check is another textfield
Then this is called in onclick as score();
I appreciate any ideas on why this gets a process closed error when I am clicking the button.
Thanks.
Edit:

Best advice I can give is set a break point at your if statement then use the debugger to see what is null.

Related

Create a key pressed function for button

i want create a key pressed (hold key) func for my application.
for example when I press (hold) a button next year the values of textview grow and when I pickup my finger from button, stop working.
anyone can help me i most use which of Listener or function for create it?
this is my setOnClickListener but i want button press(hold) work
nextDay.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (values >= downrange && values <= uprange)
values++;
if (values > uprange)
values = downrange;
if(values <10)
textDayo.setText(PersianReshape.reshape("0"+String.valueOf(values)));
else
textDayo.setText(PersianReshape.reshape(String.valueOf(values)));
}
});
thanks for your help
and sorry for bad english
when I press (hold) a button next year the values of textview grow
Have you looked at the DatePicker? This allows you to set a date very easily:
You can follow this tutorial
If you do not want to use a DatePickker, you can try to use an OnTouchListener and a Handler to do this yourself, but this is hard to do.
I understand that you are trying to hold a button (widget) and while holding it to increase the year.
If this is the case then this link should help you. The idea is that you use a timer to update the ui. Something similar is also described here. If your problem is just setting the date though, a simpler way could be the one suggested by Sam above.

parseInt crashing android app

So I'm trying to make a calculator app (just to get a hang of android development), and I noticed with some testing that the parseInt conversion from "String tack1" is causing the app to crash. Can someone tell me why? I can't seem to figure it out and I've been searching the internet for quite a while now. (hehe I'm a noob so please go easy) In the code below, I've modified a couple lines to make it seem obvious what it's supposed to print however it still crashes. Here's the code:
equals.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
oC = 1; //operator checker is on 1 (for plus)
switch(oC){ //check for operator clicked
case 0: break;
case 1:
tack1 = "1"; //INSERTED THIS TO MAKE OUTPUT OBVIOUS
tack1 = tack1.trim(); tack2 = tack2.trim(); //INSERTED BEFORE TO DEAL WITH WHITESPACE
numOne = Integer.parseInt(tack1); //CRASHES HERE
answer.setText(numOne);
modeChecker = 0; oC = 0;break;
NOTES ON PROGRAM(some of comments repeated and other stuff as well):
The tack1 = "1"; is to make output obvious
The tack1.trim() is to deal with whitespace
Yes whatever is in tack is a number and an integer (not even a negative integer)
Yes numOne is an integer and is defined wayy above (not in code listed here)
Sorry the indents are all messed up(after case 1) because of the comments I added
This is a section of my onClick method, so closing brackets aren't included in here.
Can someone please help me?
THANKYOU :D
I'd be willing to bet it's actually crashing on the following line AFTER the call to parseInt.
You're calling setText(int) on your TextView. When you pass an int to this method, that int is a pointer to a string resource...you're probably expecting an auto-conversion to a string. Since you are passing it an int that is generated in your application, it's extremely unlikely that this int also points to a string resource in your res/values/strings.xml file. What you really want to do is change numOne to a string first, which you can do inline:
Change
answer.setText(numOne);
to
answer.setText(String.valueOf(numOne));
and you're good to go.

How to count yes and no answers using radio buttons

I don't know if this was asked or not (I searched a little but no result) but I'm kinda time pressed and I need help
I've been developing an application in Android and I've only started under this platform for 3 months
I have a choice test with different questions and yes and no answers using radio buttons
I want to count the "yes" answers but even like this if I put r1.isChecked() instead of for and buttons[i].isChecked() it counts the clicks
Here is what I tried until now and I get force close everytime I click on the first radiobutton RadioButton[]buttons={rb1, rb3,rb5} ;
public void onClick(View view){
checkStates(buttons);
}
private void checkStates(RadioButton[] buttons) {
for (int i=0; i<buttons.length; i++) {
if (buttons[i].isChecked())
da++;}
tv.setText("Result:"+yes);
}
How can I tell my appplication that the radiobuttons are already checked and no need to increment it on a second click (on the same radio button?)
Is there a way to count using the ids or the name of the radiobuttons?
If so how should I do it? Some tutorials (I love them) and tips would help a lot.
Thanks.
You wouldn't want to count each click or even each "state change" because that wouldn't be accurate.
The best thing to do would be to create a function called checkStates(RadioButton[] buttons) { }
You would call this function each time the user clicks a RadioButton.
You would have all your RadioButton objects stored in an array, which you pass to the function, and then the function goes through the list of RadioButtons and checks their state. If it counts 6 as being Yes, then go to your other activity.

Setting the score on textview

I am trying to set the score in a game but it isn't doing anything at the moment.
I have declared a score variable:
static int score = 0;
When an answer is shown in another textview as "Correct" I want the score to increment and show this in another textfield which will be for the score.
So far I have tried this:
public void score(){
check.getText();
if(check.equals("Correct")){
score++;
Score.setText(String.valueOf(score));
}
check is a Textfield which shows Correct or Incorrect. Score is another textfield.
and then I put the method score() into an onclick, but this doesn't update the textfield.
I would appreciate any advice on this.
Thanks
Edit:
check.equals("Correct")
should be
check.getText().equals("Correct")
You don't heed the check.getText() method call that you have by itself. And you need to use this for your if statement
if(check.getText().toString().equals("correct))
getText returns the object type Editable which is not a String. If you are getting a force close please edit you question to include the stack trace from the LogCat output.

Error in Android's clearCheck() for RadioGroup?

I'm having an issue with RadioGroup's clearChecked(). I'm displaying a multiple choice question to the user and after the user selects an answer I check the answer, give him some feedback and then move to the next question. In the process of moving to the next question I clearCheck on the RadioGroup.
Can anyone explain to me why the onCheckedChanged method is called 3 times? Once when the change actually occurs (with the user changes), once when I clearCheck(with -1 as the selected id) and once in between (with the user changes again)?
As far as I could tell the second trigger is provoked by clearCheck. Code below:
private void checkAnswer(RadioGroup group, int checkedId){
// this makes sure it doesn't blow up when the check is cleared
// also we don't check the answer when there is no answer
if (checkedId == -1) return;
if (group.getCheckedRadioButtonId() == -1) return;
// check if correct answer
if (checkedId == validAnswerId){
score++;
this.giveFeedBack(feedBackType.GOOD);
} else {
this.giveFeedBack(feedBackType.BAD);
}
// allow for user to see feedback and move to next question
h.postDelayed(this, 800);
}
private void changeToQuestion(int questionNumber){
if (questionNumber >= this.questionSet.size()){
// means we are past the question set
// we're going to the score activity
this.goToScoreActivity();
return;
}
//clearing the check
gr.clearCheck();
// give change the feedback back to question
imgFeedback.setImageResource(R.drawable.question_mark); //OTHER CODE HERE
}
and the run method looks like this
public void run() {
questionNumber++;
changeToQuestion(questionNumber);
}
What I've discovered is that if an item is checked and you call clearCheck() on the radio group it will call onCheckedChanged twice. The first time with the id of the item that was checked and the second time with -1/View.NO_ID. IMHO, this is a bug and apparently it has been around since at least 1.6. See this google code bug report: http://code.google.com/p/android/issues/detail?id=4785
It seems to be that the only solution is to check the actual RadioButton.isChecked() and test if it is true or false. This sort of defeats the purpose of the onCheckedChanged returning the id of the item since you now have to either keep references to those buttons or call findViewById every time.
I doubt they will fix this since changing it would probably break existing code in unexpected ways.
I faced the same problem and i solved with other work-around.
Set CheckedChangeListener as NULL
Do your operation
Reset your OnCheckedChangeListener again
Code snnipet :
rdbGroup.setOnCheckedChangeListener(null);
rdbGroup.clearCheck();
rdbGroup.setOnCheckedChangeListener(checkedChangeListener);
Hope this will help ツ
I had similar problem. My solution:
in procedure:
public void onCheckedChanged(RadioGroup rGroup, int checkedId)
I check checkedId. It equals to -1 if you use clearCheck()
else it equals to selected radiogroup child

Categories

Resources