java - ASyncTask with progressdialog and button -
i beginner , had test. did tasks, have problem -
public class httptask extends asynctask<integer, string, string> {#### progressdialog dialog; context context; public httptask(activity activity) { //init progress dialog dialog = new progressdialog(context);**** dialog.setprogressstyle(progressdialog.style_horizontal); } protected void onpreexecute() { // show progress dialog dialog.setmessage("loading..."); dialog.setcancelable(false); } protected string doinbackground(integer... params) { //freeze system 5 seconds try { int seconds = params[0]*5;#### timeunit.seconds.sleep(seconds); } catch (interruptedexception e) { e.printstacktrace(); } return null; } @override protected void onpostexecute(final string success) { // if there progress dialog hide dialog.dismiss(); } }
it crashes, when try compile (i showed problems * sign):
08-03 10:43:10.873 29441-29441/? e/androidruntime﹕ fatal exception: main java.lang.nullpointerexception @ android.app.alertdialog.resolvedialogtheme(alertdialog.java:142) @ android.app.alertdialog.<init>(alertdialog.java:98) @ android.app.progressdialog.<init>(progressdialog.java:77) @ net.joerichard.androidtest.main.f.httptask.<init>(httptask.java:26) @ net.joerichard.androidtest.main.f.f_networking_activity$1.onclick(f_networking_activity.java:27) @ android.view.view.performclick(view.java:4107) @ android.view.view$performclick.run(view.java:17166) @ android.os.handler.handlecallback(handler.java:615) @ android.os.handler.dispatchmessage(handler.java:92) @ android.os.looper.loop(looper.java:155) @ android.app.activitythread.main(activitythread.java:5559) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1074) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:841) @ dalvik.system.nativestart.main(native method) 08-03 10:43:10.913 754-877/? e/embeddedlogger﹕ app crashed! process: net.joerichard.androidtest 08-03 10:43:10.913 754-877/? e/embeddedlogger﹕ app crashed! package: net.joerichard.androidtest v1 (1.0) 08-03 10:43:10.913 754-877/? e/embeddedlogger﹕ application label: androidtest
this class of main activity.
public class f_networking_activity extends actionbaractivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_f__networking_); // bdownload: start httptask button bdownload = (button) findviewbyid(r.id.bdownload); bdownload.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { httptask task = new httptask(f_networking_activity.this);**** task.execute(); } }); }
thank answers. have problem (i showed # sign of second problems)
08-03 11:28:18.292 754-877/? e/embeddedlogger﹕ app crashed! process: net.joerichard.androidtest' 08-03 11:28:18.292 754-877/? e/embeddedlogger﹕ app crashed! package: net.joerichard.androidtest v1 (1.0) 08-03 11:28:18.292 754-877/? e/embeddedlogger﹕ application label: androidtest 08-03 11:28:18.292 30544-30726/? e/androidruntime﹕ fatal exception: asynctask #1 java.lang.runtimeexception: error occured while executing doinbackground() @ android.os.asynctask$3.done(asynctask.java:299) @ java.util.concurrent.futuretask$sync.innersetexception(futuretask.java:273) @ java.util.concurrent.futuretask.setexception(futuretask.java:124) @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:307) @ java.util.concurrent.futuretask.run(futuretask.java:137) @ android.os.asynctask$serialexecutor$1.run(asynctask.java:230) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1076) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:569) @ java.lang.thread.run(thread.java:864) caused by: java.lang.arrayindexoutofboundsexception: length=0; index=0 @ net.joerichard.androidtest.main.f.httptask.doinbackground(httptask.java:40) @ net.joerichard.androidtest.main.f.httptask.doinbackground(httptask.java:20) @ android.os.asynctask$2.call(asynctask.java:287) @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:305) @ java.util.concurrent.futuretask.run(futuretask.java:137) @ android.os.asynctask$serialexecutor$1.run(asynctask.java:230) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1076) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:569) @ java.lang.thread.run(thread.java:864)
actually, context
null
because didn't initialize it.
add 1 line inside httptask
:
public httptask(activity activity) { this.context = activity; dialog = new progressdialog(context); dialog.setprogressstyle(progressdialog.style_horizontal); }
and change context
activity
this:
activity context;
now call context
anywhere in class.
Comments
Post a Comment