gpt4 book ai didi

android - 如何在 onPostExecute Asynctask 中设置 List Adapter?

转载 作者:搜寻专家 更新时间:2023-11-01 08:28:34 26 4
gpt4 key购买 nike

大家好

我这里有一些问题。我在 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();
}
}

}

最佳答案

有几种方法可以解决这个问题,其中一种快速且最脏的方法是:

像这样更改您的 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();

关于android - 如何在 onPostExecute Asynctask 中设置 List Adapter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42617773/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com