我试图让我的应用程序每15秒自动重新加载,但是当我执行该功能时,我得到一个窗口泄漏错误。
错误日志:
09-16 23:40:17.708: E/WindowManager(2087): android.view.WindowLeaked: Activity com.weatherapp.piwe.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{b19372c0 V.E..... R.....ID 0,0-729,192} that was originally added here
09-16 23:40:17.708: E/WindowManager(2087): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:348)
09-16 23:40:17.708: E/WindowManager(2087): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
09-16 23:40:17.708: E/WindowManager(2087): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
09-16 23:40:17.708: E/WindowManager(2087): at android.app.Dialog.show(Dialog.java:286)
09-16 23:40:17.708: E/WindowManager(2087): at com.weatherapp.piwe.MainActivity$JSONWeatherTask.onPreExecute(MainActivity.java:166)
09-16 23:40:17.708: E/WindowManager(2087): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
09-16 23:40:17.708: E/WindowManager(2087): at android.os.AsyncTask.execute(AsyncTask.java:535)
09-16 23:40:17.708: E/WindowManager(2087): at com.weatherapp.piwe.MainActivity$2$1.run(MainActivity.java:406)
09-16 23:40:17.708: E/WindowManager(2087): at android.os.Handler.handleCallback(Handler.java:733)
09-16 23:40:17.708: E/WindowManager(2087): at android.os.Handler.dispatchMessage(Handler.java:95)
09-16 23:40:17.708: E/WindowManager(2087): at android.os.Looper.loop(Looper.java:136)
09-16 23:40:17.708: E/WindowManager(2087): at android.app.ActivityThread.main(ActivityThread.java:5017)
09-16 23:40:17.708: E/WindowManager(2087): at java.lang.reflect.Method.invokeNative(Native Method)
09-16 23:40:17.708: E/WindowManager(2087): at java.lang.reflect.Method.invoke(Method.java:515)
09-16 23:40:17.708: E/WindowManager(2087): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-16 23:40:17.708: E/WindowManager(2087): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-16 23:40:17.708: E/WindowManager(2087): at dalvik.system.NativeStart.main(Native Method)
我使用的当前方式是否正确用于自动重新加载功能?
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String city = "";
Intent intent = getIntent();
pi = intent.getStringExtra("pi");
Log.d("PI", pi);
mainListView = (ListView) findViewById(R.id.mainListView);
cityText = (TextView) findViewById(R.id.cityText);
condDescr = (TextView) findViewById(R.id.condDescr);
imgView = (ImageView) findViewById(R.id.condIcon);
// Create and populate a List of variable names.
String[] weather = new String[] {};
ArrayList<String> weatherData = new ArrayList<String>();
weatherData.addAll(Arrays.asList(weather));
listAdapter = new WeatherAdapter(this, weatherData);
JSONWeatherTask task = new JSONWeatherTask();
task.execute(new String[] { city });
ImageButton picture = (ImageButton) findViewById(R.id.btnPicturePage);
picture.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent startPic = new Intent(MainActivity.this,
PicturePage.class);
startPic.putExtra("date", date);
startPic.putExtra("pi", pi);
startActivity(startPic);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.selectPi1:
Toast.makeText(getApplicationContext(),
"Pi 1 is currently not available", Toast.LENGTH_LONG)
.show();
return true;
case R.id.selectPi2:
Intent i2 = new Intent(this, MainActivity.class);
i2.putExtra("pi", "pi2");
finish();
startActivity(i2);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private class JSONWeatherTask extends AsyncTask<String, Void, Weather> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Weather doInBackground(String... params) {
Weather weather = new Weather();
String data = ((new WeatherHttpClient()).getWeatherData(params[0]));
List<NameValuePair> params2 = new ArrayList<NameValuePair>();
params2.add(new BasicNameValuePair("pi", pi));
try {
httpPost.setEntity(new UrlEncodedFormEntity(params2));
} catch (UnsupportedEncodingException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
ResponseHandler<String> responseHandler = new BasicResponseHandler();
HttpClient httpclient = new DefaultHttpClient();
try {
response = httpclient.execute(httpPost, responseHandler);
Log.d("JSON OUT", response);
if (response == null) {
showAlertDialog(MainActivity.this);
}
} catch (UnknownHostException uhe) {
uhe.printStackTrace();
} catch (ClientProtocolException e4) {
// TODO Auto-generated catch block
e4.printStackTrace();
} catch (IOException e4) {
// TODO Auto-generated catch block
e4.printStackTrace();
}
try {
weather = JSONWeatherParser.getWeather(data);
} catch (JSONException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
try {
json = new JSONObject(response);
} catch (JSONException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
weatherD = json.getJSONArray(TAG_WEATHER);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
JSONObject c = weatherD.getJSONObject(0);
temp = c.getString(TAG_TEMP);
nTemp = temp.replace('*', '°');
pressure = c.getString(TAG_PRESSURE);
humid = c.getString(TAG_HUMIDITY);
ac = c.getString(TAG_AC);
int aq = Integer.parseInt(ac);
if (aq <= 150) {
airq = "Good";
} else if (aq <= 300) {
airq = "Moderate";
} else if (aq <= 500) {
airq = "Unhealthy";
} else if (aq <= 700) {
airq = "Very Unhealthy";
} else {
airq = "HARZADOUS!";
}
dateO = c.getString(TAG_DATE);
SimpleDateFormat formatter = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss", java.util.Locale.getDefault());
Date dateD = null;
try {
dateD = formatter.parse(dateO);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SimpleDateFormat formatter2 = new SimpleDateFormat(
"dd/MM/yyyy hh:mm:ss a", java.util.Locale.getDefault());
date = formatter2.format(dateD);
} catch (JSONException e) {
e.printStackTrace();
}
return weather;
}
@Override
protected void onPostExecute(Weather weather) {
super.onPostExecute(weather);
Drawable day = getResources().getDrawable(R.drawable.clear_day);
Drawable night = getResources().getDrawable(R.drawable.clear_night);
Drawable cloud = getResources().getDrawable(R.drawable.cloudy);
Drawable haze = getResources().getDrawable(R.drawable.haze);
Drawable rain = getResources().getDrawable(R.drawable.rain);
Drawable thunder = getResources().getDrawable(R.drawable.thunder);
String code = weather.currentCondition.getIcon();
if (code.equals("01d") || code.equals("02d")) {
imgView.setImageDrawable(day);
} else if (code.equals("01n") || code.equals("02n")) {
imgView.setImageDrawable(night);
} else if (code.startsWith("03") || code.startsWith("04")) {
imgView.setImageDrawable(cloud);
} else if (code.startsWith("09") || code.startsWith("10")) {
imgView.setImageDrawable(rain);
} else if (code.startsWith("11")) {
imgView.setImageDrawable(thunder);
} else if (code.startsWith("50")) {
imgView.setImageDrawable(haze);
}
cityText.setText("Cyberjaya" + ", " + weather.location.getCountry()
+ " - " + pi);
if (weather.currentCondition.getCondition().equals("Clouds")) {
condDescr.setText("Weather: Cloudy");
weatherS = "Cloudy";
} else {
condDescr.setText("Weather: "
+ weather.currentCondition.getCondition());
weatherS = weather.currentCondition.getCondition();
}
listAdapter.add("Temperature: " + "\t\t\t\t" + nTemp);
listAdapter.add("Humidity: " + "\t\t\t\t\t\t" + humid);
listAdapter.add("Air pressure: " + "\t\t\t\t" + pressure);
listAdapter.add("Air quality: " + "\t\t\t\t\t" + airq + " (" + ac
+ ")");
listAdapter.add("Updated on: " + "\t\t\t\t" + date);
pDialog.dismiss();
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter(listAdapter);
callAsynchronousTask();
}
}
public void callAsynchronousTask() {
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
public void run() {
try {
// Class that extends AsynchTask
JSONWeatherTask backgroundTask = new JSONWeatherTask();
backgroundTask.execute();
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0, 15000); // execute in every 15
// seconds
}
}
答案 0 :(得分:0)
可能是由于在Post Execute方法中解除Dialog的方式 试着这样做:
@Override
protected void onPostExecute(Weather weather) {
super.onPostExecute(weather);
Drawable day = getResources().getDrawable(R.drawable.clear_day);
Drawable night = getResources().getDrawable(R.drawable.clear_night);
Drawable cloud = getResources().getDrawable(R.drawable.cloudy);
Drawable haze = getResources().getDrawable(R.drawable.haze);
Drawable rain = getResources().getDrawable(R.drawable.rain);
Drawable thunder = getResources().getDrawable(R.drawable.thunder);
String code = weather.currentCondition.getIcon();
if (code.equals("01d") || code.equals("02d")) {
imgView.setImageDrawable(day);
} else if (code.equals("01n") || code.equals("02n")) {
imgView.setImageDrawable(night);
} else if (code.startsWith("03") || code.startsWith("04")) {
imgView.setImageDrawable(cloud);
} else if (code.startsWith("09") || code.startsWith("10")) {
imgView.setImageDrawable(rain);
} else if (code.startsWith("11")) {
imgView.setImageDrawable(thunder);
} else if (code.startsWith("50")) {
imgView.setImageDrawable(haze);
}
cityText.setText("Cyberjaya" + ", " + weather.location.getCountry()
+ " - " + pi);
if (weather.currentCondition.getCondition().equals("Clouds")) {
condDescr.setText("Weather: Cloudy");
weatherS = "Cloudy";
} else {
condDescr.setText("Weather: "
+ weather.currentCondition.getCondition());
weatherS = weather.currentCondition.getCondition();
}
listAdapter.add("Temperature: " + "\t\t\t\t" + nTemp);
listAdapter.add("Humidity: " + "\t\t\t\t\t\t" + humid);
listAdapter.add("Air pressure: " + "\t\t\t\t" + pressure);
listAdapter.add("Air quality: " + "\t\t\t\t\t" + airq + " (" + ac
+ ")");
listAdapter.add("Updated on: " + "\t\t\t\t" + date);
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter(listAdapter);
callAsynchronousTask();
pDialog.dismiss();
}
希望它对你有所帮助。
答案 1 :(得分:0)
您应该在对话框中使用dismiss方法。
答案 2 :(得分:0)
在执行任务之前放置finish()
。
public void callAsynchronousTask() {
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
public void run() {
try {
// Class that extends AsyncTask
JSONWeatherTask backgroundTask = new JSONWeatherTask();
finish(); //<------here
backgroundTask.execute();
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0, 15000); // execute in every 15
// seconds