Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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: