-3

I don't know what I'm doing wrong. I've followed the directions on this said article, but the Integer (c_correct) I'm passing is still not showing.

How to pass integer from one Activity to another?

I've been struggling with this problem for some time now. I've been using the method .setText() and I eventually found out that there are two types. So I made sure to put String.setValue() when putting the integer inside .setText().

P1.java (Player 1)

public void checkAnswer(View view){
    final int read_motion = 0;
    onShake(read_motion);
    //get the pressed
    Button answerBtn = (Button) findViewById(view.getId());
    String btnTest = answerBtn.getText().toString();

    String alertTitle;

    if(btnTest.equals(rightAnswer)){
        alertTitle = "Correct: " + rightAnswer;
        //access this value!
        c_correct = c_correct + 1;
    }
    else{
        //add up 2
        alertTitle = "Nope! Turn the screen to change question";
    }

    //prompt
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(alertTitle);
    builder.setMessage("Turn to change the question - Click OK to get Credit");
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            if (read_motion > 19) {
                //show redundency b/c we dont now what we're doing
                Intent new_start = new Intent(P1.this,ResultActivity.class);
                new_start.putExtra("P1_Score",c_correct);
                startActivity(new_start);

                showNextQuiz();
            } else {
                showNextQuiz();
            }
        }
        //end of line 131
    });
    builder.setCancelable(false);
    builder.show();
}

What I think the problem is: The app implements a Accelerator that would read if the user shakes the phone, so I think whenever the phone shakes, the counter re-sets. But I've looked through the code, and I don't think its the cause of the problem. I've looked through this entire code file, but I don't see any line saying to reset the counter when the phone is flipped.

Here is ResultActivity.java. It takes c_correct from both P1.java and P2.java. It would display the numbers in a title screen.

  public String text = "J";
    @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_result);

    TextView P1_Score = (TextView)findViewById(R.id.Score_P1);
    TextView P2_Score = (TextView)findViewById(R.id.Score_P2);

    Intent mIntent = getIntent();
    int intVal = mIntent.getIntExtra("P1_Score",0);
    P1_Score.setText(String.valueOf(intVal));
//    int p1 = getIntent().getIntExtra("P1_Score", 0);
//    int p2 = getIntent().getIntExtra("P2_Score", 0);

 //   P1_Score.setText(String.valueOf(p1));
 //   P2_Score.setText(String.valueOf(p2));
}

Yes, I've looked through logcat. That's how I realized there was a Resource$notFoumd Exception that lead to to use String.valOf(). Here is the Logcat:

02-24 20:23:58.142 8772-8772/? I/zygote: Not late-enabling -Xcheck:jni 
(already on)
02-24 20:23:58.151 8772-8772/? W/zygote: Unexpected CPU variant for X86 
using 
defaults: x86
02-24 20:23:58.626 8772-8793/com.example.mosesapostol.cvtrivia 
I/OpenGLRenderer: Initialized EGL, version 1.4
02-24 20:23:58.626 8772-8793/com.example.mosesapostol.cvtrivia 
W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, 
retrying without...
02-24 20:24:00.622 8772-8772/com.example.mosesapostol.cvtrivia W/zygote: 
Current dex file has more than one class in it. Calling RetransformClasses 
on 
this class might fail if no transformations are applied to it!

I've also looked at Run also. No signs that there is a NullPointerException or any type of run-time error. ring stopped.

Here is the Run Window Picture of Run in android

I've even looked up the error: Current dex file has more than one class in it. Calling RetransformClasses on this class might fail if no transformations are applied to it!. I already implimented the fix as seen here: Android - transform Classes With Dex For Debug.

I kept changeing the value of c_correct but with no avail. What am I doing wrong?

pyth12
  • 9
  • 1
  • 7
  • Even when the varible name is the same, it still doesn't work. Sorry if my code example was misleading – pyth12 Feb 25 '18 at 06:31

1 Answers1

0

First thing first . Passing parameter in bundle will be available on same keys in which they put at the time of sending .

here i see you have put c_correct in Key "P1_Score" and getting it with key "Score1" .

Keys should be same . Also check for int c_correct value by debugging . If you put as "P1_Score" then you should get it with .

 Intent mIntent = getIntent();
int intVal = mIntent.getIntExtra("P1_Score",0);
P1_Score.setText(String.valueOf(intVal));
ADM
  • 20,406
  • 11
  • 52
  • 83
  • I made the error when posting the problem. Even when the varible is the same, it still doesn't work – pyth12 Feb 25 '18 at 06:26
  • Debug your code. if the statement `if (read_motion > 19)` is true then it should work . – ADM Feb 25 '18 at 06:58