2012-12-21 61 views
1

我想在android上執行Asychronous任務,我在不同的類而不是在活動內部做了這個。我的問題是,當我想從INVISIBLE到GONE設置ProgressBar可見性時,它返回NullPointerException。我不明白爲什麼,這是我的代碼。Toogle ProgressBar從不同的AsyncTask類返回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是我的主要活動。

回答

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); 
    } 
} 
0

這裏

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

    AktifitasUtama ut; //<<--here 

擺脫活動訪問UI元素上的活動類作爲你的情況在AsyncTask

訪問非活動類的UI元素創建的非的構造這不是正確的方式活性:

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

現在使用context從訪問所有UI元素0

+0

我會嘗試這個 –

+0

我怎麼得到進度,起初我使用UT,但我不知道這一次:■ –

+0

@tegaralaga:參見[這] (http://stackoverflow.com/questions/6030982/android-how-to-access-activity-ui-from-my-class)從非Activity類訪問Ui –