您好我正在使用android Fragments。我在使用异步任务从服务器加载json时创建了一个进度对话框。但它工作正常,有时会导致窗口泄漏错误。我在onPreExcecute方法中创建对话并在dobackground中加载内容并在onpost excecute方法中解除我的对话。我认为这是正确的方法。但是为什么我有时会导致这个窗口泄漏错误?这是我的代码。请帮助我提前谢谢:)
public class PendingWork extends Fragment {
ListView lv;
public ProgressDialog pDialog;
EditText inputSearch;
// Creating JSON Parser object
JSONParser jsonParser = new JSONParser();
static ArrayList<HashMap<String, Object>> pendingList = new ArrayList<HashMap<String, Object>>();
SharedPreferences app_pref;
SharedPreferences.Editor appedt;
static JSONArray jArray2 = null;
String picture, complaint_id[],complaint_type[],engine_model[],customer_id[],customer_name[],customer_address[],description[],date[];
// Inbox JSON url
final String PENDING_URL = "myURl";
// ALL JSON node names
private static final String TAG_ID = "cmp_id";
private static final String TAG_TYPE = "cmp_type";
private static final String TAG_ENGINE = "engine";
private static final String TAG_NAME = "cust_name";
private static final String TAG_DATE = "date";
private static final String TAG_DESC = "descriptn";
private String TAG_PIC;
int textlength=0;
//Date strDate;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_pending_list, container, false);
inputSearch = (EditText)rootView. findViewById(R.id.inputSearch);
// Hashmap for ListView
HashMap<String, Object> map = new HashMap<String, Object>();
pendingList.clear();
// Loading pending list in Background Thread
new Loadpending().execute();
lv = (ListView)rootView.findViewById(R.id.list);
lv.setTextFilterEnabled(true);
// lv.setBackgroundResource(R.drawable.bg);
app_pref = getActivity().getSharedPreferences("MY_PREF",getActivity().MODE_PRIVATE);
appedt=app_pref.edit();
/*--------------------------------------------------listview click listener------------------------------------------------*/
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> apt, View v, int pos,
long id) {
// TODO Auto-generated method stub
appedt.putString("complt_id", complaint_id[pos]);
// appedt.putString("imei", IMEI_no);
appedt.commit();
Bundle b = new Bundle();
b.putString("type", complaint_type[pos]);
b.putString("id", complaint_id[pos]);
b.putString("date", date[pos]);
b.putString("engine", engine_model[pos]);
b.putString("cus_name", customer_name[pos]);
b.putString("cus_id", customer_id[pos]);
b.putString("cus_addr", customer_address[pos]);
b.putString("desc", description[pos]);
b.putString("flag", "1");
Intent in=new Intent(getActivity(),Pend_Details.class);
in.putExtras(b);
startActivity(in);
}
});
inputSearch.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable s)
{
// Abstract Method of TextWatcher Interface.
}
public void beforeTextChanged(CharSequence s,
int start, int count, int after)
{
// Abstract Method of TextWatcher Interface.
}
public void onTextChanged(CharSequence s,
int start, int before, int count)
{
textlength = inputSearch.getText().length();
pendingList.clear();
for (int j = 0; j < jArray2.length(); j++)
{
try {
if (textlength <= jArray2.getJSONObject(j).length())
{
if(inputSearch.getText().toString().equalsIgnoreCase((String)((CharSequence) jArray2.getJSONObject(j).getString("complnt_type")).subSequence(0,textlength)) ||
inputSearch.getText().toString().equalsIgnoreCase((String)((CharSequence) jArray2.getJSONObject(j).getString("name")).subSequence(0,textlength)) )
{
SetList(j);
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
ListAdapter adapter = new SimpleAdapter(
getActivity(), pendingList,
R.layout.fragment_pending_list_item2, new String[] { TAG_TYPE, TAG_NAME, TAG_DATE},
new int[] { R.id.title,R.id.name, R.id.location});
// updating listview
lv.setAdapter(adapter);
}
});
return rootView;
}
class Loadpending extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pendingList.clear();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Loading ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting Inbox JSON
* */
protected String doInBackground(String... args) {
app_pref=getActivity().getSharedPreferences("MY_PREF", 0);
String tech_id = app_pref.getString("username", "");
jArray2 = jsonParser.ParseJson(PENDING_URL, "GET",tech_id);
//Log.w("Lengh",""+jArray2.length());
complaint_id = new String[jArray2.length()];
complaint_type = new String[jArray2.length()];
engine_model = new String[jArray2.length()];
customer_id = new String[jArray2.length()];
customer_name = new String[jArray2.length()];
customer_address = new String[jArray2.length()];
description = new String[jArray2.length()];
date = new String[jArray2.length()];
pendingList.clear();
for (int i = 0; i < jArray2.length(); i++) {
SetList(i);
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
// updating UI from Background Thread
getActivity().runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
getActivity(), pendingList,
R.layout.fragment_pending_list_item2, new String[] { TAG_TYPE, TAG_NAME, TAG_DATE},
new int[] { R.id.title,R.id.name, R.id.location });
// updating listview
lv.setAdapter(adapter);
pDialog.dismiss();
}
});
}
}
}
答案 0 :(得分:1)
在显示ProgressDialog
之前添加此检查if(!isCancelled())
{
dialog.show();
}