Problem with CheatActivity


#1

I’m having a bit of trouble with CheatActivity and the judgment toast that’s supposed to pop up. For some reason, my GeoQuiz will start up fine and work perfectly fine if I’m not trying to cheat. When I cheat, it lets me see the answer, but when I go back to QuizActivity, neither my true nor false buttons work. If I hit next or previous and then come back to the question, I can answer it again, but not if I try to cheat.

I have no idea how it’s happening and why! Help! Here is my code:

CheatActivity

[code]package com.example.asta.geoquiz;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**

  • Created by Asta on 1/13/2015.
    */
    public class CheatActivity extends Activity{

    public static final String EXTRA_ANSWER_IS_TRUE=“com.example.asta.geoquiz.answer_is_true”; //adds extra constant
    public static final String EXTRA_ANSWER_SHOWN=“com.example.asta.geoquiz.answer_shown”;

    private boolean mAnswerIsTrue;
    private TextView mAnswerTextView;
    private Button mShowAnswer;

    private void setAnswerShownResult(boolean isAnswerShown){
    Intent data=new Intent();
    data.putExtra(EXTRA_ANSWER_SHOWN, isAnswerShown);
    setResult(RESULT_OK, data);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cheat);

     mAnswerIsTrue=getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);
    
     mAnswerTextView= (TextView)findViewById(R.id.answerTextView);
    
     //Answer will not be shown until user presses button
     setAnswerShownResult(false);
     mShowAnswer=(Button)findViewById(R.id.showAnswerButton);
     mShowAnswer.setOnClickListener(new View.OnClickListener()
     {
         @Override
         public void onClick (View v)
         {
             if (mAnswerIsTrue)
             {
                 mAnswerTextView.setText(R.string.true_button);
             }
             else
             {
                 mAnswerTextView.setText(R.string.false_button);
             }
    
            setAnswerShownResult(true);
         }
     });
    

    }
    }
    [/code]

QuizActivity

[code]package com.example.asta.geoquiz;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class QuizActivity extends ActionBarActivity {
private static final String TAG=“QuizActivity”;
private static final String KEY_INDEX=“index”;
private Button mTrueButton;
private Button mFalseButton;
private Button mNextButton;
private Button mPreviousButton;
private Button mCheatButton;
private TextView mQuestionTextView;

private TrueFalse[] mQuestionBank= new TrueFalse[]{
        new TrueFalse(R.string.question_oceans, true),
        new TrueFalse(R.string.question_mideast, false),
        new TrueFalse(R.string.question_africa, false),
        new TrueFalse(R.string.question_americas, true),
        new TrueFalse(R.string.question_asia, true),

};

private int mCurrentIndex=0;
private boolean mIsCheater;




private void updateQuestion(){
   // Log.d(TAG, "Updating question text for question #" + mCurrentIndex, new Exception());
    int question=mQuestionBank[mCurrentIndex].getQuestion();
    mQuestionTextView.setText(question);
};

private void checkAnswer(boolean userPressedTrue){
    boolean answerIsTrue=mQuestionBank[mCurrentIndex].isTrueQuestion();
    int messageResId=0;
    if (mIsCheater) {
        messageResId = R.string.judgment_toast;
    }
    else
    {
    if (userPressedTrue == answerIsTrue)
    {
        messageResId=R.string.correct_toast;
    }
    else
    {
        messageResId=R.string.incorrect_toast;
    }
    Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show();

}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG, "onCreate(Bundle) called");
    setContentView(R.layout.activity_quiz);

   mQuestionTextView=(TextView)findViewById(R.id.question_text_view);


   mTrueButton=(Button)findViewById(R.id.true_button);

    mTrueButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v)
        {
            checkAnswer(true);
        }

    });

    mFalseButton=(Button)findViewById(R.id.false_button);
    mFalseButton.setOnClickListener(new View.OnClickListener(){
       @Override
        public void onClick(View v)
       {
           checkAnswer(false);
       }
    });

    mNextButton=(Button)findViewById(R.id.next_button);
    mNextButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick (View v)
        {
            mCurrentIndex=(mCurrentIndex +1) % mQuestionBank.length;
            mIsCheater=false;
            updateQuestion();
        }

    });

   mPreviousButton=(Button)findViewById(R.id.previous_button);
   mPreviousButton.setOnClickListener(new View.OnClickListener()
   {
       @Override
       public void onClick (View v)
       {
            if (mCurrentIndex==0)
            {
                mCurrentIndex=mQuestionBank.length-1;
                updateQuestion();
            }
           else
            {
                mCurrentIndex=mCurrentIndex-1;
                updateQuestion();
            }
       }
   });

    if (savedInstanceState!=null)
    {
        mCurrentIndex=savedInstanceState.getInt(KEY_INDEX, 0);
    }

    mCheatButton=(Button)findViewById(R.id.cheat_button);
    mCheatButton.setOnClickListener(new View.OnClickListener(){
        @Override
            public void onClick(View v){
                    Intent i=new Intent(QuizActivity.this, CheatActivity.class);
                    boolean answerIsTrue=mQuestionBank[mCurrentIndex].isTrueQuestion();
                    i.putExtra(CheatActivity.EXTRA_ANSWER_IS_TRUE, answerIsTrue);
                    startActivityForResult(i, 0);
        }
    });

    updateQuestion();



}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (data == null)
    {
        return;
    }

    mIsCheater=data.getBooleanExtra(CheatActivity.EXTRA_ANSWER_SHOWN, false);

}
@Override
public void onSaveInstanceState(Bundle savedInstanceState)
{
    super.onSaveInstanceState(savedInstanceState);
    Log.i(TAG, "onSaveInstanceState");
    savedInstanceState.putInt(KEY_INDEX, mCurrentIndex);
}
@Override
public void onStart(){
    super.onStart();
    Log.d(TAG, "onStart() called");
}

