2013-05-21 33 views
2

我寫一個教育數學遊戲,你可以在同一時間選擇許多操作,現在即時試圖產生5個問題,但它運行所有五個問題太快,我不能回答他們,除了最後的問題,因爲它卡在那裏。所以我想創建一個線程,在創建第一個問題後等待(),然後等待它解決並正確回答,然後繼續下一個問題,等等......現在我從來沒有用過wait和notify,所以在那裏我應該爲它們分配 在這裏我得到了什麼,到目前爲止,但它給了我一個例外:等待()和通知()在一個線程android

while (counter < 5) { 
    Thread pause = new Thread() { 

     @Override 
     public void run() { 
      try { 
       this.wait(); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } finally{ 

       // ops array is for knowing what operation he chose, 
       // 1 for plus and 2 for minus 
       //generate random number within the range of the operations length. 
       int gen = Integer.valueOf((int) ((Math.random() * (ops.length)))); 
       Log.d("Testgen", String.valueOf(gen)); 

       //random operation is generated 
       int TheChosenOperation = ops[gen]; 
       Log.d("Test The chosen", String.valueOf(TheChosenOperation)); 

       switch (TheChosenOperation) { 
       case 1: // if it is plus, assign the generated numbers to i and j from plus.java 
        Toast t = Toast.makeText(SecondActivity.this, "Plus", 5000); 
        t.show(); 
        tv.setText("+"); 
        int i = plus.getBorder(); 
        int j = plus.getBorder2(); 
        tv2.setText(String.valueOf(i)); 
        tv3.setText(String.valueOf(j)); 
        answer = plus.getAnswer(); 
        break; 
       case 2: //if it is minus, assign the generated numbers to i and j from minus.java 
        Toast t2 = Toast.makeText(SecondActivity.this, "minus", 5000); 
        t2.show(); 
        tv.setText("-"); 
        int i2 = minus.getBorder(); 
        int j2 = minus.getBorder2(); 
        tv2.setText(String.valueOf(i2)); 
        tv3.setText(String.valueOf(j2)); 
        answer = minus.getAnswer(); 
        break; 
       } 
       b.setOnClickListener(new OnClickListener() { 

        public void onClick(View arg0) { 
         // TODO Auto-generated method stub 
         if (answer > 0 && answer == Integer.parseInt(ed.getText().toString())) { 
          Toast t = Toast.makeText(SecondActivity.this, "true", 
            5000); 
          t.show(); 
          this.notify(); // if the answer is true then notify() 
         } else { 
          Toast t = Toast.makeText(SecondActivity.this, "false", 
            5000); 
          t.show(); 
         } 

        } 
       }); // end of clicklistner 
       counter++; // counting for 5 questions 


      }//finally 
     }//run 

    }; // end of thread 
    pause.start(); 
} //end of while loop 

我還在新的Android和線程所以請耐心等待我。 提前致謝,並對我可怕的英語感到抱歉。

+0

我沒有看過細節,但一個顯而易見的問題是'this.wait'中的'this'與'this.notify'中的'this'不是同一個對象(它是前者中的線程,後者中的OnClickListener)。 – assylias

+0

同樣你在創建你的onclicklistener之前等待,所以你的線程會卡在這個等待線上,永遠不會有調用通知的機會。你只能在同步塊中調用wait和notify ... – assylias

回答

1

在這種情況下使用Thread並不真實。

  • 有一個主線程或UI線程負責呈現和管理UI狀態。如果您想對UI元素進行任何更改(如TextViewButton等),則必須在此主題中完成此操作。如果你在這個線程中保持長時間的任務,或者讓這個線程等待,你的應用程序將不會響應,Android操作系統將顯示一個ANR對話框。 (在Keeping Your App Responsive中詳細瞭解它)
  • Thread s通常用於有併發任務要執行的情況。在這種情況下,任務並不是真正的併發,而是順序的。

您應該考慮事件驅動的方法,其中應用程序根據事件做出決定(如問題回答,可用於檢查已回答多少個問題以決定是否需要生成更多問題)。

+0

好的,如果線程不是我的問題的答案,那麼你建議如何讓我的程序等待,直到用戶回答問題,然後轉到第二個問題? ! – Coderji

+0

我已經在答案中提供了一個指示 - 去事件驅動。你不會等待任何事情,當有事情發生時你會採取行動。用戶界面等待輸入並在事件發生時調用操作處理程序。 (搜索測驗應用程序,你可以找到許多這樣的應用程序的源代碼 - 向他們學習) – Rajesh