0

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

活动一

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" />

4

2 回答 2

0

your AsyncTask code has no problem !

problem may be with starting Activity

so try following code in onPostExecute()

final Intent mainIntent = new Intent(A.this,B.class);
startActivity(mainIntent);
finish();
return;
于 2013-07-29T06:11:01.863 回答
0

试试下面的代码片段

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

方法也sleeplong类型作为参数,尝试以下

 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
于 2013-07-29T06:48:24.000 回答