安卓:ProgressDialog.show()窗口管理器,令牌,无效 [英] Android: ProgressDialog.show() WindowManager token, not valid
问题描述
public class MainActivity extends Activity {
private static String State = "STOP";
final String address = "http://XXXX.co.uk:9994";
private ImageView Player;
private ProgressDialog progress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
// requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_main);
Button Schedule;
Button Presenters;
Button Image;
Player = (ImageView) findViewById(R.id.imgStart);
if(State != "STOP"){
Player.setImageResource(R.drawable.stop);
}
else{
Player.setImageResource(R.drawable.start);
}
Schedule = (Button) findViewById(R.id.btnSchedule);
Presenters = (Button) findViewById(R.id.btnPresenters);
Image = (Button) findViewById(R.id.imgbutton);
LocalBroadcastManager.getInstance(this).registerReceiver(LoadingReceiver, new IntentFilter("loading"));
LocalBroadcastManager.getInstance(this).registerReceiver(CompletionReceiver, new IntentFilter("done"));
LocalBroadcastManager.getInstance(this).registerReceiver(ErrorReceiver, new IntentFilter("error"));
Player.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(State != "PLAY"){
Intent intent = new Intent(MainActivity.this,
RadioPlayer.class);
intent.putExtra(RadioPlayer.START_PLAY, true);
startService(intent);
Player.setImageResource(R.drawable.stop);
State = "PLAY";
}
else{
Intent intent = new Intent(MainActivity.this,
RadioPlayer.class);
stopService(intent);
Player.setImageResource(R.drawable.start);
State = "STOP";
}
}
});
Schedule.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent scheduleListing = new Intent(MainActivity.this,
Schedule.class);
startActivity(scheduleListing);
}
});
Presenters.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent PresenterList = new Intent(MainActivity.this,
Presenters.class);
startActivity(PresenterList);
}
});
Image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent ImgGet = new Intent(MainActivity.this,
OnAir.class);
startActivity(ImgGet);
}
});
}// End On Create
BroadcastReceiver LoadingReceiver = new BroadcastReceiver(){
@SuppressLint("NewApi")
@Override
public void onReceive(Context arg0, Intent arg1) {
progress = new ProgressDialog(MainActivity.this);
progress.setTitle("Loading");
progress.setMessage("Loading RadioPlymouth...");
progress.show(); //error here
Player.setImageResource(R.drawable.stop);
}
};
BroadcastReceiver CompletionReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
progress.dismiss();
Player.setImageResource(R.drawable.stop);
}
};
BroadcastReceiver ErrorReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
progress.dismiss();
State = "STOP";
Player.setImageResource(R.drawable.start);
}
};
}
结果
06-28 14:33:14.988: E/AndroidRuntime(18960): android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@41888998 is not valid; is your activity running?
06-28 14:33:14.988: E/AndroidRuntime(18960): at android.view.ViewRootImpl.setView(ViewRootImpl.java:564)
06-28 14:33:14.988: E/AndroidRuntime(18960): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:269)
06-28 14:33:14.988: E/AndroidRuntime(18960): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
06-28 14:33:14.988: E/AndroidRuntime(18960): at android.app.Dialog.show(Dialog.java:289)
06-28 14:33:14.988: E/AndroidRuntime(18960): at com.radioplymouth.RadioPlymouth.MainActivity$1.onReceive(MainActivity.java:144)
当我改变activitys(例)发生错误,从MainActivity - >转到计划页 - >回MainActivity。有人previously建议,我需要处理的onPause()和/或onResume(窗口令牌),但我尝试待办事项所以一直没sucsessful。在不改变activitys中,code运行正常。如果任何人能发现我缺少什么,那将是巨大的:)
The error occurs when i change activitys (Example), From MainActivity -> goto Schedule page -> return to MainActivity. someone has previously suggested that i need to handle the window token in onPause() and/or onResume() however my attempts todo so have not been sucsessful. without changing activitys, the code runs fine. if anyone can spot what i am missing, that'll be great :)
推荐答案
挠头,我终于找到了解决办法。
Well after a lot of searching, debugging & head scratching I finally found a solution.
if(!isFinishing()){
progress.show();
Player.setImageResource(R.drawable.stop);
}
我用其他
与敬酒时看到的情况是真实的,它好像在的onReceive()
这code被封装在被调用多次,直到条件是不正确的。我不能完全肯定这意味着什么,或者为什么会发生是,但是,它的工作原理。
I used the else
with a toast to see when the condition is true, and it seems as if the onReceive()
that this code was encapsulated in is being called multiple times until the condition is not true. I'm not entirely sure what that means, or why is happens but, it works.
这篇关于安卓:ProgressDialog.show()窗口管理器,令牌,无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!