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 trialneil booc
140 PointsAccessing View code
Watching the video as i go along .... where do you put declare view variable ... does it have to be in order?? ... the guy just puts it between the override
2 Answers
Ben Jakuben
Treehouse TeacherHi Neil,
Can you paste in the code you are referring to? I'd be happy to explain things in more detail. It will also be good for me to know where the confusion is so I can adjust my explanation in the videos the next time around.
neil booc
140 Pointspackage com.example.driod;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
}
}
i wana know where you input declare view variable ... it doesnt say in the video .. he just puts it in between the override
Ben Jakuben
Treehouse TeacherOkay, so what we have here are a few different methods in the MainActivity class. The latest code generated by Eclipse is a little different than what is in the videos, which probably leads to some confusion. I'll be updating all the videos in this course soon.
Anyhow, each method is a reusable block of code that can be called from elsewhere in code. So the first one we have is the onCreate()
method. It has other information that declares its availability (public), return type (void), and parameters (Bundle savedInstanceState). Then all the code for the method goes between the left and right curly braces: { }
.
Those curly braces are key in Java. They denote blocks of code. So in the video, notice that I declare the variables inside those two curly braces for the onCreate()
method. And the order of the statements matter; you can't set View variables until after the line setContentView(R.layout.activity_main);
is called.
Try to code along just like the video and let me know if you have any follow up questions. We talk more about the variables and their scope within the program as the course goes on.
neil booc
140 Pointsi wana know where you input declare view variable ... it doesnt say in the video .. he just puts it in between the override
package com.example.driod;
import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar; import android.support.v4.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.os.Build;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
neil booc
140 Pointsneil booc
140 Pointsi wana know where you input declare view variable ... it doesnt say in the video .. he just puts it in between the override