2015-09-28 59 views
-1

我有一個android項目,所有的源代碼沒有錯誤,但有時它給我一個力量關閉。無法添加窗口 - 令牌是您的活動運行

這裏是我的代碼:

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 

    } 
} 

在這段代碼是沒有錯誤。但我不知道什麼是錯的。

,這裏是我的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); 
    } 


} 

這裏是我的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 [email protected] 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) 

你可以給我哪來的錯誤?

+0

您應該發佈您的代碼。 –

+0

在showDialog(TimeCompleteDialog.java:46) – Blackbelt

+0

更新的問題先生@ThomasR。 –

回答

2

android.view.WindowManager $ BadTokenException:無法添加窗口 - 令牌[email protected]無效;您的活動 正在運行?

這通常發生在您調用當前Activity的finish()後顯示對話框時。在你的代碼中,這會發生兩次。在你的TimeCompleteDialogonKeyDown。可能發生的情況是,您在Activity上調用finish(),並且忘記清除Runnable的隊列。當調用timeComplete.onTimeFinish();時,在這種情況下,您將遇到這種崩潰。如果我在你身上,我會採取的第一個行動是在活動暫停時清理處理程序的隊列。第二件事是在顯示彈出窗口之前檢查活動的isRunning標誌。

E.g.

if (!isFinishing()) { 
    // show popup 
} 
+0

這樣做,只是避免了例外。但是,dialog.show不會執行,唯一需要的是:/ – NarendraJi

+0

如果要用來顯示它的活動的上下文無效,您將如何顯示彈出窗口? – Blackbelt

相關問題