2013-05-19 86 views
0

好吧,我只是爲了有趣的應用程序,在屏幕上彈出一個隨機數。如果您點擊與隨機數字相同的按鈕,您將得到一個點,如果您點擊不正確的數字按鈕,則會失去一個點。接球是你只有10秒得分足夠的分數(我沒有得到的部分,我檢查用戶是否得分足夠分)。無論如何,我有兩個應用程序的主要問題。計時器不會在沒有應用程序崩潰的情況下顯示到屏幕上(我嘗試使用try/catch來阻止它崩潰,以便我可以處理其他事情),並且當您點擊正確的按鈕時,您的分數不會增加。但是,如果您點擊不正確的按鈕,分數將下降。我覺得這很奇怪。Android的TextViews和按鈕的麻煩

謝謝你的幫助!

這裏是我的活動課:

package ab.game.crazynumbers; 

import java.util.Random; 
import java.util.Timer; 
import java.util.TimerTask; 

import android.app.Activity; 
import android.os.Bundle; 
import android.os.Handler; 
import android.view.MotionEvent; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class Level1Activity extends Activity { 

Random r; 
Timer t; 
int random; 
int time; 
boolean running = false; 
TextView showingnumber; 
TextView score; 
TextView timer; 
Handler handler; 
Button b1; 
Button b2; 
Button b3; 
Button b4; 
Button b5; 
Button b6; 
Button b7; 
Button b8; 
Button b9; 
Button b0; 
int playerscore; 
TextView finalscore; 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.level_1); 

    playerscore = 0; 

    showingnumber = (TextView) findViewById(R.id.game_number); 
    score = (TextView) findViewById(R.id.game_score); 
    timer = (TextView) findViewById(R.id.game_timer2); 
    finalscore = (TextView) findViewById(R.id.finalscore); 

    running = true; 
    time = 10; 

    b0 = (Button) findViewById(R.id.b1_0); 
    b1 = (Button) findViewById(R.id.b1_1); 
    b2 = (Button) findViewById(R.id.b1_2); 
    b3 = (Button) findViewById(R.id.b1_3); 
    b4 = (Button) findViewById(R.id.b1_4); 
    b5 = (Button) findViewById(R.id.b1_5); 
    b6 = (Button) findViewById(R.id.b1_6); 
    b7 = (Button) findViewById(R.id.b1_7); 
    b8 = (Button) findViewById(R.id.b1_8); 
    b9 = (Button) findViewById(R.id.b1_9); 

    randomnumber(); 

    b1.setOnTouchListener(new View.OnTouchListener() { 

     public boolean onTouch(View v, MotionEvent e) { 

      if (e.getAction() == android.view.MotionEvent.ACTION_UP) { 

       if (1 == random) { 
        playerscore++; 
        runOnUiThread(new Runnable() { 
         public void run() { 
          score.setText("Your Score Is " + playerscore); 
         } 
        }); 
        randomnumber(); 
       } 

       if (1 != random) { 
        playerscore--; 
        runOnUiThread(new Runnable() { 
         public void run() { 
          score.setText("Your Score Is " + playerscore); 
         } 
        }); 
        randomnumber(); 
       } 
      } 

      return false; 

     } 
    }); 

    b2.setOnTouchListener(new View.OnTouchListener() { 

     public boolean onTouch(View v, MotionEvent e) { 

      if (e.getAction() == android.view.MotionEvent.ACTION_UP) { 

       if (2 == random) { 
        playerscore++; 
        runOnUiThread(new Runnable() { 
         public void run() { 
          score.setText("Your Score Is " + playerscore); 
         } 
        }); 
        randomnumber(); 
       } 

       if (2 != random) { 
        playerscore--; 
        runOnUiThread(new Runnable() { 
         public void run() { 
          score.setText("Your Score Is " + playerscore); 
         } 
        }); 
        randomnumber(); 
       } 
      } 

      return false; 
     } 
    }); 

    b3.setOnTouchListener(new View.OnTouchListener() { 

     public boolean onTouch(View v, MotionEvent e) { 

      if (e.getAction() == android.view.MotionEvent.ACTION_UP) { 

       if (3 == random) { 
        playerscore++; 
        runOnUiThread(new Runnable() { 
         public void run() { 
          score.setText("Your Score Is " + playerscore); 
         } 
        }); 
        randomnumber(); 
       } 

       if (3 != random) { 
        playerscore--; 
        runOnUiThread(new Runnable() { 
         public void run() { 
          score.setText("Your Score Is " + playerscore); 
         } 
        }); 
        randomnumber(); 
       } 
      } 

      return false; 
     } 
    }); 

    b4.setOnTouchListener(new View.OnTouchListener() { 

     public boolean onTouch(View v, MotionEvent e) { 

      if (e.getAction() == android.view.MotionEvent.ACTION_UP) { 

       if (4 == random) { 
        playerscore++; 
        runOnUiThread(new Runnable() { 
         public void run() { 
          score.setText("Your Score Is " + playerscore); 
         } 
        }); 
        randomnumber(); 
       } 

       if (4 != random) { 
        playerscore--; 
        runOnUiThread(new Runnable() { 
         public void run() { 
          score.setText("Your Score Is " + playerscore); 
         } 
        }); 
        randomnumber(); 
       } 
      } 

      return false; 
     } 
    }); 

    b5.setOnTouchListener(new View.OnTouchListener() { 

     public boolean onTouch(View v, MotionEvent e) { 

      if (e.getAction() == android.view.MotionEvent.ACTION_UP) { 

       if (5 == random) { 
        playerscore++; 
        runOnUiThread(new Runnable() { 
         public void run() { 
          score.setText("Your Score Is " + playerscore); 
         } 
        }); 
        randomnumber(); 
       } 

       if (5 != random) { 
        playerscore--; 
        runOnUiThread(new Runnable() { 
         public void run() { 
          score.setText("Your Score Is " + playerscore); 
         } 
        }); 
        randomnumber(); 
       } 
      } 

      return false; 
     } 
    }); 

    b6.setOnTouchListener(new View.OnTouchListener() { 

     public boolean onTouch(View v, MotionEvent e) { 

      if (e.getAction() == android.view.MotionEvent.ACTION_UP) { 

       if (6 == random) { 
        playerscore++; 
        runOnUiThread(new Runnable() { 
         public void run() { 
          score.setText("Your Score Is " + playerscore); 
         } 
        }); 
        randomnumber(); 
       } 

       if (6 != random) { 
        playerscore--; 
        runOnUiThread(new Runnable() { 
         public void run() { 
          score.setText("Your Score Is " + playerscore); 
         } 
        }); 
        randomnumber(); 
       } 
      } 

      return false; 
     } 
    }); 

    b7.setOnTouchListener(new View.OnTouchListener() { 

     public boolean onTouch(View v, MotionEvent e) { 

      if (e.getAction() == android.view.MotionEvent.ACTION_UP) { 

       if (7 == random) { 
        playerscore++; 
        runOnUiThread(new Runnable() { 
         public void run() { 
          score.setText("Your Score Is " + playerscore); 
         } 
        }); 
        randomnumber(); 
       } 

       if (7 != random) { 
        playerscore--; 
        runOnUiThread(new Runnable() { 
         public void run() { 
          score.setText("Your Score Is " + playerscore); 
         } 
        }); 
        randomnumber(); 
       } 
      } 

      return false; 
     } 
    }); 

    b8.setOnTouchListener(new View.OnTouchListener() { 

     public boolean onTouch(View v, MotionEvent e) { 

      if (e.getAction() == android.view.MotionEvent.ACTION_UP) { 

       if (8 == random) { 
        playerscore++; 
        runOnUiThread(new Runnable() { 
         public void run() { 
          score.setText("Your Score Is " + playerscore); 
         } 
        }); 
        randomnumber(); 
       } 

       if (8 != random) { 
        playerscore--; 
        runOnUiThread(new Runnable() { 
         public void run() { 
          score.setText("Your Score Is " + playerscore); 
         } 
        }); 
        randomnumber(); 
       } 
      } 

      return false; 
     } 
    }); 

    b9.setOnTouchListener(new View.OnTouchListener() { 

     public boolean onTouch(View v, MotionEvent e) { 

      if (e.getAction() == android.view.MotionEvent.ACTION_UP) { 

       if (9 == random) { 
        playerscore++; 
        runOnUiThread(new Runnable() { 
         public void run() { 
          score.setText("Your Score Is " + playerscore); 
         } 
        }); 
        randomnumber(); 
       } 

       if (9 != random) { 
        playerscore--; 
        runOnUiThread(new Runnable() { 
         public void run() { 
          score.setText("Your Score Is " + playerscore); 
         } 
        }); 
        randomnumber(); 
       } 
      } 

      return false; 
     } 
    }); 

    b0.setOnTouchListener(new View.OnTouchListener() { 

     public boolean onTouch(View v, MotionEvent e) { 

      if (e.getAction() == android.view.MotionEvent.ACTION_UP) { 

       if (0 == random) { 
        playerscore++; 
        runOnUiThread(new Runnable() { 
         public void run() { 
          score.setText("Your Score Is " + playerscore); 
         } 
        }); 
        randomnumber(); 
       } 

       if (0 != random) { 
        playerscore--; 
        runOnUiThread(new Runnable() { 
         public void run() { 
          score.setText("Your Score Is " + playerscore); 
         } 
        }); 
        randomnumber(); 
       } 
      } 

      return false; 
     } 
    }); 

    startTimer(); 

} 

