Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Code Challenge: The Android Log, Part 2

Hey everybody,

I seem to be having some difficulty getting past the second question in the Android Log code challenge.

The error I am receiving: Bummer! Compilation Error! Make sure you aren't using the String "MainActivity" from Task 1, and don't forget to add '.class' before the method call.

Here is my code:

    package com.example;

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

public class MainActivity extends Activity {

  public static final String TAG = MainActivity.class.getSimpleName;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Log.d(TAG, "Activity created!");
  }
}

I'm using .getSimpleName because in the video says that it doesn't include the package, like the prompt is asking for.

The solution is probably right in front of me but I'm not sure what I'm doing wrong, the code is almost identical to my MainActivity.java file. Thanks! And sorry, I can't seem to get the formatting right.

2 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

So close! getSimpleName is a method call. In Java, methods always have parentheses, even if there are no parameters. So in this case, the correct syntax to call that method is getSimpleName(). Without the parenthesis the system thinks you are trying to access a variable named getSimpleName, which doesn't exist (hence the Compilation Error).

Thanks Ben! This is the first time I got stuck on the course, it's really well laid out and easy to understand. Thanks again.

Thank you for posting this question David, and thank you for the answer Ben. The "try the other one" hint wasn't quite enough for me so I was stuck at ...MainActivity.class.getName();