java - Passing parameters through AsyncTask - android -
i'm trying pass integer through asynctask use in doinbackground on click of button.
here passing 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 simulate download mytask.execute(5000, downloads); }
and here try use same values 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 = 0; i<myd; i++){ try{ thread.sleep(params[0]); publishprogress(i+1); }catch (interruptedexception e){ e.printstacktrace(); } } return null; }
i keep getting 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: not execute method of activity 04-20 19:10:43.198: e/androidruntime(4363): @ android.view.view$1.onclick(view.java:3633) 04-20 19:10:43.198: e/androidruntime(4363): @ android.view.view.performclick(view.java:4240) 04-20 19:10:43.198: e/androidruntime(4363): @ android.view.view$performclick.run(view.java:17721) 04-20 19:10:43.198: e/androidruntime(4363): @ android.os.handler.handlecallback(handler.java:730) 04-20 19:10:43.198: e/androidruntime(4363): @ android.os.handler.dispatchmessage(handler.java:92) 04-20 19:10:43.198: e/androidruntime(4363): @ android.os.looper.loop(looper.java:137) 04-20 19:10:43.198: e/androidruntime(4363): @ android.app.activitythread.main(activitythread.java:5103) 04-20 19:10:43.198: e/androidruntime(4363): @ java.lang.reflect.method.invokenative(native method) 04-20 19:10:43.198: e/androidruntime(4363): @ java.lang.reflect.method.invoke(method.java:525) 04-20 19:10:43.198: e/androidruntime(4363): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:737) 04-20 19:10:43.198: e/androidruntime(4363): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) 04-20 19:10:43.198: e/androidruntime(4363): @ 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): @ java.lang.reflect.method.invokenative(native method) 04-20 19:10:43.198: e/androidruntime(4363): @ java.lang.reflect.method.invoke(method.java:525) 04-20 19:10:43.198: e/androidruntime(4363): @ 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): @ java.lang.integer.invalidint(integer.java:138) 04-20 19:10:43.198: e/androidruntime(4363): @ java.lang.integer.parse(integer.java:375) 04-20 19:10:43.198: e/androidruntime(4363): @ java.lang.integer.parseint(integer.java:366) 04-20 19:10:43.198: e/androidruntime(4363): @ java.lang.integer.parseint(integer.java:332) 04-20 19:10:43.198: e/androidruntime(4363): @ com.example.asyncfinalpractice.mainactivity.go(mainactivity.java:30) 04-20 19:10:43.198: e/androidruntime(4363): ... 14 more
any , appreciated. i'm kind of new android.
you're missing gettext()
call in method chaining.
it needs input.gettext().tostring()
.
the clue logcat output:
invalid int: "android.widget.edittext{...
that not input.
for number parsing, i'd recommend catch
ing exception. way input might have forgotten filter not crash program.
Comments
Post a Comment