0

I have an android project, all source code haven't error but sometimes it give me a force close.

here is my code :

timecompletedialog.java

public class TimeCompleteDialog implements OnClickListener {

    private Activity act;
    private LayoutInflater inflater;

    /**UI Components*/
    private Dialog dialog;
    private TextView txt_msg;

    private ImageButton btn_cancal;

    public TimeCompleteDialog(Activity a) {
        this.act=a;

        inflater=LayoutInflater.from(act);
        dialog=new Dialog(a);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }
    /**Attaching the dialog to custom views*/
    private  void buildDialog(){        
        View v=inflater.inflate(R.layout.time_complete_dialog,null,false);
        dialog.setContentView(v);
        dialog.setCancelable(false);
        this.findDialogViews(v);
    }

    public void showDialog(){
        this.buildDialog();
        this.dialog.show();
    }

    /**Find the ids of the custom views components*/
    private  void findDialogViews(View view){
        txt_msg=(TextView)view.findViewById(R.id.txt_time_up);
        btn_cancal=(ImageButton)view.findViewById(R.id.btn_time_cancel);

        btn_cancal.setOnClickListener(this);
        /**Changing the state of the views*/
        this.renderViews();

    }

    private void renderViews() {
        txt_msg.setText("Sorry Time is up.\nTry Harder Next Time");
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_time_cancel:
            this.dialog.dismiss();
            act.finish();
            break;
        default:
            break;
        }
        //Closing the dialog

    }
}

in this code it have no error. but i don't know what's wrong.

and here is my quizactivity