public void startTimer() { 

    Timer t = new Timer("Game Timer"); 

    handler = new Handler(); 

    t.scheduleAtFixedRate(new TimerTask() { 

     @Override 
     public void run() { 

      if (time > 0) { 

       time -= 1; 

       runOnUiThread(new Runnable() { 
        public void run() { 
         try { 
          timer.setText("Seconds Remaining: "+ time); 
         } catch (Exception e) { 
         } 
        } 
       }); 

      } 

      if (time <= 0) { 
       this.cancel(); 
        finish(); 
      } 

     } 

    }, 

    // time before start of timer 
      0, 

      // time before it runs again 
      1000); 

} 

public void randomnumber() { 

    r = new Random(); 
    random = r.nextInt(10); 

    runOnUiThread(new Runnable() { 
     public void run() { 
      showingnumber.setText(String.valueOf(random)); 
     } 
    }); 

} 

} 

這裏是我的XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/game_timer" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:text="Seconds Remaining: 0.0" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<TextView 
android:id="@+id/game_score" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_alignParentBottom="true" 
android:layout_alignParentLeft="true" 
android:gravity="center" 
android:text="Your Score Is 0" 
android:textAppearance="?android:attr/textAppearanceLarge" /> 

<Button 
    android:id="@+id/b1.4" 
    android:layout_width="80dp" 
    android:layout_height="50dp" 
    android:layout_alignLeft="@+id/b1.1" 
    android:layout_alignTop="@+id/b1.1" 
    android:layout_marginTop="73dp" 
    android:text="4" /> 

