1

我想在 android 上做异步任务,我在不同的类上而不是在活动内部做。我的问题是当我想将 ProgressBar 可见性从 INVISIBLE 设置为 GONE 时,它返回 NullPointerException。我不明白为什么,这是我到目前为止的代码。

package com.ursabyte.boxi;

import android.os.AsyncTask;
import android.widget.ProgressBar;

public class SyncPercobaan extends AsyncTask<Object, Boolean, String> {

    AktifitasUtama ut;
    ManajemenFile mf;
    ProgressBar loading;


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        ProgressBar loading = (ProgressBar) ut.findViewById(R.id.loading);
        loading.setVisibility(ProgressBar.VISIBLE);
    }

    @Override
    protected void onPostExecute(String response) {
        super.onPostExecute(response);
        ProgressBar loading = (ProgressBar) ut.findViewById(R.id.loading);
        loading.setVisibility(ProgressBar.GONE);
        ut.browseToRoot();
    }

    @Override
    protected String doInBackground(Object... params) {
        String dir_name = (String) params[0];
        ut = (AktifitasUtama) params[1];
        ManajemenFile mf = new ManajemenFile(ut);
        mf.retrieve_user_dir();
        return dir_name;
    }
}

AktifitasUtama 是我的主要活动。

4

2 回答 2

0

这里

public class SyncPercobaan extends AsyncTask<Object, Boolean, String> {

    AktifitasUtama ut;  //<<--here

这不是从 Activity 访问 Ui 元素到 Activity 类的正确方法,如您的情况AsyncTask

为了从非 Activity 类访问 UI 元素,创建非 Activity 的构造函数:

public class SyncPercobaan extends AsyncTask<Object, Boolean, String> {
Context context;
    SyncPercobaan(Context context){
      this.context=context;
    }
  //your code here

现在context用于访问所有 UI 元素AktifitasUtama

于 2012-12-21T04:05:37.230 回答
0

答案是 context.runOnUiThread。就我而言,ut.runOnUiThread。这是代码:

public class SyncPercobaan extends AsyncTask<Object, Boolean, String> {

    AktifitasUtama ut;
    ManajemenFile mf;
    ManajemenKoneksi mk;
    SessionManager session;
    DatabaseHandler dh;


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(String response) {
        final String res = response; 
        super.onPostExecute(response);
        session = new SessionManager(ut);
        ut.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ut.toogle_indicator(false);
                ut.display_toast(res);
            }
        });
        ut.browseToRoot();
    }

    @Override
    protected String doInBackground(Object... params) {
        String dir_name = (String) params[0];
        ut = (AktifitasUtama) params[1];
        session = new SessionManager(ut);
        mk = new ManajemenKoneksi(ut);
        if(mk.is_connected()){
            ut.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ut.display_toast("Starting syncing...");
                    ut.toogle_indicator(true);
                }
            });     
            ManajemenFile mf = new ManajemenFile(ut);
            String result = mf.retrieve_user_dir(dir_name);
            if(result.equals("true")){
                return "Syncing completed...";
            } else {
                return result;
            }
        } else {
            return "Internet connection unavailable";
        }
    }

}

class Task extends TimerTask {
    AktifitasUtama au;
    String dir_name;
    Task(AktifitasUtama ut, String dir_name){
        au=ut;
        this.dir_name=dir_name;
    }

    public void run() {
        ManajemenFile mf = new ManajemenFile(au);
        mf.retrieve_user_dir(dir_name);
    }
}
于 2013-01-10T05:02:00.107 回答