Tenali_raman
Tenali_raman

Reputation: 2192

How to log results to the console in android studio

i have just made a small test case in my android studio project , see code below :

public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {

    MainActivity activity;



    public MainActivityTest() {
        super(MainActivity.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        activity = getActivity();
    }

    public void testMainActivity() {
        TextView textView = (TextView) activity.findViewById(R.id.hello_world);
        Log.d(textView);
        assertNotNull(textView);
    }
}

now all i wanted to do was log the value of textView to the console , so i referred the documentation and saw that i could console.log() results(just like in javascript) using Log.d(testView); .

but the problem is the below line in my code :

Log.d(testView); , causes an error , when i hover over Log i get the message saying "cannot resolve symbol Log" .

so my question is how do i log results to the console in android studio .

I refered to THIS question too but i'am still stuck.

Upvotes: 1

Views: 5032

Answers (4)

Alex Cortinovis
Alex Cortinovis

Reputation: 166

Did you import it?

import android.util.Log

And according to the documentation you have to put at least 2 strings to the function, a TAG and a message:

log.d("MainActivityTest", textView.getText());

Upvotes: 1

Jonty800
Jonty800

Reputation: 522

Log is a part of android.util.Log. So you must first import this.

Log uses a tag as it's first parameter, and an output string as it's second. For example:

private static final String TAG = "MyActivity";
Log.d(TAG, "index=" + i);

You could also do System.out.println("My string here");

But please note that there is an error in your code. Log requires a string value, which can be fetched using testView.getText().toString() instead of testView

Upvotes: 2

Sebastian Pakieła
Sebastian Pakieła

Reputation: 3029

It saying this because there is no Log.d(TextView textView) method. Here is a doc https://developer.android.com/reference/android/util/Log.html. But there is a Log.d(String tag, String message) method. Then call it like

Log.d("Message tag",textView.getText().toString());

Upvotes: 6

Anton Shkurenko
Anton Shkurenko

Reputation: 4327

Log.d("TAG", "Message");

1) Use capital letter as Log not log 2) It has two params (or three), not just one

You can log textview value by

TextView textView = (TextView) activity.findViewById(R.id.hello_world);
Log.d("TAG", textView.getText().toString());

Upvotes: 3

Related Questions