<TextView 
    android:id="@+id/game_number" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/game_timer" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="89dp" 
    android:gravity="center" 
    android:text="0" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<Button 
    android:id="@+id/b1.1" 
    android:layout_width="80dp" 
    android:layout_height="50dp" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/game_number" 
    android:layout_marginLeft="14dp" 
    android:layout_marginTop="130dp" 
    android:text="1" /> 

<Button 
    android:id="@+id/b1.7" 
    android:layout_width="80dp" 
    android:layout_height="50dp" 
    android:layout_above="@+id/game_score" 
    android:layout_alignLeft="@+id/b1.4" 
    android:layout_marginBottom="15dp" 
    android:text="7" /> 

<Button 
    android:id="@+id/b1.2" 
    android:layout_width="80dp" 
    android:layout_height="50dp" 
    android:layout_alignBaseline="@+id/b1.1" 
    android:layout_alignBottom="@+id/b1.1" 
    android:layout_centerHorizontal="true" 
    android:text="2" /> 

<Button 
    android:id="@+id/b1.5" 
    android:layout_width="80dp" 
    android:layout_height="50dp" 
    android:layout_alignBaseline="@+id/b1.4" 
    android:layout_alignBottom="@+id/b1.4" 
    android:layout_centerHorizontal="true" 
    android:text="5" /> 

