2017-03-06 5 views
1

Guten Tag alle,Wie setze ich Listenadapter in onPostExecute Asynctask?

Ich habe ein Problem hier. Ich mache einen Web-Service-Anruf in AsyncTask DoInBackground.

Und ich wollte Liste Adapter setzen, aber ich habe Fehler

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

Nun, wie Liste Adapter in der Post ausführen oder etwas einstellen?

Mein Code

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

überprüfen, ob 'mItems' Liste Wert hat nach dem Parsen oder auch initialisieren' mItems = new Arraylist <>(); '' – Mrinmoy

Antwort

0

bewegen sie von onCreate zu OnPostExecute() Instanz Adapter definieren als globale Variable dann Angebote im doInBackground add() & Anruf setAdapter onPostExec.

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

Es gibt mehrere Möglichkeiten, dies zu lösen, Einer der schnellen und schmutzigsten ist:

ändern AsyncTaskActivity So:

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

Und Sie können die AsyncTask wie folgt aufrufen:

1

Initialisieren Sie Elemente wie

mItems = new ArrayList<>(); 

Objekt erstellen für ListProfileAdapter

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

Artikel hinzufügen in mItems

mItems.add(item); 

Nachdem alle Elemente in die Liste benachrichtigen Sie den Adapter

adapter.notifyDataSetChanged(); 
+1

mItems = new Arraylist <>(); 'Diese Zeile ist der Grund für den Fehler .. Er muss diese Zeile in der onCreate-Methode hinzufügen –

0

ich die folgende Zeile denken hinzugefügt sollte vor der Einstellung Ihres Adapters,

die Instanz-Variable Stellen,

ListProfileAdapter adapter; 

stattdessen in der AsyncTask zu stellen. Nach dem AsyncTask in OnPostExecute Finishing Sie auch ausführen müssen,

adapter.notifyDatasetChanged(); 
Verwandte Themen