2017-03-06 55 views
1

美好的一天大家,如何在onPostExecute Asynctask中設置列表適配器?

我在這裏有一些問題。我在AsyncTask DoInBackground中進行Web服務調用。

,我想設置列表適配器,但我得到了錯誤

java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference 

現在怎麼設置列表適配器後執行什麼?

我的代碼

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_profile); 
    progressBar = (ProgressBar)findViewById(R.id.prgLoading); 


    //Initialize the ListView 
    final ListView lvProf = (ListView)findViewById(R.id.lvProfile); 

    //call the asynctask cals and add item to the list. 
    new LoadDataForActivity().execute(); 

    //Set adapter , but i got error here 
    lvProf.setAdapter(new ListProfileAdapter(this,mItems)); 



} 

private class LoadDataForActivity extends AsyncTask<Void, Void, Void> { 
    @Override 
    protected void onPreExecute() { 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, 
       WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE); 
     progressBar.setVisibility(View.VISIBLE); 
     progressBar.setIndeterminate(false); 
     progressBar.setClickable(false); 
    } 
    @Override 
    protected Void doInBackground(Void... params) { 

     getAll(); 
     getTotalLeaveBalance(); 
     getMedicalBalance(); 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
    //here is I add the item to my list (mitems) 
     try{ 
      ResponseServiceMedicalBalance = ResponseServiceMedicalBalance.replace("\\\"", "\""); 
      ResponseServiceMedicalBalance = ResponseServiceMedicalBalance.substring(1, ResponseServiceMedicalBalance.length() - 1); 

      JSONParser jsonParser = new JSONParser(); 
      JSONObject jsonObject = (JSONObject) jsonParser.parse(ResponseServiceMedicalBalance); 
      String Status = jsonObject.get("Status").toString(); 

      if (Status == "true") { 
       // JSONArray structure = (JSONArray) jsonObject.get("DataList"); 
       String dataku = jsonObject.get("DataList").toString(); 
       mItems = new ArrayList<ListProfileItem>(); 
       try { 
        dataku = ANGGACRYYPT.decrypt(Enc_Pass, dataku); 
       }catch (GeneralSecurityException e){ 
        //handle error - could be due to incorrect password or tampered encryptedMsg 
       } 

       JSONParser parser = new JSONParser(); 
       JSONArray structure = (JSONArray) parser.parse(dataku); 
       for (int i = 0; i < structure.size(); i++) { 
        JSONObject data = (JSONObject) structure.get(i); 
        item = new ListProfileItem(); 
        item.claimpostname = data.get("claim_post_name").toString(); 
        String claimamount = data.get("max_limit_per_year").toString(); 
        if (claimamount!=("0.0")) 
        { 
         Double amount = Double.parseDouble(claimamount); 
         DecimalFormat formatter = new DecimalFormat("#,###.00"); 
         String AmountFormatted = formatter.format(amount); 
         item.claimpostamount = AmountFormatted; 
        } 
        else 
        { 
         item.claimpostamount = data.get("max_limit_per_year").toString(); 
        } 
        mItems.add(item); 
       } 
       // initialize and set the list adapter 

      } 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 

} 
+0

檢查,如果'mItems'列表中包含了解析後或者初始化'值後mItems =新的ArrayList <>();'' – Mrinmoy

回答

0

移動它的onCreate到onPostExecute() 定義適配器的實例作爲全局變量,然後在doInBackground()調用& setAdapter onPostExec添加項目。

//Set adapter , but i got error here >> move it to onPostExecute 
    lvProf.setAdapter(new ListProfileAdapter(this,mItems)); 
1

有解決這個,一個快速的幾種方式和最骯髒的是:

更改AsyncTaskActivity像這樣:

private class LoadDataForActivity extends AsyncTask<Void, Void, Void> { 


    private ListView listView; 

    private Context context; 

    public LoadDataForActivity(ListView listView,Context context){ 
    this. listView = listView; 
    this.context = context; 
    } 

    @Override 
    protected void onPreExecute() { 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, 
       WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE); 
     progressBar.setVisibility(View.VISIBLE); 
     progressBar.setIndeterminate(false); 
     progressBar.setClickable(false); 
    } 
    @Override 
    protected Void doInBackground(Void... params) { 

     getAll(); 
     getTotalLeaveBalance(); 
     getMedicalBalance(); 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
    //here is I add the item to my list (mitems) 
     try{ 
      ResponseServiceMedicalBalance = ResponseServiceMedicalBalance.replace("\\\"", "\""); 
      ResponseServiceMedicalBalance = ResponseServiceMedicalBalance.substring(1, ResponseServiceMedicalBalance.length() - 1); 

      JSONParser jsonParser = new JSONParser(); 
      JSONObject jsonObject = (JSONObject) jsonParser.parse(ResponseServiceMedicalBalance); 
      String Status = jsonObject.get("Status").toString(); 

      if (Status == "true") { 
       // JSONArray structure = (JSONArray) jsonObject.get("DataList"); 
       String dataku = jsonObject.get("DataList").toString(); 
       mItems = new ArrayList<ListProfileItem>(); 
       try { 
        dataku = ANGGACRYYPT.decrypt(Enc_Pass, dataku); 
       }catch (GeneralSecurityException e){ 
        //handle error - could be due to incorrect password or tampered encryptedMsg 
       } 

       JSONParser parser = new JSONParser(); 
       JSONArray structure = (JSONArray) parser.parse(dataku); 
       for (int i = 0; i < structure.size(); i++) { 
        JSONObject data = (JSONObject) structure.get(i); 
        item = new ListProfileItem(); 
        item.claimpostname = data.get("claim_post_name").toString(); 
        String claimamount = data.get("max_limit_per_year").toString(); 
        if (claimamount!=("0.0")) 
        { 
         Double amount = Double.parseDouble(claimamount); 
         DecimalFormat formatter = new DecimalFormat("#,###.00"); 
         String AmountFormatted = formatter.format(amount); 
         item.claimpostamount = AmountFormatted; 
        } 
        else 
        { 
         item.claimpostamount = data.get("max_limit_per_year").toString(); 
        } 
        mItems.add(item); 
       } 

    listView.setAdapter(new ListProfileAdapter(context,mItems)); 


      } 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 
    } 

而且你可以調用的AsyncTask是這樣的:

new LoadDataForActivity(listView,this).execute(); 
1

初始化模塊爲

mItems = new ArrayList<>(); 

創建對象ListProfileAdapter

ListProfileAdapter adapter = new ListProfileAdapter(this,mItems); 
lvProf.setAdapter(adapter); 

添加項mItems

mItems.add(item); 

後的所有項目添加到列表通知適配器

adapter.notifyDataSetChanged(); 
+1

= mItems新的ArrayList <> ();'這一行是錯誤的原因..他需要在onCreate方法中添加這一行 –

0

我覺得下面的行應在設置您的適配器之前,

讓實例變量,

ListProfileAdapter adapter; 

而不是把在的AsyncTask。在完成的AsyncTask在onPostExecute還需要磨合,

adapter.notifyDatasetChanged(); 
相關問題