简体   繁体   中英

If ... Else statement seems to be returning the wrong output

I am just exploring different methods to test out Log class in java, where I use Log.d() if a certain text view is shown and Log.e() to log an exception error if otherwise.

However, the logcat seems to be showing the opposite output instead. Why is that so?

My main activity code:

package com.example.dummy_app;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (findViewById(R.id.editText).toString().equals("Happy Birthday!")) {
            Log.d("logMessage", "correct message shown");
        } else {
            Log.e("logMessage", "exception error");
        }
    }
}

XML --

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Happy Birthday!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

The logcat is showing "exception error" instead of "correct message shown". I have also tried removing the toString() but it still shows the same result. Can anyone explain why? 在此处输入图片说明

change:

findViewById(R.id.editText).toString().equals("Happy Birthday!")

to:

findViewById(R.id.editText).text.toString().equals("Happy Birthday!")

Your issue is with the following statement,

findViewById(R.id.editText).toString().equals("Happy Birthday!")

Don't declareTextView like that. It is not a good practice. First, declare your TextView as follows,

TextView textView = (TextView) findViewById(R.id.textView);

Now you can finally do whatever you want with it like getting input as follows,

String inputTxt = textView.getText().toString();

So your full code should be like the following,

package com.example.dummy_app;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView textView = (TextView) findViewById(R.id.textView);

        if (textView.getText().toString().equals("Happy Birthday!")) {
            Log.d("logMessage", "correct message shown");
        } else {
            Log.e("logMessage", "exception error");
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM