onPostExecute()没有执行

时间:2013-07-29 06:01:45

标签: android android-widget android-asynctask android-progressbar

当进度条达到最大值时,我想从一个活动切换到另一个活动。

活动A

public class A extends Activity {
private ProgressBar progressBar;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_LEFT_ICON);
    setContentView(R.layout.mainpage);
    getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,R.drawable.main);
    progressBar = (ProgressBar) findViewById(R.id.prog_bar);    

    new AsyncTask<Void, Integer, Integer>(){

        @Override
        protected Integer doInBackground(Void... params) {
            int progressStatus = 0;
            while (progressStatus < 5000) {
                progressStatus++;
                publishProgress(progressStatus);
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
            return 1;  
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            progressBar.setProgress(values[0]);
        }

        protected void onPostExecute(Integer result) {
            final Intent mainIntent = new Intent(A.this,B.class);
            startActivity(mainIntent);
            finish();
            return;
        }
    }.execute();
}

}

但是在这段代码中,onPostExecute()方法没有执行...我也遇到了aSyncTask的参数... 当我在调试模式下运行此代码时,它从try块

返回
  

尝试{                           了Thread.sleep(1);                       } catch(InterruptedException e){                           e.printStackTrace();                       }

此类的XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="5dp"
android:background="@drawable/eggstation"
tools:context=".A" >

<TextView
    android:id="@+id/dutech"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="-5dp"
    android:text="@string/text_data"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#00cccc"
    android:textSize="25sp" />

<TextView
    android:id="@+id/devby"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/dutech"
    android:layout_marginBottom="5dp"
    android:gravity="center"
    android:text="Developed by:"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#3399cc"
    android:textSize="28sp" />

<ProgressBar
    android:id="@+id/prog_bar"
    style="@android:style/Widget.ProgressBar.Horizontal"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:max="100"   
    android:layout_alignParentTop="true" />

2 个答案:

答案 0 :(得分:0)

您的AsyncTask代码没问题!

问题可能出在启动活动

请尝试按照onPostExecute()

中的代码进行操作
final Intent mainIntent = new Intent(A.this,B.class);
startActivity(mainIntent);
finish();
return;

答案 1 :(得分:0)

尝试以下代码段

 protected void onPostExecute(Integer result) {
        startActivity(new Intent(A.this,B.class));
        return;
    }

同样sleep方法将long类型作为参数,请尝试以下

 Thread.sleep(1000);

同时在B注册您的Manifest.xml活动,如下所示

<application>
...
...
<activity
        android:name="[package_name].B"
        android:label="@string/class_b" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="[package_name].MainActivity" />
 </activity>
</application