2015-09-20 42 views
0

我在嘗試使用線程更新分數,以便當分數大於或等於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按鈕的應用程序崩潰。任何幫助?

enter image description here

人誰可以回答這個問題???我卡在這一點上一週:/

+0

如果您發佈LOGCAT,它將會更容易幫助您。 –

+0

LogCat錯誤... –

+0

@SyedNazarMuhammad logCat添加。 – hyeri

回答

0

你的分數參考宣佈決賽:

final int score1 = pref.getInt("score1", 0); 

,這樣的值將不會更新。

需要解決的問題是: 1)將「preferences」對象設置爲您在可運行參考中引用的「最終」值。 2)獲取可運行對象中score1的值。

更好的是,在創建runnable的時候,您已經知道「score」的值,爲什麼不把它放在該函數的「if」塊中,並且只有在得分爲正確的值(如果分數沒有被設置爲正確的值,甚至不會在ui線程上運行那個可運行的)。

+0

我使用的是socre1,因爲它存儲在共享的首選項文件中,所以下次用戶嘗試玩遊戲時,按鈕的背景也是相同的,即解鎖。我會嘗試最後的東西感謝您的提示:) – hyeri

相關問題