Devrath
Devrath

Reputation: 42824

Simple Asynchronous Task in android

I am trying to learn about Asynchronous Task in android


My work:: I have used a single button to launch a empty AsynchronousTask, when we click outside of asynchronous dialog box Async activity canceled and a toast message is displayed.


MainActivity.java

public class MainActivity extends Activity {

    Button btn;
    ProgressDialog pDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn=(Button) findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                new MyAsyncTask().execute();
            }
        });
    }


    class MyAsyncTask extends AsyncTask<String, integer, String>{

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            pDialog=new ProgressDialog(MainActivity.this);
            pDialog.setCancelable(true);
            pDialog.setMessage("AsynchronousTaskRunning...");
            pDialog.show();
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();

            pDialog.dismiss();
            Toast.makeText(MainActivity.this, "We have come out of Asynchronous task", Toast.LENGTH_LONG).show();

        }
    }
}

log::

12-24 15:55:26.747: E/AndroidRuntime(19916): Uncaught handler: thread main exiting due to uncaught exception
12-24 15:55:26.975: E/AndroidRuntime(19916): java.lang.NullPointerException
12-24 15:55:26.975: E/AndroidRuntime(19916):    at com.example.asynchronoustask.MainActivity$MyAsyncTask.onPreExecute(MainActivity.java:57)
12-24 15:55:26.975: E/AndroidRuntime(19916):    at android.os.AsyncTask.execute(AsyncTask.java:391)
12-24 15:55:26.975: E/AndroidRuntime(19916):    at com.example.asynchronoustask.MainActivity$1.onClick(MainActivity.java:29)
12-24 15:55:26.975: E/AndroidRuntime(19916):    at android.view.View.performClick(View.java:2364)
12-24 15:55:26.975: E/AndroidRuntime(19916):    at android.view.View.onTouchEvent(View.java:4179)
12-24 15:55:26.975: E/AndroidRuntime(19916):    at android.widget.TextView.onTouchEvent(TextView.java:6541)
12-24 15:55:26.975: E/AndroidRuntime(19916):    at android.view.View.dispatchTouchEvent(View.java:3709)
12-24 15:55:26.975: E/AndroidRuntime(19916):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
12-24 15:55:26.975: E/AndroidRuntime(19916):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
12-24 15:55:26.975: E/AndroidRuntime(19916):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
12-24 15:55:26.975: E/AndroidRuntime(19916):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
12-24 15:55:26.975: E/AndroidRuntime(19916):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
12-24 15:55:26.975: E/AndroidRuntime(19916):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
12-24 15:55:26.975: E/AndroidRuntime(19916):    at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
12-24 15:55:26.975: E/AndroidRuntime(19916):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
12-24 15:55:26.975: E/AndroidRuntime(19916):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
12-24 15:55:26.975: E/AndroidRuntime(19916):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-24 15:55:26.975: E/AndroidRuntime(19916):    at android.os.Looper.loop(Looper.java:123)
12-24 15:55:26.975: E/AndroidRuntime(19916):    at android.app.ActivityThread.main(ActivityThread.java:4363)
12-24 15:55:26.975: E/AndroidRuntime(19916):    at java.lang.reflect.Method.invokeNative(Native Method)
12-24 15:55:26.975: E/AndroidRuntime(19916):    at java.lang.reflect.Method.invoke(Method.java:521)
12-24 15:55:26.975: E/AndroidRuntime(19916):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-24 15:55:26.975: E/AndroidRuntime(19916):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-24 15:55:26.975: E/AndroidRuntime(19916):    at dalvik.system.NativeStart.main(Native Method)

How to resolve this !

Upvotes: 0

Views: 151

Answers (6)

Raghunandan
Raghunandan

Reputation: 133560

pDialog.dismiss(); pDialog is null

onPreExecuteis executed before onPostExecute.

Move the initialization of progress dialog to onPreExecute. Then do your background work in doInbackground. Dismiss dialog in onPostExecute.

Reference :

http://developer.android.com/reference/android/os/AsyncTask.html

Upvotes: 2

M D
M D

Reputation: 47807

You just move your code

onPreExecute() to onPostExecute()

and

onPostExecute() to onPreExecute()

and also set

pDialog.setCancelable(false);

Upvotes: 2

Kailash Dabhi
Kailash Dabhi

Reputation: 3513

use this code:

public class MainActivity extends Activity {

Button btn;
ProgressDialog pDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btn=(Button) findViewById(R.id.button1);
    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            new MyAsyncTask().execute();
        }
    });
}


class MyAsyncTask extends AsyncTask<String, integer, String>{

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
       pDialog.dismiss();
        Toast.makeText(MainActivity.this, "We have come out of Asynchronous task", Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
       pDialog=new ProgressDialog(MainActivity.this);
        pDialog.setCancelable(true);
        pDialog.setMessage("AsynchronousTaskRunning...");
        pDialog.show();


    }
}
}

Upvotes: 1

GrIsHu
GrIsHu

Reputation: 23638

You have wrongly written the code of progressdialog. It should be shown in the onPreExecute method and dismissed in onPostExecute method as below:

Change your code as below:

   @Override
    protected void onPostExecute(String result) {
       pDialog.dismiss();
        Toast.makeText(MainActivity.this, "We have come out of Asynchronous task", Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
     // TODO Auto-generated method stub
        pDialog=new ProgressDialog(MainActivity.this);
        pDialog.setCancelable(true);
        pDialog.setMessage("AsynchronousTaskRunning...");
        pDialog.show();


    }

Upvotes: 1

Vaibhav Agarwal
Vaibhav Agarwal

Reputation: 4499

interchange the code of both onPreExecute() and onPostExexcute()

your code

 @Override
 protected void onPostExecute(String result)
 {
     // TODO Auto-generated method stub
     pDialog=new ProgressDialog(MainActivity.this);
     pDialog.setCancelable(true);
     pDialog.setMessage("AsynchronousTaskRunning...");
     pDialog.show();
 }

 @Override
 protected void onPreExecute() 
 {
     // TODO Auto-generated method stub
     super.onPreExecute();
     pDialog.dismiss();
     Toast.makeText(MainActivity.this, "We have come out of Asynchronous task", Toast.LENGTH_LONG).show();
 }

Change it to

 @Override
 protected void onPostExecute(String result) 
 {
    // TODO Auto-generated method stub
    pDialog.dismiss();
    Toast.makeText(MainActivity.this, "We have come out of Asynchronous task", Toast.LENGTH_LONG).show();
 }

 @Override
 protected void onPreExecute()
 {
     // TODO Auto-generated method stub
     super.onPreExecute();
     pDialog=new ProgressDialog(MainActivity.this);
     pDialog.setCancelable(true);
     pDialog.setMessage("AsynchronousTaskRunning...");
     pDialog.show();
  }

Upvotes: 1

SathishKumar
SathishKumar

Reputation: 1644

Try this,

 @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            pDialog.dismiss();
            Toast.makeText(MainActivity.this, "We have come out of Asynchronous task", Toast.LENGTH_LONG).show();
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            pDialog=new ProgressDialog(MainActivity.this);
            pDialog.setCancelable(true);
            pDialog.setMessage("AsynchronousTaskRunning...");
            pDialog.show();


        }

Here, AsyncTask first call onPreExecute(), second calls doInBackground() and finally calls onPostExecute(). So you need to do initializing stuff inside onPreExecute().

Upvotes: 1

Related Questions