每次用戶單擊一個位圖時,我都想要一個分數合計爲分數變量。這個分數將被用於另一個班級。但到目前爲止,我不能得到它的工作,運行時我沒有得到任何錯誤,但它總是顯示0(這是文本視圖中的默認字符串)。在另一個腳本(Java,Android Studio)中設置一個int
這是一個計數的分數,而在另一個腳本應該「設置」,它的方法:
@Override public void run(){
Point size = new Point();
Display display = getWindowManager().getDefaultDisplay();
display.getSize(size);
Random wR = new Random();
Random hR = new Random();
int width = size.x;
int height = size.y;
int randomW = wR.nextInt(width-50)+50;
int randomH = hR.nextInt(height-50)+50;
CheckScore scoreClass = new CheckScore();
while (running){
if(!surface.getSurface().isValid())
continue;
Canvas canvas = surface.lockCanvas();
canvas.drawColor(Color.rgb(85, 107, 47));
canvas.drawBitmap(incect[frame], randomW, randomH, null);
if ((touch.x >= randomW && touch.x <(randomW + xBitmap) && (touch.y >= randomH && touch.y < (randomH+yBitmap)) && move == true)){
randomW = wR.nextInt(width-50)+50;
randomH = hR.nextInt(height-50)+50;
if (randomW > width - 200){
randomW = width - 200;
}
if (randomH > height - 200){
randomH = height - 200;
}
canvas.drawBitmap(incect[frame], randomW, randomH, null);
hasMoved = true;
scoreClass.score = score;
score = score +1;
}
這是一個應該收到的得分並顯示它的腳本:
package com.example.bena.23;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class CheckScore extends AppCompatActivity {
int score;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.score);
getScore();
}
public void getScore(){
TextView text = (TextView)findViewById(R.id.thisScore);
text.setText(Integer.toString(score));
}
}
由於腳本大小,我只寫了方法(run())。如果有人能夠幫助我解決這個問題,我將非常感激。
可能的重複[如何在Android上的活動之間傳遞數據?](http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-on-android) –
切勿在Android中使用'新的Activity()'。使用'意圖' –