Reputation: 2962
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), but for Android.
I tried this
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)
Upvotes: 24
Views: 43890
Reputation: 11
You can try using Gson class as shown below :
public void yourMethod(SomeClass theObject) {
Gson gson = new Gson();
Log.d( "sample" , gson.toJson(theObject));
}
Here is its Gradle repository
implementation 'com.google.code.gson:gson:2.8.6'
Upvotes: 1
Reputation: 1486
Convert object to JSON. You can use Gson.
val gson = Gson()
val json = gson.toJson(yourObject)
Log.e(TAG, json)
Or
Log.e(TAG, Gson().toJson(yourObject))
Upvotes: 12
Reputation: 538
It's a bit easy to output the data object in the logcat.
The correct way is to override
the toString()
inside the object.
It can be generated by the Android Studio itself by following this simple steps:
1- Open the DataClass
2- Press Alt+Insert OR right click and click on Generate...
3- In Generate window select toString()
4- Select all the variables in the next window and click OK
5- toString()
method would override
in your data class returning the String
template of your data model.
Cheers!
Upvotes: 1
Reputation: 76689
just wrote one generic method, which makes it possible trough reflection:
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (Field field : this.getClass().getDeclaredFields()) {
String item;
if(! field.getType().getSimpleName().equals("ArrayList")) {
try {
Object value = field.get(this);
item = String.format("%s %s %s: %s%n", Modifier.toString(field.getModifiers()), field.getType().getSimpleName(), field.getName(), String.valueOf(value));
sb.append(item);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} else {
item = String.format("%s %s %s: ArrayList<>%n", Modifier.toString(field.getModifiers()), field.getType().getSimpleName(), field.getName());
sb.append(item);
}
}
return sb.toString();
}
Upvotes: 0
Reputation: 191738
The second argument must be a String.
Log.d("an_id", String.valueOf(the_arg));
Your error says you can't log a View
class
no suitable method found for d(String,View)
Don't be surprised when you see some nonsense in the console when you print that View
object through using String.valueOf
Upvotes: 7
Reputation: 2300
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
Upvotes: 29