无法在AsyncTask中访问时间,变量point
不再发生变化
这是我的代码:
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class GamePlayActivity extends Activity {
ArrayList<Player> arrPlayer = new ArrayList<Player>();
TextView tvLevel, tvPoint;
ImageView ivGuessedImage;
Button btn1stChoice, btn2ndChoice, btn3rdChoice, btn4thChoice;
int level = 1;
int point = 0;
Random rand = new Random(System.currentTimeMillis());
ProgressBar customProgress;
TextView progressDisplay;
int myProgress;
MediaPlayer mp1, mp2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_gameplayactivity);
mp1 = MediaPlayer.create(GamePlayActivity.this, R.raw.uefa_champion_league);
mp2 = MediaPlayer.create(GamePlayActivity.this, R.raw.the_time_of_our_life);
mp1.start();
mp1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp2.start();
}
});
mp2.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp1.start();
}
});
tvLevel = (TextView) findViewById(R.id.tvLevel);
tvPoint = (TextView) findViewById(R.id.tvPoint);
ivGuessedImage = (ImageView) findViewById(R.id.ivGuessedImage);
btn1stChoice = (Button) findViewById(R.id.btn1stChoice);
btn2ndChoice = (Button) findViewById(R.id.btn2ndChoice);
btn3rdChoice = (Button) findViewById(R.id.btn3rdChoice);
btn4thChoice = (Button) findViewById(R.id.btn4thChoice);
arrPlayer = createPlayer();
Collections.shuffle(arrPlayer);
int resID = getResources().getIdentifier(arrPlayer.get(0).getPlayerFile(), "drawable", getPackageName());
ivGuessedImage.setImageResource(resID);
btn1stChoice.setText(arrPlayer.get(0).getPlayerName());
btn2ndChoice.setText(arrPlayer.get(1).getPlayerName());
btn3rdChoice.setText(arrPlayer.get(2).getPlayerName());
btn4thChoice.setText(arrPlayer.get(3).getPlayerName());
customProgress = (ProgressBar)findViewById(R.id.customProgress);
progressDisplay = (TextView)findViewById(R.id.progressDisplay);
new ShowCustomProgressBarAsyncTask().execute();
if(myProgress < 1){
Intent intent = new Intent(GamePlayActivity.this, GameOverActivity.class);
startActivity(intent);
}
btn1stChoice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (btn1stChoice.getText().toString() == arrPlayer.get(0).getPlayerName()) {
final MediaPlayer mp = MediaPlayer.create(GamePlayActivity.this, R.raw.right_answer);
mp.start();
point = point + 2;
tvPoint.setText(point + " Point");
createNewLevel();
myProgress = 100;
} else {
final MediaPlayer mp = MediaPlayer.create(GamePlayActivity.this, R.raw.wrong_answer);
mp.start();
Intent intent = new Intent(GamePlayActivity.this, GameOverActivity.class);
intent.putExtra("Point", point);
startActivity(intent);
}
}
});
btn2ndChoice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(btn2ndChoice.getText().toString() == arrPlayer.get(0).getPlayerName()){
final MediaPlayer mp = MediaPlayer.create(GamePlayActivity.this, R.raw.right_answer);
mp.start();
point = point + 2;
tvPoint.setText(point + " Point");
createNewLevel();
myProgress = 100;
}
else{
final MediaPlayer mp = MediaPlayer.create(GamePlayActivity.this, R.raw.wrong_answer);
mp.start();
Intent intent = new Intent(GamePlayActivity.this, GameOverActivity.class);
intent.putExtra("Point", point);
startActivity(intent);
}
}
});
btn3rdChoice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(btn3rdChoice.getText().toString() == arrPlayer.get(0).getPlayerName()){
final MediaPlayer mp = MediaPlayer.create(GamePlayActivity.this, R.raw.right_answer);
mp.start();
point = point + 2;
tvPoint.setText(point + " Point");
createNewLevel();
myProgress = 100;
}
else{
final MediaPlayer mp = MediaPlayer.create(GamePlayActivity.this, R.raw.wrong_answer);
mp.start();
Intent intent = new Intent(GamePlayActivity.this, GameOverActivity.class);
intent.putExtra("Point", point);
startActivity(intent);
}
}
});
btn4thChoice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(btn4thChoice.getText().toString() == arrPlayer.get(0).getPlayerName()){
final MediaPlayer mp = MediaPlayer.create(GamePlayActivity.this, R.raw.right_answer);
mp.start();
point = point + 2;
tvPoint.setText(point + " Point");
createNewLevel();
myProgress = 100;
}
else{
final MediaPlayer mp = MediaPlayer.create(GamePlayActivity.this, R.raw.wrong_answer);
mp.start();
Intent intent = new Intent(GamePlayActivity.this, GameOverActivity.class);
intent.putExtra("Point", point);
startActivity(intent);
}
}
});
}
public class ShowCustomProgressBarAsyncTask extends AsyncTask<Void, Integer, Void> {
@Override
protected void onPreExecute() {
myProgress = 100;
}
@Override
protected Void doInBackground(Void... params) {
while(myProgress>0){
myProgress--;
publishProgress(myProgress);
SystemClock.sleep(200);
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
customProgress.setProgress(values[0]);
customProgress.setSecondaryProgress(values[0] + 1);
progressDisplay.setText(String.valueOf(myProgress)+"%");
}
@Override
protected void onPostExecute(Void result) {
}
}
public void createNewLevel(){
arrPlayer.remove(0);
Collections.shuffle(arrPlayer);
int randomButtonChoice = rand.nextInt(4);
int resID = getResources().getIdentifier(arrPlayer.get(0).getPlayerFile(), "drawable", getPackageName());
((BitmapDrawable)ivGuessedImage.getDrawable()).getBitmap().recycle();
ivGuessedImage.setImageResource(resID);
switch (randomButtonChoice){
case 0: {
btn1stChoice.setText(arrPlayer.get(0).getPlayerName());
btn2ndChoice.setText(arrPlayer.get(1).getPlayerName());
btn3rdChoice.setText(arrPlayer.get(2).getPlayerName());
btn4thChoice.setText(arrPlayer.get(3).getPlayerName());
break;
}
case 1: {
btn1stChoice.setText(arrPlayer.get(1).getPlayerName());
btn2ndChoice.setText(arrPlayer.get(0).getPlayerName());
btn3rdChoice.setText(arrPlayer.get(2).getPlayerName());
btn4thChoice.setText(arrPlayer.get(3).getPlayerName());
break;
}
case 2: {
btn1stChoice.setText(arrPlayer.get(1).getPlayerName());
btn2ndChoice.setText(arrPlayer.get(2).getPlayerName());
btn3rdChoice.setText(arrPlayer.get(0).getPlayerName());
btn4thChoice.setText(arrPlayer.get(3).getPlayerName());
break;
}
case 3: {
btn1stChoice.setText(arrPlayer.get(1).getPlayerName());
btn2ndChoice.setText(arrPlayer.get(2).getPlayerName());
btn3rdChoice.setText(arrPlayer.get(3).getPlayerName());
btn4thChoice.setText(arrPlayer.get(0).getPlayerName());
break;
}
}
level++;
tvLevel.setText("Level " + level);
}
}
}
答案 0 :(得分:1)
您可以使用界面
来实现在AsyncTask
中创建一个界面,并创建一个将用作回调的方法
public class ShowCustomProgressBarAsyncTask extends AsyncTask<Void, Integer, Void> {
interface ProgressListner {
public void onProgressChanged(int progress);
}
private ProgressListner listner;
public ShowCustomProgressBarAsyncTask(ProgressListner listner) {
this.listner = listner;
}
//..your remaining codes
@Override
protected void onProgressUpdate(Integer... values) {
//..your remaining codes
listner.onProgressChanged(values[0]);
}
}
然后在您的活动中实现该界面。
还通过构造函数将接口实例传递给AsyncTask
。
然后使用该实例,您可以回调将传递进度值
的方法 public class GamePlayActivity extends Activity implements ProgressListner{
//.. your remaining codes
new ShowCustomProgressBarAsyncTask(this).execute();
public void onProgressChanged(int progress) {
a = progress;
}
}