public class QuizActivity extends BaseFragmentActivity implements OnClickListener,AnswerListener,
                                    OnTimeCompleteListener{

    private TextView questionTextView,timer_text,score_text;
    private ImageButton option_1, option_2, option_3, option_4;
    private LinearLayout layout;
    public static int LEVEL=0,timer=0;
    private Handler handler=new Handler();
    private OnTimeCompleteListener timeComplete=(OnTimeCompleteListener)this;
    private Runnable timerThread=new Runnable() {


        @Override
        public void run() {

            if(timer>0){
                //Time is running 
                timer--;
                timer_text.setText("Time : "+timer);
                handler.postDelayed(this, 1000);
            }else{
                timeComplete.onTimeFinish();

            }

        }
    };

    private int QUESTION_COUNTER=1;
    private int SCORE_COUNTER=0;
    public static int QUESTION_LIMIT;
    private CurrentQuestion currentQuestion ;
    UtilityFile utility;

    private static final String TEST_DEVICE_ID = "TES_EMULATOR";
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.quiz_activity_layout);

        AdView adView = (AdView) this.findViewById(R.id.adView5);
        AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
        .addTestDevice(TEST_DEVICE_ID).build();
        adView.loadAd(adRequest);



        layout=(LinearLayout)this.findViewById(R.id.subject_animate1);
        score_text=(TextView)this.findViewById(R.id.score);
        timer_text=(TextView)this.findViewById(R.id.time);
        score_text.setText("Score : "+"0/"+QUESTION_LIMIT);


        questionTextView = (TextView) this.findViewById(R.id.quiz_question);
        option_1 = (ImageButton) this.findViewById(R.id.quiz_level_option_1);
        option_2 = (ImageButton) this.findViewById(R.id.quiz_level_option_2);
        option_3 = (ImageButton) this.findViewById(R.id.quiz_level_option_3);
        option_4 = (ImageButton) this.findViewById(R.id.quiz_level_option_4);


        option_1.setOnClickListener(this);
        option_2.setOnClickListener(this);
        option_3.setOnClickListener(this);
        option_4.setOnClickListener(this);

        /**Utility for formating of the question*/
        utility=new UtilityFile();
        refreshQuestion();
        handler.postDelayed(timerThread, 10);
        Log.i("questionset", ""+QuizQuestionHandler.lev1.size());



    }


    public void refreshQuestion(){
        List<Integer> randomOptionOrdering=new ArrayList<Integer>();        
        currentQuestion = utility
                .setCurrentQuestionSet(QuizQuestionHandler.lev1);

        ImageButton buttons[]={option_1,option_2,option_3,option_4};
        int answerIndex=utility.randInt(0, 3);  
        Log.i("answertag",""+answerIndex);
        //Right Answer
        buttons[answerIndex].setImageDrawable(getResources().getDrawable(
                currentQuestion.getCurrentSet().getAnsImage()));
        buttons[answerIndex].setTag((Object)currentQuestion.getCurrentSet().getId());
        questionTextView.setText(""
                + currentQuestion.getCurrentSet().getQuestion());       
        //Options

        buttons[randomOrder(randomOptionOrdering, answerIndex)].setImageDrawable(getResources().getDrawable(
                currentQuestion.getOption_1().getAnsImage()));
        buttons[randomOrder(randomOptionOrdering, answerIndex)].setImageDrawable(getResources().getDrawable(
                currentQuestion.getOption_2().getAnsImage()));
        buttons[randomOrder(randomOptionOrdering, answerIndex)].setImageDrawable(getResources().getDrawable(
                currentQuestion.getOption_3().getAnsImage()));

    }

    public int randomOrder(List<Integer> rand,int currentAnswerIndex){
        while(true){
            int index = new UtilityFile().randInt(0,3);
            if(index!=currentAnswerIndex){
                if (!isInserted(rand, index)) {
                    rand.add(index);
                    Log.i("return",""+index);
                    return index;
                }
            }
        }   
    }
    public  boolean isInserted(List<Integer> inserted,int currentIndex){
        for(Integer inte:inserted){
            if(inte==currentIndex){
                return true;
            }
        }
        return false;
    }

    @Override
    public void onClick(View v) {
        disableOptionButton();
        AnswerFinder finder=null;
        switch (v.getId()) {        
        case R.id.quiz_level_option_1:
             finder=new AnswerFinder(option_1,currentQuestion , this);
             finder.getAnswer();
            break;
        case R.id.quiz_level_option_2:
             finder=new AnswerFinder(option_2,currentQuestion , this);
             finder.getAnswer();
            break;
        case R.id.quiz_level_option_3:
             finder=new AnswerFinder(option_3,currentQuestion , this);
             finder.getAnswer();
            break;
        case R.id.quiz_level_option_4:
             finder=new AnswerFinder(option_4,currentQuestion , this);
             finder.getAnswer();
            break;      
        default:
            break;
        }       
    }
    private void disableOptionButton(){
    option_1.setClickable(false);
    option_2.setClickable(false);
    option_3.setClickable(false);
    option_4.setClickable(false);   
    }
    private void enableOptionButton(){
        option_1.setClickable(true);
        option_2.setClickable(true);
        option_3.setClickable(true);
        option_4.setClickable(true);
    }
    public void animateNext(){
        Animation anim=AnimationUtils.loadAnimation(getBaseContext(), R.anim.right_to_left);
        layout.startAnimation(anim);
        anim.setDuration(200);
        anim.setAnimationListener(new AnimationListener() {         
            @Override
            public void onAnimationStart(Animation animation) { 
                stopTimer();

            }           
            @Override
            public void onAnimationRepeat(Animation animation) {
            }           
            @Override
            public void onAnimationEnd(Animation animation) {
                /**Handling Animation Delays and Render Other Animations
                 * */
                Animation anim=AnimationUtils.loadAnimation(getBaseContext(), R.anim.left_to_right);
                anim.setDuration(200);
                refreshQuestion();
                layout.startAnimation(anim);
                startTimer();
            }
        });
    }


    public void startTimer(){
        handler.postDelayed(timerThread, 100);
    }
    public void stopTimer(){
        handler.removeCallbacks(timerThread);
    }
    /**For getting the call of right and wrong answer*/
    @Override
    public void onAnswerClick(boolean answer) {     
        /**Check for the question overflow*/
        if(QUESTION_COUNTER<QUESTION_LIMIT){
            if(answer){
                SCORE_COUNTER++;
                AnswerHandler.rightAnswerHandler(score_text,this,SCORE_COUNTER,QUESTION_LIMIT);
            }else{
                //AnswerHandler.wrongAnswerHandler(this);
            }
            animateNext();
            QUESTION_COUNTER++;
        }else{
            /**Incrementing the final score*/           
            SCORE_COUNTER++;
            AnswerHandler.finalAnswerPlayer(this);
            this.gameCompleted();
        }
        enableOptionButton();
    }

    public void gameCompleted(){
        GameCompleteDialog dialog=new GameCompleteDialog(QuizActivity.this,SCORE_COUNTER);
        dialog.buildDialog();
        dialog.showDialog();            
        handler.removeCallbacks(timerThread);
    }
    public void onTimeFinish() {
        TimeCompleteDialog dialog=new TimeCompleteDialog(this);
        dialog.showDialog();
        AnswerHandler.vibrate(this);

    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK))
        {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }


}