<Button 
    android:id="@+id/b1.8" 
    android:layout_width="80dp" 
    android:layout_height="50dp" 
    android:layout_alignBaseline="@+id/b1.7" 
    android:layout_alignBottom="@+id/b1.7" 
    android:layout_centerHorizontal="true" 
    android:text="8" /> 

<Button 
    android:id="@+id/b1.0" 
    android:layout_width="80dp" 
    android:layout_height="50dp" 
    android:layout_above="@+id/b1.2" 
    android:layout_alignLeft="@+id/b1.2" 
    android:layout_marginBottom="18dp" 
    android:text="0" /> 

<Button 
    android:id="@+id/b1.6" 
    android:layout_width="80dp" 
    android:layout_height="50dp" 
    android:layout_alignBaseline="@+id/b1.4" 
    android:layout_alignBottom="@+id/b1.4" 
    android:layout_alignParentRight="true" 
    android:layout_marginRight="20dp" 
    android:text="6" /> 

<Button 
    android:id="@+id/b1.9" 
    android:layout_width="80dp" 
    android:layout_height="50dp" 
    android:layout_alignBaseline="@+id/b1.7" 
    android:layout_alignBottom="@+id/b1.7" 
    android:layout_alignLeft="@+id/b1.6" 
    android:text="9" /> 

<Button 
    android:id="@+id/b1.3" 
    android:layout_width="80dp" 
    android:layout_height="50dp" 
    android:layout_alignBaseline="@+id/b1.1" 
    android:layout_alignBottom="@+id/b1.1" 
    android:layout_alignLeft="@+id/b1.6" 
    android:text="3" /> 

</RelativeLayout> 

回答

0

您的佈局說game_timer和你的代碼說game_timer2

檢查一下。 您的線路有:

timer = (TextView) findViewById(R.id.game_timer2); 
finalscore = (TextView) findViewById(R.id.finalscore); 

game_timer2和finalscore不要存在於您的佈局。

您應該使用Handler來編輯您的GUI。例如。 在你的類添加此

static final int SCORE_UP = 1;  static final int SCORE_DOWN = 2; 

    final Handler mhandler = new Handler() { 
        int playerscore = 0; 
      @Override 
      public void handleMessage(Message msg) { 
       switch(msg.what){ 
       case SCORE_UP: 
       { 
        playerscore++; 
       } 
        break; 
       case SCORE_DOWN: 
       { 
        playerscore--; 
       } 
        break; 
        default: 

       } 
       score.setText("Your Score Is " + playerscore); 
      } 
    }; 

而且你在哪裏做正面採用

runOnUiThread(new Runnable() { 
    public void run() { 
    mhandler.sendMessage(Message.obtain(mhandler, SCORE_UP)); 
    //score.setText("Your Score Is " + playerscore); 
    } 
}); 
+0

這解決了這個計時器的問題,但改變比分仍然無法正常工作。你能幫我解決這個問題嗎? – user2154477

+0

您正嘗試從線程更新UI。您可以在代碼上創建處理程序,但無需管理消息的內容,也不會向其發佈消息。 – soynerdito

+0

在這個例子中,我沒有運行它,所以不知道它的行爲。我想你可能想要檢查一個「串行執行任務」,在那裏你可以觸發動作並按順序執行。這可能會讓你的遊戲變得更加強大,因爲在贏得勝利之前你不會失敗。或相反亦然。 – soynerdito