Reputation: 79
I'm trying to pass an integer through a AsyncTask for use in the doInBackground on click of button.
Here is my passing the parameters
public void go(View view) {
EditText input = (EditText)findViewById(R.id.txtInput);
int downloads = Integer.parseInt(input.toString());
ProcessTask myTask = new ProcessTask();
//5000 - number of Ms to simulate download
myTask.execute(5000, downloads);
}
and then here is where I try to use those same values I pass
private class ProcessTask extends AsyncTask<Integer, Integer, Void>{
@Override
protected Void doInBackground(Integer... params) {
// TODO Auto-generated method stub
Integer myD = params[1];
for(int i = 0; i<myD; i++){
try{
Thread.sleep(params[0]);
publishProgress(i+1);
}catch (InterruptedException e){
e.printStackTrace();
}
}
return null;
}
I keep getting the following errors:
04-20 19:10:43.198: E/AndroidRuntime(4363): FATAL EXCEPTION: main
04-20 19:10:43.198: E/AndroidRuntime(4363): java.lang.IllegalStateException: Could not execute method of the activity
04-20 19:10:43.198: E/AndroidRuntime(4363): at android.view.View$1.onClick(View.java:3633)
04-20 19:10:43.198: E/AndroidRuntime(4363): at android.view.View.performClick(View.java:4240)
04-20 19:10:43.198: E/AndroidRuntime(4363): at android.view.View$PerformClick.run(View.java:17721)
04-20 19:10:43.198: E/AndroidRuntime(4363): at android.os.Handler.handleCallback(Handler.java:730)
04-20 19:10:43.198: E/AndroidRuntime(4363): at android.os.Handler.dispatchMessage(Handler.java:92)
04-20 19:10:43.198: E/AndroidRuntime(4363): at android.os.Looper.loop(Looper.java:137)
04-20 19:10:43.198: E/AndroidRuntime(4363): at android.app.ActivityThread.main(ActivityThread.java:5103)
04-20 19:10:43.198: E/AndroidRuntime(4363): at java.lang.reflect.Method.invokeNative(Native Method)
04-20 19:10:43.198: E/AndroidRuntime(4363): at java.lang.reflect.Method.invoke(Method.java:525)
04-20 19:10:43.198: E/AndroidRuntime(4363): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
04-20 19:10:43.198: E/AndroidRuntime(4363): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-20 19:10:43.198: E/AndroidRuntime(4363): at dalvik.system.NativeStart.main(Native Method)
04-20 19:10:43.198: E/AndroidRuntime(4363): Caused by: java.lang.reflect.InvocationTargetException
04-20 19:10:43.198: E/AndroidRuntime(4363): at java.lang.reflect.Method.invokeNative(Native Method)
04-20 19:10:43.198: E/AndroidRuntime(4363): at java.lang.reflect.Method.invoke(Method.java:525)
04-20 19:10:43.198: E/AndroidRuntime(4363): at android.view.View$1.onClick(View.java:3628)
04-20 19:10:43.198: E/AndroidRuntime(4363): ... 11 more
04-20 19:10:43.198: E/AndroidRuntime(4363): Caused by: java.lang.NumberFormatException: Invalid int: "android.widget.EditText{41754410 VFED..CL .F...... 32,110-736,188 #7f080002 app:id/txtInput}"
04-20 19:10:43.198: E/AndroidRuntime(4363): at java.lang.Integer.invalidInt(Integer.java:138)
04-20 19:10:43.198: E/AndroidRuntime(4363): at java.lang.Integer.parse(Integer.java:375)
04-20 19:10:43.198: E/AndroidRuntime(4363): at java.lang.Integer.parseInt(Integer.java:366)
04-20 19:10:43.198: E/AndroidRuntime(4363): at java.lang.Integer.parseInt(Integer.java:332)
04-20 19:10:43.198: E/AndroidRuntime(4363): at com.example.asyncfinalpractice.MainActivity.go(MainActivity.java:30)
04-20 19:10:43.198: E/AndroidRuntime(4363): ... 14 more
any and all help is much appreciated. I'm kind of new to android.
Upvotes: 1
Views: 131
Reputation: 36449
You're missing a getText()
call in your method chaining.
It needs to be input.getText().toString()
.
The clue is the logcat output:
Invalid int: "android.widget.EditText{...
That is definitely not your input.
For number parsing, I'd recommend actually catch
ing the Exception. That way any input that you might have forgotten to filter does not crash the program.
Upvotes: 1