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)
当我更改活动时出现错误(示例),来自MainActivity - >转到计划页面 - >返回MainActivity。有人以前建议我需要在onPause()和/或onResume()中处理窗口令牌,但是我的尝试todo所以没有成功。在不更改活动的情况下,代码运行良好。如果有人能发现我所缺少的东西,那就太棒了:)。
答案 0 :(得分:6)
经过大量的搜索,调试和解决之后头部刮伤我终于找到了解决方案。
if(!isFinishing()){
progress.show();
Player.setImageResource(R.drawable.stop);
}
我使用带有toast的else
来查看条件是否为真,并且看起来这个代码被封装的onReceive()
被多次调用,直到条件不为真。我不完全确定这意味着什么,或者为什么会发生,但是,它有效。