-2

I have a problem with the application Quiz to college.
Even after I pass the right answer application crashes: Incorrect on the main activity screen.
Please help me.
I think the issue lies in the transmission of a variable between the activities or in the if instruction.

My MainActivity code:

package com.example.mariusz.qiuz;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;

import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {


    Button b3;
    private static final String answer_1= "Mariusz";
    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b3 = (Button) findViewById(R.id.answer1);


        b3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                startActivity(new Intent(MainActivity.this, Answer1Activity.class));

            }
        });
        Intent intent = getIntent();
        String message = intent.getStringExtra(Answer1Activity.EXTRA_MESSAGE);
        if (message != null ) {

            if (message == answer_1) {
                TextView textView_1 = new TextView(MainActivity.this);
                textView_1.setGravity(Gravity.CENTER);
                textView_1.setTextSize(40);
                textView_1.setText(R.string.correct);
                LinearLayout activity_main = (LinearLayout)findViewById(R.id.layout);
                activity_main.addView(textView_1);
            } else {
                TextView textView_1 = new TextView(MainActivity.this);
                textView_1.setGravity(Gravity.CENTER);
                textView_1.setTextSize(40);
                textView_1.setText(R.string.incorrect);
                LinearLayout activity_main = (LinearLayout)findViewById(R.id.layout);
                activity_main.addView(textView_1);
            }
        }
        else {

        }
            }

    }

And my second activity code:

package com.example.mariusz.qiuz;

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

/**
 * Created by Mariusz on 2016-03-04.
 */
public class Answer1Activity extends Activity {

    public final static String EXTRA_MESSAGE = "com.example.mariusz.quiz.MESSAGE";

    Button a1;
    EditText e1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.answer1);
        a1 = (Button) findViewById(R.id.buttona1);
        e1 = (EditText) findViewById(R.id.text1);
        a1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                Intent intent = new Intent(Answer1Activity.this, MainActivity.class);

                String message = e1.getText().toString();
                intent.putExtra(EXTRA_MESSAGE, message);
                startActivity(intent);


            }
        });


    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • I would suggest using if (message.contains(answer_1)) { } instead of '==' for Strings – Hosein Hamedi Mar 06 '16 at 08:41
  • 1
    Also mind that `package com.example.mariusz.qiuz;` is misspelled. And this `public final static String EXTRA_MESSAGE = "com.example.mariusz.quiz.MESSAGE";` will cause an error (because this one is NOT misspelled). – Phantômaxx Mar 06 '16 at 08:53

2 Answers2

1

It's not the proper way of checking two Strings, So try replacing this line

if (message == answer_1) {

With

if (message.equalsIgnoreCase(answer_1)) {

Note : I used equalsIgnoreCase because I thought you don't need to make it case sensitive for given answer.

Shree Krishna
  • 8,474
  • 6
  • 40
  • 68
1

always use string.equals(String other) function to compare strings, not the == operator.

The string.equals(String other) function checks the actual contents of the string,however the == operator checks whether the references to the objects are equal.

equalsIgnoreCase is another function which is used when case is not considered

SSH
  • 1,609
  • 2
  • 22
  • 42