I use this code to get a number from an EditText and pass it into a Count Down Timer
final int mytime;
mytime = Integer.parseInt(textIn.getText().toString());
btnStartTimer.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startCountDownTimer(mytime);
}
});
However the app crashed and I get a:
java.lang.NumberFormatException: Invalid int:
However when I write it like this it works:
btnStartTimer.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startCountDownTimer(Integer.parseInt(textIn.getText().toString()));
}
});
Can please someone explain me difference ? Can I use the first code somehow ?
I really don't understand the problem
In you first example the value of the text has not been set and will be null. You want to do the action after the use has filled in the form and clicked on the button don't you.
Related
I want to design a basic command pattern that will save integer values and be able to execute a undo method. I'm not sure where to start at all so any help would be much appreciated. Very basic interpretations like below:
package com.k.s;
public class MainActivity extends Activity {
public int Counter = 0;
#Override
protected void onCreate(Bundle savedInsanceState) {
super.onCreate(savedInsanceState);
setContentView(R.layout.activity_main);
Button.setOnClickListener (new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Counter += 1;
}
}
}
}
add action to the stack and remove it for "undo". example of command pattern: https://berther.io/2004/09/16/using-the-command-pattern-for-undo-functionality
So I'm developing currently my own private App which I want to use only for me and maybe some friends.
Well I'm german so my english is maybe not the best I hope you can forgive me.
Now my Problem is that I want to set in my Optionsmenu a Budget for the current month to keep track of. I'm doing that by using an EditText with a Button.
Now I want to save this String which is getting entered in my EditText to a String value and a Integer value because I want to show the Value in a TextView on my MainPage and use the Integer value to calculate my current budget I got and so on.
Now I'm showing you guys my code and I hope u can tell me whats wrong with it.
I'm trying to get my Value in the Options class which is related to my OptionsMenu and later Trying to get the Value out of my Options class into my Main class.
public class Options extends Activity {
Button GoBack, SetBudget;
private int Budget;
String BudgetString = "";
EditText BudgetEdit;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.options);
GoBack = (Button) findViewById(R.id.button2);
GoBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent GoBackToMain = new Intent("com.lunarrepublic.STARTINGPOINT");
startActivity(GoBackToMain);
}
});
SetBudget = (Button) findViewById(R.id.button8);
SetBudget.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
BudgetEdit = (EditText) findViewById(R.id.editText1);
BudgetString = EditText.getText();
//In the Line above this is the error "Type mismatch. Cannot convert Editable to String
Budget = Integer.parseInt(BudgetString);
}
});
}
But if I try to set my "BudgetString" to Editable It won't work either.
The GoBack Button is unnecessary for my problem here so you don't have to read over it.
So I hope you guys understood what my problem is and can maybe help me getting it fixed
Thanks in advance
This is wrong
BudgetString = EditText.getText();
Use below one
BudgetString = BudgetEdit.getText().toString();
change this
SetBudget.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
BudgetEdit = (EditText) findViewById(R.id.editText1);
BudgetString = EditText.getText();
//In the Line above this is the error "Type mismatch. Cannot convert Editable to String
Budget = Integer.parseInt(BudgetString);
}
});
to
SetBudget.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
BudgetEdit = (EditText) Options.this.findViewById(R.id.editText1);
BudgetString = BudgetEdit.getText();
//In the Line above this is the error "Type mismatch. Cannot convert Editable to String
Budget = Integer.parseInt(BudgetString);
}
});
I've tried implementing an onClickListener on a CameraFragment, however, it never seems to be called. I am probably missing something quite simple. Does anyone have any ideas?
public class CWACCameraFragment extends CameraFragment implements OnClickListener {
//...
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
takePicture();
Toast.makeText(getActivity(),"click",
Toast.LENGTH_LONG).show();
}
Is there a way to ensure that the onClick event occurs?
In the demo app, I added the following to DemoCameraFragment:
#Override
public void onStart() {
super.onStart();
getView().setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e(getClass().getSimpleName(), "got here");
}
});
}
Log messages showed up just fine. Hence, AFAICT, your approach works, so perhaps there is some bug in how you wired in the click listener.
How can I write command in onclicklistener() so that I can move to next activity as well as my post status dialog will appear on that new activity. I am using intent for switching to next activity and also using postTowall() method. But these two doesn't perform simultaneously. I am using this method:
public void onClick(View v) {
// TODO Auto-generated method stub
postTowall();
Intent intent= new Intent(Frnd.this,Logout.class);
startActivity(intent);
}
private void postTowall() {
facebook.dialog(this, "feed", new DialogListener() {
#Override
public void onFacebookError(FacebookError e) {
}
#Override
public void onError(DialogError e) {
}
public void onComplete(Bundle values) {
String sh = null;
Bundle params = new Bundle();
params.putString("caption", sh);
}
public void onCancel() {
// TODO Auto-generated method stub
}
You can use thread for posting. Use asynctask and inside that post the message to face book. Or you may just write a simple thread and start it.
This asynctask link may help you. it has usage example also:
http://developer.android.com/reference/android/os/AsyncTask.html
You should set other activity to open post dialog. Send via intent a signal if it should or not...
Is there a way of having 1 single button perform more then 1 job? Lets say the user clicks on a button called 4 that displays a textview showing the number 4, can the user then press the button 4 again and display the string "four". Can you some how use switch() cases to do it?
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
add = add +4;
display.setText("2 + 2 =" + add);
}
});
Have a Boolean variable to keep track of states (please make this global to an activity so that function may access it). i.e.:
#Override
public void onClick(View v) {
if(flag){
add = add +4;
display.setText("2 + 2 =" + add);
}
else display.setText("Something else");
flag=!flag;
}