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

Help with using the Log function in Android

Inside the 'onCreate()' method (after the 'setContentView()' call), log the message "Activity created!" using the Log debug method (remember that it's just one letter). The 1st parameter should be "MainActivity", but use the member variable instead of typing it out a second time. The 2nd parameter is the message "Activity created!"

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Hi Milo,

Can you paste your code in here so we can see what is wrong? Or if you have any specific questions do let us know. :smile:

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";

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

  }
}

super . onCreate(savedInstanceState) log.d ("MainActivity","Activity created!");

2 Answers

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";

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

Im getting the error message: Compilation Error! The Log debug call must be inside the onCreate() method, and don't forget 'Log' before the method call since it's a static method of the Log class.

Please hint me :)

Ben Jakuben
Ben Jakuben
Treehouse Teacher

That capital "D" in there isn't doing you any favors. :wink:

The naming convention in Java is that Class names begin with capital letters and pretty much everything else (method names and variable names) begin with lowercase letters (with some exceptions, of course).

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

So with the line you pasted, it looks like some of the original code was deleted (the semicolon at the end of the onCreate() call and the setContentView line). Your Log statement needs to start with a capital L. :smile:

Bummer! Make sure you call the Log.d() method at the end of the onCreate() method!

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";

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

} }

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Check out the instructions again, specifically this part: "(after the 'setContentView()' call), log the message..." :smile: