> Where ctx is a context object. I understand now that you can't call
> methods from the UI thread in the AsyncTask.

You can call method on the UI thread in AsyncTask, that's what it's
for. But you cannot call method from other classes (like Context)
unless you have instances of them. This is no different than in any
other place of your application :)

>
> On Mar 7, 1:08 am, John Wesonga <[email protected]> wrote:
>> I figured something out which I thought would work, within my
>> LoginTask class I created an object which was of the parent activity:
>>
>> Test101 mParentActivity;
>>
>> and then:
>>
>> protected void onPostExecute(String result) {
>>
>>        Intent i=new Intent(ctx,SillyActivity.class);
>>        mParentActivity.startActivity(i);
>>
>> }
>>
>> An error is thrown:
>>
>> 03-07 01:04:50.259: ERROR/AndroidRuntime(801): Uncaught handler:
>> thread main exiting due to uncaught exception
>> 03-07 01:04:50.319: ERROR/AndroidRuntime(801):
>> java.lang.NullPointerException
>> 03-07 01:04:50.319: ERROR/AndroidRuntime(801):     at
>> org.tutorial.test101.tasks.LoginTask.onPostExecute(LoginTask.java:46)
>> 03-07 01:04:50.319: ERROR/AndroidRuntime(801):     at
>> org.tutorial.test101.tasks.LoginTask.onPostExecute(LoginTask.java:1)
>> 03-07 01:04:50.319: ERROR/AndroidRuntime(801):     at
>> android.os.AsyncTask.finish(AsyncTask.java:416)
>> 03-07 01:04:50.319: ERROR/AndroidRuntime(801):     at
>> android.os.AsyncTask.access$300(AsyncTask.java:127)
>> 03-07 01:04:50.319: ERROR/AndroidRuntime(801):     at
>> android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:428)
>> 03-07 01:04:50.319: ERROR/AndroidRuntime(801):     at
>> android.os.Handler.dispatchMessage(Handler.java:99)
>> 03-07 01:04:50.319: ERROR/AndroidRuntime(801):     at
>> android.os.Looper.loop(Looper.java:123)
>> 03-07 01:04:50.319: ERROR/AndroidRuntime(801):     at
>> android.app.ActivityThread.main(ActivityThread.java:3948)
>> 03-07 01:04:50.319: ERROR/AndroidRuntime(801):     at
>> java.lang.reflect.Method.invokeNative(Native Method)
>> 03-07 01:04:50.319: ERROR/AndroidRuntime(801):     at
>> java.lang.reflect.Method.invoke(Method.java:521)
>> 03-07 01:04:50.319: ERROR/AndroidRuntime(801):     at
>> com.android.internal.os.ZygoteInit
>> $MethodAndArgsCaller.run(ZygoteInit.java:782)
>> 03-07 01:04:50.319: ERROR/AndroidRuntime(801):     at
>> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
>> 03-07 01:04:50.319: ERROR/AndroidRuntime(801):     at
>> dalvik.system.NativeStart.main(Native Method)
>>
>> Stuck again it seems..
>>
>> On Mar 6, 10:57 pm, John Wesonga <[email protected]> wrote:
>>
>>
>>
>> > I just modified my onPostExecute(...) method:
>>
>> > protected void onPostExecute(String result) {
>>
>> >                         Log.i("LOGGER", "Done...");
>> >                         pDialog.dismiss();
>> >                         if(result=="success"){
>> >                                 Toast.makeText(ctx, "complete", 
>> > Toast.LENGTH_LONG).show();
>> >                                 Intent i=new 
>> > Intent(ctx,SillyActivity.class);
>> >                                 startActivity(i);
>> >                         }else{
>> >                                 Toast.makeText(ctx, "fail", 
>> > Toast.LENGTH_LONG).show();
>> >                         }
>>
>> >                 super.onPostExecute(result);
>> >         }
>>
>> > ctx is my context
>> > I noticed that I can't call the startActivity(Intent i) method within
>> > the AsyncTask, how else would I start an activity from here?
>>
>> > On Mar 6, 10:13 pm, Romain Guy <[email protected]> wrote:
>>
>> > > onPostExecute() is invoked on the UI thread so you can use this method
>> > > to start an Activity. It's pretty much what it's for.
>>
>> > > On Fri, Mar 5, 2010 at 6:29 AM, John Wesonga <[email protected]> 
>> > > wrote:
>> > > > I have a simple app and I'm using the AsyncTask to test out a
>> > > > background process, for clearness purposes I've opted to put my
>> > > > AsyncTask in a separate class rather than in an inner class, which
>> > > > where my problems begin, this is my AsyncTask
>>
>> > > > package org.tutorial.test101.tasks;
>>
>> > > > import android.app.ProgressDialog;
>> > > > import android.content.Context;
>> > > > import android.os.AsyncTask;
>> > > > import android.util.Log;
>>
>> > > > public class LoginTask extends AsyncTask<String, String, String> {
>>
>> > > >        Context ctx;
>> > > >        ProgressDialog pDialog;
>>
>> > > >        public LoginTask(Context context){
>> > > >                super();
>> > > >                this.ctx=context;
>> > > >        }
>> > > >       �...@override
>> > > >        protected String doInBackground(String... params) {
>> > > >                Log.i("LOGGER", "Starting...");
>> > > >                try{
>> > > >                        Thread.sleep(8000);
>> > > >                }catch(InterruptedException e){
>>
>> > > >                }
>> > > >                return null;
>> > > >        }
>>
>> > > >       �...@override
>> > > >        protected void onPostExecute(String result) {
>>
>> > > >                        Log.i("LOGGER", "Done...");
>> > > >                        pDialog.dismiss();
>> > > >                      //
>> > > >                super.onPostExecute(result);
>> > > >        }
>>
>> > > >       �...@override
>> > > >        protected void onPreExecute() {
>> > > >                pDialog=new ProgressDialog(ctx);
>> > > >                pDialog.setTitle("Login");
>> > > >                pDialog.setMessage("doing stuff..");
>> > > >                pDialog.show();
>> > > >        }
>>
>> > > > }
>>
>> > > > What I'd like is that once the task is complete, I do something in the
>> > > > UI thread may be start another activity or even display a Toast saying
>> > > > activity complete. I know I can't start a new Activity in the
>> > > > onPostExecute() so I need to start it in the UI thread any idea how I
>> > > > can do this? This is my Activity that starts the AsyncTask
>>
>> > > > package org.tutorial.test101.activities;
>>
>> > > > import org.tutorial.test101.tasks.LoginTask;
>>
>> > > > import android.app.Activity;
>> > > > import android.app.Dialog;
>> > > > import android.content.Context;
>> > > > import android.os.AsyncTask;
>> > > > import android.os.Bundle;
>> > > > import android.util.Log;
>> > > > import android.view.View;
>> > > > import android.view.View.OnClickListener;
>> > > > import android.widget.Button;
>>
>> > > > public class Test101 extends Activity{
>> > > >        private Button btnLogin;
>> > > >        private LoginTask mLoginTask;
>> > > >        private Context context=this;
>> > > >    /** Called when the activity is first created. */
>> > > >   �...@override
>> > > >    public void onCreate(Bundle savedInstanceState) {
>> > > >        super.onCreate(savedInstanceState);
>> > > >        setContentView(R.layout.main);
>> > > >        btnLogin=(Button)findViewById(R.id.btnLogin);
>> > > >        btnLogin.setOnClickListener(new OnClickListener(){
>>
>> > > >                        public void onClick(View v) {
>> > > >                                if(mLoginTask==null){
>> > > >                                        mLoginTask=new 
>> > > > LoginTask(context);
>> > > >                                        mLoginTask.execute(null);
>> > > >                                }
>>
>> > > >                        }
>>
>> > > >        });
>> > > >    }
>>
>> > > > }
>>
>> > > > --
>> > > > You received this message because you are subscribed to the Google
>> > > > Groups "Android Developers" group.
>> > > > To post to this group, send email to 
>> > > > [email protected]
>> > > > To unsubscribe from this group, send email to
>> > > > [email protected]
>> > > > For more options, visit this group at
>> > > >http://groups.google.com/group/android-developers?hl=en
>>
>> > > --
>> > > Romain Guy
>> > > Android framework engineer
>> > > [email protected]
>>
>> > > Note: please don't send private questions to me, as I don't have time
>> > > to provide private support.  All such questions should be posted on
>> > > public forums, where I and others can see and answer them
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>



-- 
Romain Guy
Android framework engineer
[email protected]

Note: please don't send private questions to me, as I don't have time
to provide private support.  All such questions should be posted on
public forums, where I and others can see and answer them

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to