setOnTouchListener AsyncTask新活动

时间:2014-03-14 05:18:26

标签: android android-asynctask android-activity android-progressbar

我想在触发事件发布和asynctask结束时启动其他活动。,所以我这样做:

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.home);

    image = (ImageView) findViewById(R.id.iVHome);

    progressbar = (ProgressBar) findViewById(R.id.pbHome);

    progressbar.setVisibility(View.INVISIBLE);    

    image.setOnTouchListener(new View.OnTouchListener() 
    {
        @Override
        public boolean onTouch(View v, MotionEvent event)
        {
            if(event.getAction()==MotionEvent.ACTION_DOWN && star==true)
            { 
                new ProgressBarAsyncTask().execute();
            }
            return true;
        }
    });
}

这是我的asynctask,doInBackground是一个模拟我将做什么任务,在帖子上,我把看不见的进度条和我试图启动新活动,但是我得到了错误模拟器和我的手机

public class ProgressBarAsyncTask extends AsyncTask<Void, Integer, Void> 
{
    int myProgress;
    @Override
    protected void onPostExecute(Void result) 
    { 
        progressbar.setVisibility(View.INVISIBLE);
        inicio=false;
        Intent goMain = new Intent();
        goMain.setClass(HomeActivity.this, MainActivity.class);
        startActivity(goMain);
    } 
    @Override
    protected void onPreExecute() 
    { 
        myProgress = 0;
        progressbar.setVisibility(View.VISIBLE);
    } 
    @Override
    protected Void doInBackground(Void... params) 
    { 
     //THIS IS A SIMULATION, HERE I WILL DO SOME TASKS
        while(myProgress<100)
        {
            myProgress++; 
            publishProgress(myProgress);
            SystemClock.sleep(100);
        }
        return null;
    } 

 } 

我怎么做?请healp

0 个答案:

没有答案