0

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.

Community
  • 1
  • 1
Tenali_raman
  • 2,000
  • 3
  • 18
  • 28

4 Answers4

6

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());
Sebastian Pakieła
  • 2,970
  • 1
  • 16
  • 24
3

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());
Anton Shkurenko
  • 4,301
  • 5
  • 29
  • 64
2

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

Jonty800
  • 482
  • 7
  • 20
1

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());