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 Build a Simple Android App with Kotlin Testing and Debugging Writing to the Log

Use a method to set the TAG variable instead of hard-coding the class name as "TreehouseActivity". We want just the clas

Use a method to set the TAG variable instead of hard-coding the class name as "TreehouseActivity". We want just the class name by itself without the package name: is it getName() or getSimpleName()? Change the "TreehouseActivity" string to the correct method call (ex. TreehouseActivity.class.).

TreehouseActivity.kt
import android.app.Activity
import android.os.Bundle

class TreehouseActivity : Activity()
{
  val TAG = "TreehouseActivity"

  override fun onCreate(savedInstanceState: Bundle) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_treehouse)
            Log.d(TAG, "Activity Created!");
    public static final String TAG = TreehouseActivity.class.getSimpleName(); 


  }
}

4 Answers

Karen Fletcher
Karen Fletcher
19,189 Points

You're doing great and on the right track! Your log statement is in the right place and formatted correctly. However, on the line right below the log, you're redefining TAG which is likely what's causing your error. Try this challenge again after you remove that last line.

If I remove the last line I am getting these errors TreehouseActivity.kt:3:8: error: expecting a top level declaration public static final String TAG = TreehouseActivity.class.getSimpleName(); ^ TreehouseActivity.kt:3:21: error: expecting a top level declaration public static final String TAG = TreehouseActivity.class.getSimpleName(); ^ TreehouseActivity.kt:3:28: error: expecting a top level declaration public static final String TAG = TreehouseActivity.class.getSimpleName(); ^ TreehouseActivity.kt:3:32: error: expecting a top level declaration public static final String TAG = TreehouseActivity.class.getSimpleName(); ^ TreehouseActivity.kt:3:34: error: expecting a top level declaration public static final String TAG = TreehouseActivity.class.getSimpleName(); ^ TreehouseActivity.kt:3:51: error: expecting a top level declaration public static final String TAG = TreehouseActivity.class.getSimpleName(); ^ TreehouseActivity.kt:3:57: error: name expected public static final String TAG = TreehouseActivity.class.getSimpleName(); ^ TreehouseActivity.kt:3:58: error: expecting a top level declaration public static final String TAG = TreehouseActivity.class.getSimpleName(); ^ TreehouseActivity.kt:3:71: error: expecting a top level declaration public static final String TAG = TreehouseActivity.class.getSimpleName(); ^ TreehouseActivity.kt:3:72: error: expecting a top level declaration public static final String TAG = TreehouseActivity.class.getSimpleName();

Karen Fletcher
Karen Fletcher
19,189 Points

That is a really strange error. When I take your code and paste it in to the challenge, minus the line public static final String TAG it passes for me. When you try the challenge, does your answer look like this:

import android.app.Activity
import android.os.Bundle

class TreehouseActivity : Activity()
{
  val TAG = "TreehouseActivity"

  override fun onCreate(savedInstanceState: Bundle) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_treehouse)
            Log.d(TAG, "Activity Created!");


  }
}

in place of val TAG = "TreehouseActivity" use val TAG = TreehouseActivity::class.java.simpleName and you are good to go

package com.teamtreehouse;

public class TreehouseActivity extends Activity {

public static final String TAG = TreehouseActivity.class.getSimpleName();

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

} }