【问题标题】:onPostExecute() not executingonPostExecute() 未执行
【发布时间】:2013-07-29 06:01:44
【问题描述】:

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

活动 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 块中返回

试试{ 线程.sleep(1); } 捕捉(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" />

【问题讨论】:

  • 有什么例外或任何事情吗?记录 cat plz.. 清单中指定的活动 r
  • 没有例外,或者在清单中也指定了任何东西和活动......
  • 在异步任务中,onPostexecute 方法是一种覆盖方法,但在您的代码中我找不到它。
  • @ShaniGoriwal,它不是必需的。如果未写入“覆盖”,它也会执行。 :)
  • 在清单和所有程序中指定活动后,你的代码对我来说可以正常工作...... gd 如果你发布所有源代码......还有 B 活动......

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


【解决方案1】:

您的AsyncTask 代码没有问题!

问题可能在于启动 Activity

所以试试onPostExecute()中的代码

final Intent mainIntent = new Intent(A.this,B.class);
startActivity(mainIntent);
finish();
return;

【讨论】:

    【解决方案2】:

    试试下面的代码sn-p

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

    还有sleep方法以long类型作为参数,试试下面的方法

     Thread.sleep(1000);
    

    还可以在Manifest.xml 中注册您的B 活动,如下所示

    <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
    

    【讨论】:

    • naaaaaa .....不工作 B 已经注册,我做了 Thread.sleep(1000)...在调试模式下,当它到达返回语句...它到达第一行代码(声明包的地方)..它没有执行 onPostExecute()...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-25
    • 2016-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多