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 trialGwynneth Gwara
3,515 PointsUse 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.).
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
19,189 PointsYou'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.
Karen Fletcher
19,189 PointsThat 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!");
}
}
Tawanda Mukungureyi
7,361 Pointsin place of val TAG = "TreehouseActivity" use val TAG = TreehouseActivity::class.java.simpleName and you are good to go
Silas Benza
5,117 Pointspackage 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!");
} }
Gwynneth Gwara
3,515 PointsGwynneth Gwara
3,515 PointsIf 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();