大家好,
我有点问题。我在asynctask doinbackground中进行web服务调用。
我想设置列表适配器,但出现了错误

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

现在如何在post execute中设置列表适配器?
我的代码
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();
        }
    }

}

最佳答案

有几种方法可以解决这个问题,其中最快捷和最肮脏的方法之一是:
更改异步任务活动,如下所示:

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

10-08 11:40