0

I have to make an app where the user inputs and then the AI responds but when I input some text and press send it gives me the message "Unfortunately, app has stopped".

Here is my code:

Here is the code for sending:

<Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Button"
        android:id="@+id/Send_btn"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:onClick="Zdenka"   />...

Here is the start of the .java file:

    EditText Text, OdgBox;
    String odg;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        EditText Text = (EditText) findViewById(R.id.Txt); //User input
        Button Btn = (Button) findViewById(R.id.Send_btn); //Send button
        TextView Output = (TextView) findViewById(R.id.TextView); //AI output

    }...

And the last part of the java file:

...
public void Zdenka (TextView Output, EditText Text, String odg) {

    String Text1 = Text.toString().toLowerCase();


    if (Text1 == "živjo") {

        odg = "Živjo";

    }
    else if (Text1 == "zivjo") {
        odg = "oj";
    }
    else{ odg = "Ne razumem."; }



    Output.setText(odg);

Thanks for the help!

domen1
  • 11
  • 3
  • You can't use `==` to compare strings in Java. As a result your `odg` string is never initialised in your `Zdenka` method and will be causing a `NullPointerException` when you try to use it. – Squonk Oct 06 '14 at 12:27
  • 1
    Well, the question doesn't say anything about comparing strings. Its wrong close as @Squonk. – MysticMagicϡ Oct 06 '14 at 12:27
  • @MysticMagic : Look at my comment and the code posted by the OP. – Squonk Oct 06 '14 at 12:28
  • 1
    your problem is that the activity is expecting a method with the following signature: `public void Zdenka(View view) {}` – Blackbelt Oct 06 '14 at 12:28
  • I addition to Squonk's answer of using `equalsIngnoreCase`, firstly do this: You will need to replace `public void Zdenka (TextView Output, EditText Text, String odg) {...}` with `public void Zdenka (View v) {`. So your crash will not occur. – MysticMagicϡ Oct 06 '14 at 12:29
  • 1
    @Squonk he is getting crash :) which is due to method not found. – MysticMagicϡ Oct 06 '14 at 12:29
  • @MysticMagic : OK I've reopened the question as I hadn't spotted the `onClick()` method is set in the layout file but the Java doesn't correspond to the correct method signature. The string comparison issue still holds though. – Squonk Oct 06 '14 at 12:33
  • Of course it does. :) @Squonk – MysticMagicϡ Oct 06 '14 at 12:33

1 Answers1

2

What I can see from your code is: You need to change your method of onClick:

You will need to replace

public void Zdenka (TextView Output, EditText Text, String odg) {...}

with

public void Zdenka (View v) {...}

Hope it helps.

And after you do this, make sure to use equals or equalsIgnoreCase for comparing String as Squonk said. == will compare objects, not the actual String.

MysticMagicϡ
  • 28,305
  • 16
  • 71
  • 119