我在嘗試使用線程更新分數,以便當分數大於或等於4時,下一級別按鈕背景被更改。使用線程更新玩家得分
下面是代碼Lylevel1(歌詞級別1)
public class Lylevel1 extends Activity {
List<Question> quesList;
int score=0;
int qid=0;
Question currentQ;
TextView txtQuestion;
RadioButton rda, rdb, rdc;
Button butNext;
ImageButton v1;
ImageView v2;
Button b2;
protected Lyricswho context;
//add a constructor with the Context of your activity
public Lylevel1(Lyricswho _context){
context = _context;
}
public MediaPlayer mp = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lylevel1);
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
Dblyrlevel1 db=new Dblyrlevel1(this);
quesList=db.getAllQuestions();
currentQ=quesList.get(qid);
txtQuestion=(TextView)findViewById(R.id.textView1);
rda=(RadioButton)findViewById(R.id.radio0);
rdb=(RadioButton)findViewById(R.id.radio1);
rdc=(RadioButton)findViewById(R.id.radio2);
butNext=(Button)findViewById(R.id.button1);
v1=(ImageButton)findViewById(R.id.imageButton);
v1.setImageResource(R.drawable.play);
// v2 = (ImageView)findViewById(R.id.imageView);
// b2 = (Button) findViewById(R.id.button9);
setQuestionView();
butNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mp != null) {
mp.stop();
mp = null;
}
v1.setImageResource(R.drawable.play);
RadioGroup grp = (RadioGroup) findViewById(R.id.radioGroup1);
RadioButton answer = (RadioButton) findViewById(grp.getCheckedRadioButtonId());
Log.d("yourans", currentQ.getANSWER() + " " + answer.getText());
if (answer.getText().equals(currentQ.getANSWER())) {
score++;
Log.d("score", "Your score" + score);
Toast.makeText(getApplicationContext(),"Correct!", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(),"Wrong! Try again", Toast.LENGTH_SHORT).show();
}
SharedPreferences pref = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = pref.edit();
// Set/Store data
edit.putInt("score1", score);
edit.commit();
final int score1 = pref.getInt("score1", 0);
context.runOnUiThread(new Runnable() {
@Override
public void run() {
if (score1 >= 4) {
b2.getBackground().setAlpha(150);
v2.setImageResource(android.R.color.transparent);
}
} });
此活動試圖更改活動「Levelwho」(從1級到10級的按鈕組)的按鈕控件,因此當lylevel1的得分大於等於4時,lylevel2的按鈕背景會自動更改。但是當我點擊lylevel1按鈕的應用程序崩潰。任何幫助?
人誰可以回答這個問題???我卡在這一點上一週:/
如果您發佈LOGCAT,它將會更容易幫助您。 –
LogCat錯誤... –
@SyedNazarMuhammad logCat添加。 – hyeri