dsdsdsdsd dsdsdsdsd - 7 months ago 12
Java Question

AndroidStudio: How to output an object to the console?

In AndroidStudio I would like to take a look at an object, similar to a

print_r(an_object)
in php, or a
console.log(an_object)
in javascript (in browser)

//my.java
public void a_method( SomeClass the_arg )
{ Log.d( "an_id" , the_arg );
}


This generates an error message:


Error:(23, 10) error: no suitable method found for d(String,View)
method Log.d(String,String,Throwable) is not applicable
(actual and formal argument lists differ in length)
method Log.d(String,String) is not applicable
(actual argument View cannot be converted to String by method invocation conversion)






"DownVoters should explain their reasons, otherwise be trolls" - shakespeare

Answer

You cannot print an object to the console in Java as you would in javascript.

You have three solutions.

1) Use debugger. Place a breakpoint and debug in android studio. Then you can inspect the full object in scope.

2) Serialize your object (for example to JSON) and print the result to console.

3) Override toString method of your object to give you all the information you want and call Log.d("myTag", yourObj.toString())

I highly recommend first method. I used to avoid Debugger but learning how to use the debugger was the best thing I did. It increases your efficiency and makes debugging super easy