Android: ProgressDialog.show() WindowManager token, not valid

青春壹個敷衍的年華 提交于 2019-12-24 08:13:35

问题


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)

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 :)


回答1:


Well after a lot of searching, debugging & head scratching I finally found a solution.

if(!isFinishing()){
                progress.show();            
                Player.setImageResource(R.drawable.stop);
            }

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.



来源:https://stackoverflow.com/questions/24467391/android-progressdialog-show-windowmanager-token-not-valid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!