@Override
public void onPause(){
    super.onPause();
    Log.d(TAG, "onPause() called");
}

@Override
public void onResume(){
    super.onResume();
    Log.d(TAG, "onResume() called");
}
@Override
public void onStop(){
    super.onStop();
    Log.d(TAG, "onStop() called");
}

@Override
public void onDestroy(){
    super.onDestroy();
    Log.d(TAG, "onDestroy() called");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_quiz, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}
[/code]


#2

Hello:
I hava got the same problem as you.
Then I found there are some errors in logcat when I push back button of the emulator in cheat activity.
Here is the output of the logcat. Can anyone help us?

02-10 06:15:18.634: E/AndroidRuntime(1849): FATAL EXCEPTION: main
02-10 06:15:18.634: E/AndroidRuntime(1849): java.lang.StackOverflowError
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.net.Uri.writeToParcel(Uri.java:1787)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.content.Intent.writeToParcel(Intent.java:6624)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeParcelable(Parcel.java:1254)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeValue(Parcel.java:1173)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeMapInternal(Parcel.java:591)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Bundle.writeToParcel(Bundle.java:1619)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeBundle(Parcel.java:605)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.content.Intent.writeToParcel(Intent.java:6660)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeParcelable(Parcel.java:1254)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeValue(Parcel.java:1173)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeMapInternal(Parcel.java:591)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Bundle.writeToParcel(Bundle.java:1619)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeBundle(Parcel.java:605)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.content.Intent.writeToParcel(Intent.java:6660)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeParcelable(Parcel.java:1254)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeValue(Parcel.java:1173)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeMapInternal(Parcel.java:591)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Bundle.writeToParcel(Bundle.java:1619)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeBundle(Parcel.java:605)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.content.Intent.writeToParcel(Intent.java:6660)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeParcelable(Parcel.java:1254)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeValue(Parcel.java:1173)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeMapInternal(Parcel.java:591)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Bundle.writeToParcel(Bundle.java:1619)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeBundle(Parcel.java:605)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.content.Intent.writeToParcel(Intent.java:6660)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeParcelable(Parcel.java:1254)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeValue(Parcel.java:1173)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeMapInternal(Parcel.java:591)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Bundle.writeToParcel(Bundle.java:1619)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeBundle(Parcel.java:605)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.content.Intent.writeToParcel(Intent.java:6660)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeParcelable(Parcel.java:1254)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeValue(Parcel.java:1173)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeMapInternal(Parcel.java:591)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Bundle.writeToParcel(Bundle.java:1619)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeBundle(Parcel.java:605)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.content.Intent.writeToParcel(Intent.java:6660)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeParcelable(Parcel.java:1254)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeValue(Parcel.java:1173)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeMapInternal(Parcel.java:591)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Bundle.writeToParcel(Bundle.java:1619)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeBundle(Parcel.java:605)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.content.Intent.writeToParcel(Intent.java:6660)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeParcelable(Parcel.java:1254)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeValue(Parcel.java:1173)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeMapInternal(Parcel.java:591)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Bundle.writeToParcel(Bundle.java:1619)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeBundle(Parcel.java:605)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.content.Intent.writeToParcel(Intent.java:6660)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeParcelable(Parcel.java:1254)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeValue(Parcel.java:1173)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeMapInternal(Parcel.java:591)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Bundle.writeToParcel(Bundle.java:1619)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeBundle(Parcel.java:605)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.content.Intent.writeToParcel(Intent.java:6660)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeParcelable(Parcel.java:1254)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeValue(Parcel.java:1173)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeMapInternal(Parcel.java:591)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Bundle.writeToParcel(Bundle.java:1619)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeBundle(Parcel.java:605)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.content.Intent.writeToParcel(Intent.java:6660)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeParcelable(Parcel.java:1254)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeValue(Parcel.java:1173)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeMapInternal(Parcel.java:591)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Bundle.writeToParcel(Bundle.java:1619)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeBundle(Parcel.java:605)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.content.Intent.writeToParcel(Intent.java:6660)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeParcelable(Parcel.java:1254)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeValue(Parcel.java:1173)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeMapInternal(Parcel.java:591)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Bundle.writeToParcel(Bundle.java:1619)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.os.Parcel.writeBundle(Parcel.java:605)
02-10 06:15:18.634: E/AndroidRuntime(1849): at android.content.Intent.writeToP
02-10 06:15:18.644: D/dalvikvm(1849): GC_CONCURRENT freed 465K, 17% free 2901K/3476K, paused 4ms+1ms, total 6ms
02-10 06:15:26.465: E/Trace(1886): error opening trace file: No such file or directory (2)
02-10 06:15:26.465: W/Trace(1886): Unexpected value from nativeGetEnabledTags: 0