Java android.view.WindowManager$BadTokenException:无法添加Windows--token android.os。BinderProxy@739b5ae无效
我正在从主活动调用另一个活动,以便在后台下载报价。它有时工作正常,但如果用户按下后退按钮并离开应用程序,然后尝试再次打开应用程序,有时应用程序会崩溃,出现以下错误 我已经添加了错误日志以及我从主活动调用的类。我的代码中有什么错误导致应用程序崩溃 我在主要活动中呼叫的班级Java android.view.WindowManager$BadTokenException:无法添加Windows--token android.os。BinderProxy@739b5ae无效,java,android,Java,Android,我正在从主活动调用另一个活动,以便在后台下载报价。它有时工作正常,但如果用户按下后退按钮并离开应用程序,然后尝试再次打开应用程序,有时应用程序会崩溃,出现以下错误 我已经添加了错误日志以及我从主活动调用的类。我的代码中有什么错误导致应用程序崩溃 我在主要活动中呼叫的班级 private class CheckUpdates extends AsyncTask<String, Void, Void> { ProgressDialog mProgressDialog;
private class CheckUpdates extends AsyncTask<String, Void, Void> {
ProgressDialog mProgressDialog;
@Override
protected void onPostExecute(Void result) {
mProgressDialog.dismiss();
Log.d("DESOLF", "dismiss loading dialog");
if (json != null) {
if (jsonResultNull.equals("true")) {
AlertDialog.Builder builder = new AlertDialog.Builder(
context);
builder.setTitle("Check Updates");
builder.setMessage("There are not any updates!");
builder.setNeutralButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
}
});
if(SettingsActivity.tocheck==1){
builder.show();
}
} else if(SettingsActivity.tocheck==1) {
AlertDialog.Builder builder = new AlertDialog.Builder(
context);
builder.setTitle("New Data Available");
String messageTxt = "";
if (authors.length() != 0 && quotes.length() != 0) {
messageTxt = String.valueOf(authors.length())
+ " Categories and "
+ String.valueOf(quotes.length()) + " Quotes";
} else if (authors.length() != 0 && quotes.length() == 0) {
messageTxt = String.valueOf(authors.length())
+ " Categories";
} else if (authors.length() == 0 && quotes.length() != 0) {
messageTxt = String.valueOf(quotes.length())
+ " Status";
}
builder.setMessage("There are new " + messageTxt
+ " Arrived. Download?");
builder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
Log.d("DESOLF", "start updates service");
Intent getUpdates = new Intent(context,
GetUpdatesService.class);
jsonString = json.toString();
//getUpdates.putExtra("json", json.toString());
context.startService(getUpdates);
}
});
builder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
}
});
builder.show();
}
else if(SettingsActivity.tocheck==0)
{
Log.d("DESOLF", "start updates service");
Intent getUpdates = new Intent(context,
GetUpdatesService.class);
jsonString = json.toString();
//getUpdates.putExtra("json", json.toString());
context.startService(getUpdates);
}
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Internet Connection Error");
builder.setMessage("Please connect to an internet connection!");
builder.setNeutralButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
builder.show();
}
}
// ------------------------------------------------------------------------
@Override
protected void onPreExecute() {
Log.d("DESOLF", "show loading dialog");
mProgressDialog = ProgressDialog.show(context, "Please Wait",
"Downloading New Status...");
mProgressDialog.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.getAction() == KeyEvent.ACTION_UP)
mProgressDialog.dismiss();
return false;
}
});
}
全班
package com.example.app;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Button;
import android.widget.Toast;
import com.example.app.R;
public class UpdateClass {
public Context context;
Button zain;
String siteUrl, updatesUrl;
DAO db;
Cursor c;
String auPictureDir;
int lastAuthor, lastQuote;
JSONArray authors = null;
JSONArray quotes = null;
JSONObject json;
static String jsonString;
String jsonResultNull = "";
private ConnectionDetector cd;
Boolean isSDPresent = android.os.Environment.getExternalStorageState()
.equals(android.os.Environment.MEDIA_MOUNTED);
// ==============================================================================
public UpdateClass(Context context) {
this.context = context;
db = new DAO(context);
db.open();
lastAuthor = db.getLastAuthor();
lastQuote = db.getLastQuote();
siteUrl = context.getResources().getString(R.string.siteUrl);
updatesUrl = siteUrl + "site/get_updates/" + String.valueOf(lastAuthor)
+ "/" + String.valueOf(lastQuote);
auPictureDir = siteUrl + "global/uploads/levels/";
}
// ==============================================================================
public void handleUpdates() {
// check first for internet
cd = new ConnectionDetector(context);
if (!cd.isConnectingToInternet()) {
// Internet Connection is not present
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Internet Connection Error");
builder.setMessage("Please connect to an internet connection!");
builder.setNeutralButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
builder.show();
} else {
new CheckUpdates().execute(new String[] { updatesUrl });
}
}
public static String getJsonString(){
return jsonString;
}
private class CheckUpdates extends AsyncTask<String, Void, Void> {
ProgressDialog mProgressDialog;
@Override
protected void onPostExecute(Void result) {
mProgressDialog.dismiss();
Log.d("DESOLF", "dismiss loading dialog");
if (json != null) {
if (jsonResultNull.equals("true")) {
AlertDialog.Builder builder = new AlertDialog.Builder(
context);
builder.setTitle("Check Updates");
builder.setMessage("There are not any updates!");
builder.setNeutralButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
}
});
if(SettingsActivity.tocheck==1){
builder.show();
}
} else if(SettingsActivity.tocheck==1) {
AlertDialog.Builder builder = new AlertDialog.Builder(
context);
builder.setTitle("New Data Available");
String messageTxt = "";
if (authors.length() != 0 && quotes.length() != 0) {
messageTxt = String.valueOf(authors.length())
+ " Categories and "
+ String.valueOf(quotes.length()) + " Quotes";
} else if (authors.length() != 0 && quotes.length() == 0) {
messageTxt = String.valueOf(authors.length())
+ " Categories";
} else if (authors.length() == 0 && quotes.length() != 0) {
messageTxt = String.valueOf(quotes.length())
+ " Status";
}
builder.setMessage("There are new " + messageTxt
+ " Arrived. Download?");
builder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
Log.d("DESOLF", "start updates service");
Intent getUpdates = new Intent(context,
GetUpdatesService.class);
jsonString = json.toString();
//getUpdates.putExtra("json", json.toString());
context.startService(getUpdates);
}
});
builder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
}
});
builder.show();
}
else if(SettingsActivity.tocheck==0)
{
Log.d("DESOLF", "start updates service");
Intent getUpdates = new Intent(context,
GetUpdatesService.class);
jsonString = json.toString();
//getUpdates.putExtra("json", json.toString());
context.startService(getUpdates);
}
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Internet Connection Error");
builder.setMessage("Please connect to an internet connection!");
builder.setNeutralButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
if(!CheckUpdates.isFinishing()){
builder.show();
}
}
}
// ------------------------------------------------------------------------
@Override
protected void onPreExecute() {
Log.d("DESOLF", "show loading dialog");
mProgressDialog = ProgressDialog.show(context, "Please Wait",
"Downloading New Status...");
mProgressDialog.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.getAction() == KeyEvent.ACTION_UP)
mProgressDialog.dismiss();
return false;
}
});
}
// ------------------------------------------------------------------------
@Override
protected Void doInBackground(String... params) {
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
// Log.e("url", params[0]);
Log.d("DESOLF", "url : params[0]");
json = jParser.getJSONFromUrl(params[0]);
Log.d("DESOLF", "json string has been downloaded");
try {
if (json != null) {
authors = json.getJSONArray("authors");
quotes = json.getJSONArray("quotes");
// Log.e("quotes", String.valueOf(quotes.length()));
if (authors.length() == 0 && quotes.length() == 0) {
jsonResultNull = "true";
}
} else {
jsonResultNull = "true";
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
}
package com.example.app;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.app.AlertDialog;
导入android.app.ProgressDialog;
导入android.content.Context;
导入android.content.DialogInterface;
导入android.content.DialogInterface.OnKeyListener;
导入android.content.Intent;
导入android.database.Cursor;
导入android.os.AsyncTask;
导入android.util.Log;
导入android.view.KeyEvent;
导入android.widget.Button;
导入android.widget.Toast;
导入com.example.app.R;
公共类更新类{
公共语境;
巴顿赞;
字符串siteUrl,updatesUrl;
DAO-db;
光标c;
字符串编辑器;
int lastAuthor,lastQuote;
JSONArray authors=null;
JSONArray引号=null;
JSONObject json;
静态字符串jsonString;
字符串jsonResultNull=“”;
专用连接检测器cd;
布尔值isSDPresent=android.os.Environment.getExternalStorageState()
.equals(android.os.Environment.MEDIA_安装);
// ==============================================================================
公共UpdateClass(上下文){
this.context=上下文;
db=新DAO(上下文);
db.open();
lastAuthor=db.getLastAuthor();
lastQuote=db.getLastQuote();
siteUrl=context.getResources().getString(R.string.siteUrl);
updatesUrl=siteUrl+“site/get_updates/”+String.valueOf(lastAuthor)
+“/”+String.valueOf(lastQuote);
auPictureDir=siteUrl+“全局/上传/级别/”;
}
// ==============================================================================
公共无效手册更新(){
//首先检查互联网
cd=新连接检测器(上下文);
如果(!cd.isConnectingToInternet()){
//Internet连接不存在
AlertDialog.Builder=新建AlertDialog.Builder(上下文);
builder.setTitle(“互联网连接错误”);
setMessage(“请连接到internet连接!”);
builder.setNeutralButton(“确定”,
新建DialogInterface.OnClickListener(){
public void onClick(DialogInterface对话框,int-id){
}
});
builder.show();
}否则{
new CheckUpdates().execute(新字符串[]{updatesUrl});
}
}
公共静态字符串getJsonString(){
返回jsonString;
}
私有类检查更新扩展异步任务{
进程对话框;
@凌驾
受保护的void onPostExecute(void结果){
mProgressDialog.disclose();
Log.d(“DESOLF”,“解除加载对话框”);
if(json!=null){
if(jsonResultNull.equals(“true”)){
AlertDialog.Builder=新建AlertDialog.Builder(
上下文);
builder.setTitle(“检查更新”);
setMessage(“没有任何更新!”);
builder.setNeutralButton(“确定”,
新建DialogInterface.OnClickListener(){
公共void onClick(对话框接口对话框,
int id){
}
});
if(设置活动性。tocheck==1){
builder.show();
}
}else if(setingsactivity.tocheck==1){
AlertDialog.Builder=新建AlertDialog.Builder(
上下文);
建造商名称(“新数据可用”);
字符串messageTxt=“”;
if(authors.length()!=0&"es.length()!=0){
messageTxt=String.valueOf(authors.length())
+“类别和”
+String.valueOf(quotes.length())+“quotes”;
}else if(authors.length()!=0&"es.length()=0){
messageTxt=String.valueOf(authors.length())
+“类别”;
}else if(authors.length()=0&"es.length()!=0){
messageTxt=String.valueOf(quotes.length())
+“地位”;
}
setMessage(“有新的”+messageTxt
+“到了。下载吗?”);
builder.setPositiveButton(“是”,
新建DialogInterface.OnClickListener(){
公共void onClick(对话框接口对话框,
int id){
Log.d(“DESOLF”,“启动更新服务”);
Intent getUpdates=新的Intent(上下文,
getUpdateService.class);
jsonString=json.toString();
//getUpdates.putExtra(“json”,json.toString());
startService(getUpdates);
}
});
package com.example.app;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Button;
import android.widget.Toast;
import com.example.app.R;
public class UpdateClass {
public Context context;
Button zain;
String siteUrl, updatesUrl;
DAO db;
Cursor c;
String auPictureDir;
int lastAuthor, lastQuote;
JSONArray authors = null;
JSONArray quotes = null;
JSONObject json;
static String jsonString;
String jsonResultNull = "";
private ConnectionDetector cd;
Boolean isSDPresent = android.os.Environment.getExternalStorageState()
.equals(android.os.Environment.MEDIA_MOUNTED);
// ==============================================================================
public UpdateClass(Context context) {
this.context = context;
db = new DAO(context);
db.open();
lastAuthor = db.getLastAuthor();
lastQuote = db.getLastQuote();
siteUrl = context.getResources().getString(R.string.siteUrl);
updatesUrl = siteUrl + "site/get_updates/" + String.valueOf(lastAuthor)
+ "/" + String.valueOf(lastQuote);
auPictureDir = siteUrl + "global/uploads/levels/";
}
// ==============================================================================
public void handleUpdates() {
// check first for internet
cd = new ConnectionDetector(context);
if (!cd.isConnectingToInternet()) {
// Internet Connection is not present
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Internet Connection Error");
builder.setMessage("Please connect to an internet connection!");
builder.setNeutralButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
builder.show();
} else {
new CheckUpdates().execute(new String[] { updatesUrl });
}
}
public static String getJsonString(){
return jsonString;
}
private class CheckUpdates extends AsyncTask<String, Void, Void> {
ProgressDialog mProgressDialog;
@Override
protected void onPostExecute(Void result) {
mProgressDialog.dismiss();
Log.d("DESOLF", "dismiss loading dialog");
if (json != null) {
if (jsonResultNull.equals("true")) {
AlertDialog.Builder builder = new AlertDialog.Builder(
context);
builder.setTitle("Check Updates");
builder.setMessage("There are not any updates!");
builder.setNeutralButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
}
});
if(SettingsActivity.tocheck==1){
builder.show();
}
} else if(SettingsActivity.tocheck==1) {
AlertDialog.Builder builder = new AlertDialog.Builder(
context);
builder.setTitle("New Data Available");
String messageTxt = "";
if (authors.length() != 0 && quotes.length() != 0) {
messageTxt = String.valueOf(authors.length())
+ " Categories and "
+ String.valueOf(quotes.length()) + " Quotes";
} else if (authors.length() != 0 && quotes.length() == 0) {
messageTxt = String.valueOf(authors.length())
+ " Categories";
} else if (authors.length() == 0 && quotes.length() != 0) {
messageTxt = String.valueOf(quotes.length())
+ " Status";
}
builder.setMessage("There are new " + messageTxt
+ " Arrived. Download?");
builder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
Log.d("DESOLF", "start updates service");
Intent getUpdates = new Intent(context,
GetUpdatesService.class);
jsonString = json.toString();
//getUpdates.putExtra("json", json.toString());
context.startService(getUpdates);
}
});
builder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
}
});
builder.show();
}
else if(SettingsActivity.tocheck==0)
{
Log.d("DESOLF", "start updates service");
Intent getUpdates = new Intent(context,
GetUpdatesService.class);
jsonString = json.toString();
//getUpdates.putExtra("json", json.toString());
context.startService(getUpdates);
}
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Internet Connection Error");
builder.setMessage("Please connect to an internet connection!");
builder.setNeutralButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
if(!CheckUpdates.isFinishing()){
builder.show();
}
}
}
// ------------------------------------------------------------------------
@Override
protected void onPreExecute() {
Log.d("DESOLF", "show loading dialog");
mProgressDialog = ProgressDialog.show(context, "Please Wait",
"Downloading New Status...");
mProgressDialog.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.getAction() == KeyEvent.ACTION_UP)
mProgressDialog.dismiss();
return false;
}
});
}
// ------------------------------------------------------------------------
@Override
protected Void doInBackground(String... params) {
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
// Log.e("url", params[0]);
Log.d("DESOLF", "url : params[0]");
json = jParser.getJSONFromUrl(params[0]);
Log.d("DESOLF", "json string has been downloaded");
try {
if (json != null) {
authors = json.getJSONArray("authors");
quotes = json.getJSONArray("quotes");
// Log.e("quotes", String.valueOf(quotes.length()));
if (authors.length() == 0 && quotes.length() == 0) {
jsonResultNull = "true";
}
} else {
jsonResultNull = "true";
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
}
public UpdateClass(Activity context) {
//this.context also should be Activity, so please make it as a Activity Reference
this.context = context;
db = new DAO(context);
db.open();
lastAuthor = db.getLastAuthor();
lastQuote = db.getLastQuote();
siteUrl = context.getResources().getString(R.string.siteUrl);
updatesUrl = siteUrl + "site/get_updates/" + String.valueOf(lastAuthor)
+ "/" + String.valueOf(lastQuote);
auPictureDir = siteUrl + "global/uploads/levels/";
}
if(!context.isFinishing()){ //here activity means your activity class
builder.show();
}