我试图通过android studio中的排球获取数据json,类名为obs_bgw,我的代码工作正常但是在1分钟后运行应用程序自动刷新应用程序无法工作并且我检查firebase崩溃中的错误是
Exception android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@cc00538 is not valid; is your activity running?
android.view.ViewRootImpl.setView (ViewRootImpl.java:903)
android.view.WindowManagerGlobal.addView (WindowManagerGlobal.java:377)
android.view.WindowManagerImpl.addView (WindowManagerImpl.java:97)
android.app.Dialog.show (Dialog.java:408)
android.app.ProgressDialog.show (ProgressDialog.java:151)
android.app.ProgressDialog.show (ProgressDialog.java:134)
android.app.ProgressDialog.show (ProgressDialog.java:129)
net.myaapp.app.weatherapp.obs_bgw.sendjsonrequest (obs_bgw.java:51)
net.myaapp.app.weatherapp.obs_bgw$3.run (obs_bgw.java:88)
android.os.Handler.handleCallback (Handler.java:751)
android.os.Handler.dispatchMessage (Handler.java:95)
android.os.Looper.loop (Looper.java:154)
android.app.ActivityThread.main (ActivityThread.java:6776)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1496)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1386)
我的代码
package net.myaapp.app.weatherapp;
import android.app.ProgressDialog;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.json.JSONException;
import org.json.JSONObject;
public class obs_bgw extends AppCompatActivity {
RequestQueue rq;
TextView timeDesc, tempDesc, windspeedDesc, windguestDesc, humdityDesc,pressuresDesc;
int ages;
int temp;
int windspeed;
int windguest;
int humdity;
double pressure;
long timeupdate;
String url = "station=I1410&units=metric&v=2.0&format=json";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bgw_obs);
rq = Volley.newRequestQueue(this);
timeDesc = (TextView) findViewById(R.id.timeupdateDesc);
tempDesc = (TextView) findViewById(R.id.tempid);
windspeedDesc = (TextView) findViewById(R.id.windid);
windguestDesc = (TextView) findViewById(R.id.windgustid);
humdityDesc = (TextView) findViewById(R.id.humdid);
pressuresDesc = (TextView) findViewById(R.id.pressuresDesc);
sendjsonrequest();
}
public void sendjsonrequest() {
final ProgressDialog dialog = ProgressDialog.show(this,null, "Please Wait");
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
dialog.dismiss();
try {
JSONObject stationsJO = response.getJSONObject("stations");
JSONObject I1410JO = stationsJO.getJSONObject("I1410");
temp = I1410JO.getInt("temperature");
windspeed = I1410JO.getInt("wind_speed");
windguest = I1410JO.getInt("wind_gust_speed");
humdity = I1410JO.getInt("humidity");
timeupdate = I1410JO.getLong("updated") * 1000L;
pressure = I1410JO.getDouble("pressure");
tempDesc.setText(Integer.toString(temp)+" C");
windspeedDesc.setText(Integer.toString(windspeed)+" كم ");
windguestDesc.setText(Integer.toString(windguest)+" كم ");
humdityDesc.setText(Integer.toString(humdity)+" % ");
pressuresDesc.setText(Integer.toString((int) pressure)+" in ");
timeDesc.setText(getDate(timeupdate));
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
dialog.dismiss();
}
});
rq.add(jsonObjectRequest);
final Handler handler = new Handler();
final Runnable runnable = new Runnable() {
@Override
public void run() {
sendjsonrequest();
handler.postDelayed(this, 60000);//60 second delay
}
};
handler.postDelayed(runnable, 60000);
}
private String getDate(long timeStamp) {
try {
DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
Date netDate = (new Date(timeStamp));
return sdf.format(netDate);
} catch (Exception ex) {
return "xx";
}
}
}
问题出在Progress Dialog,第51行final ProgressDialog dialog = ProgressDialog.show(this,null, "Please Wait");
,当他刷新日期停止工作时
答案 0 :(得分:0)
请检查以下条件
forPause / onDestroy中的ProgressDialog
if (progressDialog != null) progressDialog.dismiss();
用于删除onPause / onDestroy中的处理程序回调
if (handler != null && runnable != null) {
handler.removeCallbacks(runnable);
}
请在您的情况下这样写,否则处理程序将采取多个实例:
Runnable runnable;
final Handler handler = new Handler();
if (runnable != null) {
handler.removeCallbacks(runnable);
}
runnable = new Runnable() {
@Override
public void run() {
sendjsonrequest();
handler.postDelayed(this, 60000);//60 second delay
}
};
handler.postDelayed(runnable, 60000);
答案 1 :(得分:-1)
异常android.view.WindowManager $ BadTokenException:无法添加 window - 令牌android.os.BinderProxy@cc00538无效;是你的 活动在运行?
按照以下步骤
final ProgressDialog dialog = ProgressDialog.show(obs_bgw.this,null, "Please Wait");
ProgressDialog
应为 GLOBAL 变量。
添加 onPause
方法。
@Override
public void onPause() {
super.onPause();
if (dialog.isShowing())
{
dialog.dismiss();
}
答案 2 :(得分:-1)
在外部创建进度对话框实例,并在调用Web服务时显示它。任务完成后,隐藏进度对话框。
在顶部
中声明对话框private ProgressDialog progressDialog;
在onCreate中创建它的实例。
progressDialog = new ProgressDialog(this);
progressDialog.setCancelable(false);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.setTitle("Loading...");
在webservice调用方法中显示对话框。
public void sendjsonrequest() {
progressDialog.show();
}
在onResponse和onErrorResponse中,关闭对话框。
dialog.dismiss();