android中的简单异步任务

时间:2013-12-24 10:32:50

标签: android asynchronous

我正在尝试了解android

中的异步任务

我的工作 ::我使用单个按钮启动空AsynchronousTask,当我们点击异步对话框Async之外的活动取消活动时,一个Toast消息是显示。


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();

        }
    }
}

登录 ::

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)

如何解决此问题

6 个答案:

答案 0 :(得分:2)

pDialog.dismiss(); pDialog为空

onPreExecuteonPostExecute之前执行。

将进度对话框的初始化移至onPreExecute。然后在doInbackground中完成后台工作。在onPostExecute

中关闭对话框

参考:

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

答案 1 :(得分:2)

您只需移动代码

  

onPreExecute()到onPostExecute()

  

onPostExecute()到onPreExecute()

并设置

  

pDialog.setCancelable(假);

答案 2 :(得分:1)

试试这个,

 @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();


        }

此处,AsyncTask先致电onPreExecute(),第二次致电doInBackground(),最后致电onPostExecute()。因此,您需要在onPreExecute()内初始化内容。

答案 3 :(得分:1)

交换onPreExecute()和onPostExexcute()

的代码

你的代码

 @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();
 }

将其更改为

 @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();
  }

答案 4 :(得分:1)

您错误地编写了progressdialog的代码。它应该在onPreExecute方法中显示,并在onPostExecute方法中解除,如下所示:

更改您的代码如下:

   @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();


    }

答案 5 :(得分:1)

使用此代码:

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();


    }
}
}