本文主要是介绍AlertDialog is your activity running?,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
代码报错
android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@44ee42b0 is not valid; is your activity running?
错误代码:
AlertDialog.Builder builder = new AlertDialog.Builder(this);//这里写this会出现上面这个错误builder.setTitle("您的账号已在其他设备登录,请重新登录!");builder.setPositiveButton("重新登录", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {startActivity(new Intent(HomeActivity.this,LoginActivity.class));finish();}});builder.setNegativeButton("修改密码", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {new TokenDao(HomeActivity.this).deleteToken();startActivity(new Intent(HomeActivity.this,RetrieveActivity.class));finish();}});AlertDialog dialog = builder.create();if (!isFinishing();){dialog.show();}
正确代码
Activity创建的时候,把当前Activity对象赋值给homeActivity
//全局变量
private HomeActivity homeActivity;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);homeActivity = this;}AlertDialog.Builder builder = new AlertDialog.Builder(homeActivity);builder.setTitle("您的账号已在其他设备登录,请重新登录!");builder.setPositiveButton("重新登录", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {startActivity(new Intent(HomeActivity.this,LoginActivity.class));finish();}});builder.setNegativeButton("修改密码", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {new TokenDao(HomeActivity.this).deleteToken();startActivity(new Intent(HomeActivity.this,RetrieveActivity.class));finish();}});AlertDialog dialog = builder.create();if (!isFinishing()){dialog.show();}
这篇关于AlertDialog is your activity running?的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!