2015-10-29 66 views
-2

上下文傳遞給我的課程的一個大問題。我嘗試了幾乎所有的東西:getApplicationContext()/ getBaseContext()/ getContext()- >不適用於自動完成/ this。 此時this幾乎工作(我的意思不是null)。應將哪個上下文傳遞給超類?

public class PersonActivity extends Activity implements OnTaskCompletedInterface { 

    public PersonAdapter listAdapter ; 

    public PersonActivity() { 
     try { 
      PersonGetAsync asyncGet = new PersonGetAsync(this, this, this); 
      asyncGet.execute().get; // supposed to block the UI I know 
     // With this, I catch the exception 
     }catch (Exception e) {e.printStackTrace();} 
    } 
public PersonAdapter getListAdapter() { 
     return listAdapter; 
    } 

有關詳細信息,這裏是孩子:

public class PersonBlueActivity extends PersonActivity { 

private ListView lv_Person; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_person_chooser_blue); 

    lv_Person = (ListView) findViewById(R.id.activity_person_chooser_blue_lv); 
    lv_Person.setAdapter(getListAdapter()); 
    lv_Person.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE); 
    lv_Person.setClickable(true); 
} 
@Override 
    public PersonAdapter getListAdapter() { 
     return super.getListAdapter(); 
    } 

這裏是異步

public class PersonGetAsync extends AsyncTask<String, Void, String> { 

    ProgressDialog dialog; 
    Activity mActivity; 
    Context mContext; 
    String response; 
    private OnTaskCompletedInterface listener; 

    public PersonGetAsync(Activity activity, Context context, OnTaskCompletedInterface listener) { 
     this.listener = listener; 
     mActivity = activity; 
     mContext = context; 
    } 

@Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     dialog = new ProgressDialog(mContext); 
     String str = "Connexion en cours"; 
     dialog.setTitle(str + "..."); 
     dialog.setIndeterminate(false); 
     dialog.setCancelable(true); 
     dialog.show(); 
    } 

    @Override 
    protected String doInBackground(String... params) { 
     System.setProperty("http.keepAlive", "false"); 
     response = ""; 
     try { 
      //Appel du webservice 
      HttpClient httpClient = new DefaultHttpClient(); 
      HttpGet request = new HttpGet(Globale.webURL+Globale.PERSON); 

      // Envoi de la requête GET 
      HttpResponse httpResponse = httpClient.execute(request); 
      response = EntityUtils.toString(httpResponse.getEntity()); 

      Log.v("Get : ", response); 

     } catch (Exception e) { 
      e.printStackTrace(); 
      return ""; 
     } 
     return response; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     listener.onTaskCompleted(result); 
     dialog.dismiss(); 
    } 

但是,因爲我的背景沒有通過,我不能去了這一步:DebugLog

它引起了我一個「空指針異常「爲什麼?

+3

您不應該在Activity構造函數中初始化它。您可能會獲得NPE,因爲該活動尚未建立。在onCreate()中執行此操作,然後立即執行此操作。 – DeeV

+0

謝謝DeeV,它的工作! – Nawako

回答

0

請勿在Activity子類上實現構造函數。

不要試圖使用Activity(調用它的方法或將它作爲參數傳遞給其他東西),直到調用onCreate()方法,直到調用super.onCreate()

您崩潰的原因是您的Activity在您嘗試將其與PersonGetAsync類一起使用時尚未初始化。延遲PersonGetAsync的創建和使用,直到super.onCreate()onCreate()方法PersonBlueActivity內撥打。

0

你需要做到這一點,背景應該給作爲活動這裏你試圖給參考的活動讓你的AsyncTask類是不能夠認識到,所以你需要給參考活動你是從喜歡與調用的AsyncTask:

ProgressDialog dialog; 
    Activity mActivity; 
    Context mContext; 
    String response; 
    private OnTaskCompletedInterface listener; 

    public PersonGetAsync(Activity activity, Context context, OnTaskCompletedInterface listener) { 
     this.listener = listener; 

     mContext = activity; 
    } 

@Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     dialog = new ProgressDialog(mContext); 
     String str = "Connexion en cours"; 
     dialog.setTitle(str + "..."); 
     dialog.setIndeterminate(false); 
     dialog.setCancelable(true); 
     dialog.show(); 
    } 

此外,我看到現在,你還沒有實現onCreate()方法中使用的製作構造函數可以調用異步中的onCreate()的activity.Instead

+0

我看不出評論活動如何解決問題。我測試過了,它不起作用。上下文可能存在問題,而不是活動。 – Nawako

+0

編輯我的答案,現在檢查 – Aakash

+0

我將活動傳遞給mContext,也得到java.lang.NullPointerException – Nawako

0

你永遠不應該使用一個Activity的實際構造函數。

覆蓋Activity生命週期功能並在那裏完成您的工作。

之所以你的背景是空當您嘗試與進度對話框使用它,是因爲它尚未設置。活動的上下文被設置在活動的生命週期函數中,並且在構造函數被首先調用時,這還沒有發生。

把你所有的構造函數代碼的onCreate,一切都會好起來的。 onCreate函數也被調用一次,就像構造函數一樣。

+0

謝謝,它工作!但CommonsWare是第一個回覆,對不起 – Nawako

相關問題