here is my logcat:

09-28 02:56:58.303: E/AndroidRuntime(2180): FATAL EXCEPTION: main
09-28 02:56:58.303: E/AndroidRuntime(2180): Process: com.sendquiz.guessanimalsquiz, PID: 2180
09-28 02:56:58.303: E/AndroidRuntime(2180): android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@b2282c30 is not valid; is your activity running?
09-28 02:56:58.303: E/AndroidRuntime(2180):     at android.view.ViewRootImpl.setView(ViewRootImpl.java:532)
09-28 02:56:58.303: E/AndroidRuntime(2180):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
09-28 02:56:58.303: E/AndroidRuntime(2180):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
09-28 02:56:58.303: E/AndroidRuntime(2180):     at android.app.Dialog.show(Dialog.java:286)
09-28 02:56:58.303: E/AndroidRuntime(2180):     at com.sendquiz.javafile.TimeCompleteDialog.showDialog(TimeCompleteDialog.java:46)
09-28 02:56:58.303: E/AndroidRuntime(2180):     at com.sendquiz.guessanimalsquiz.QuizActivity.onTimeFinish(QuizActivity.java:254)
09-28 02:56:58.303: E/AndroidRuntime(2180):     at com.sendquiz.guessanimalsquiz.QuizActivity$1.run(QuizActivity.java:53)
09-28 02:56:58.303: E/AndroidRuntime(2180):     at android.os.Handler.handleCallback(Handler.java:733)
09-28 02:56:58.303: E/AndroidRuntime(2180):     at android.os.Handler.dispatchMessage(Handler.java:95)
09-28 02:56:58.303: E/AndroidRuntime(2180):     at android.os.Looper.loop(Looper.java:136)
09-28 02:56:58.303: E/AndroidRuntime(2180):     at android.app.ActivityThread.main(ActivityThread.java:5001)
09-28 02:56:58.303: E/AndroidRuntime(2180):     at java.lang.reflect.Method.invokeNative(Native Method)
09-28 02:56:58.303: E/AndroidRuntime(2180):     at java.lang.reflect.Method.invoke(Method.java:515)
09-28 02:56:58.303: E/AndroidRuntime(2180):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
09-28 02:56:58.303: E/AndroidRuntime(2180):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
09-28 02:56:58.303: E/AndroidRuntime(2180):     at dalvik.system.NativeStart.main(Native Method)

can you give me where's the error?

3 Answers3

6

android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@b2282c30 is not valid; is your activity running?

This usually happens when you try to show a dialog after you call finish() on the current Activity. In your code this happens twice. In your TimeCompleteDialog and onKeyDown. What could happen is that you call finish() on your Activity, and you forget to clean the Runnable's queue. When timeComplete.onTimeFinish(); is called, in this case, you will experiencing this kind of crash. The first action I'd take, if I were in you, is to clean the handler's queue, when the Activity is paused. The second thing would be to check the Activity's isRunning flag, before showing the popup.

E.g.

 if (!isFinishing()) {
   // show popup
 }
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
1

This problem comes that passed context/activity is not active context/activity.. You should pass Current activity or context

Ucdemir
  • 2,852
  • 2
  • 26
  • 44
1

I was able to reproduce this in my application by forcing the code to show a dialog after I had navigated to another activity (while this activity was in the background).

The fix was adding the following check before showing the dialog:

    if (activity == null) {
        return false;
    } else {
        return !activity.isDestroyed() && !activity.isFinishing();
    }