• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps App keeps crashing (because of threads)

hyerikim

Lurker
I'm trying to update the score using threads so that when the score is greater than or equal to 4 the next level button background is changed.

Here is the code Lylevel1 (Lyrics Level 1)

Code:
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);

    }

This activity is trying to change button controls of activity "Levelwho" (group of buttons from level 1 to level 10) so when score of lylevel1 is >= 4 the button's background of lylevel2 is changed automatically. But when I click on lylevel1 button the app crashes. Any help?

Here is the logcat
ecdv4.jpg
 
Back
Top Bottom