I can't seem to tackle this confusing problem, I have lots and lots of things that I would like to add at the top of my class to help cut down on clutter.
Since multiple methods use these checkbox variables.
I would like to have everything at the top directly under the opening bracket.
Here's what works, but not what I want.:
public class MyClass extends Activity implements View.OnClickListener {
//leaving out most code like onCreate. Just pretend it's there.
public void checkboth(View view) {
CheckBox cb1 = (CheckBox) findViewById(R.id.cb1);
CheckBox cb2 = (CheckBox) findViewById(R.id.cb2);
cb1.setchecked(true);
cb2.setchecked(true);
}
@Override
public void onClick(View v) {
}
}
But for the life of me I can't figure out why I can't do this:
public class MyClass extends Activity implements View.OnClickListener {
CheckBox cb1 = (CheckBox) findViewById(R.id.cb1);
CheckBox cb2 = (CheckBox) findViewById(R.id.cb2);
//leaving out most code like onCreate. Just pretend it's there.
public void checkboth(View view) {
cb1.setchecked(true);
cb2.setchecked(true);
}
@Override
public void onClick(View v) {
}
}
You can't initialize View
s outside of a method like this
CheckBox cb1 = (CheckBox) findViewById(R.id.cb1);
CheckBox cb2 = (CheckBox) findViewById(R.id.cb2);
because that would mean that these lines run before onCreate()
which results in these variables being null
and throw a NPE
when you try to call methods on these variables. This is because you call setContentView(R.layout.your_layout)
inside of onCreate()
and your Views
"live" inside of that layout
. This means that they can't be initialized until your layout
has been inflated
by calling setContentView()
. You must call setContentView()
before trying to initialize your Views
. There is no way around that part.
What some people do that might help is to create a separate function that initializes these variables just after setContentView()
is called. Something like this
public class MyActivity
// declare your Views so they are global to the class
TextView tv;
// more views
@Override
public void onCreat(stuff)
{
// super call
setContentView(R.layout.my_layout);
init();
then in your init()
function initialize all of the Views
you have declared
private void init()
{
tv = (TextView) findViewById(R.id.myTextView);
// more initializations
}
but you can just initialize them in onCreate()
, onResume()
or wherever as long as it's after setContentView()
Declaring and initializing them in this way will make sure they are all available to other functions, listeners, inner classes, etc... of the Activity
. And if you have a lot of Views
it may lessen the "clutter" a bit.
findViewById is a method that can be run from the activity (or any view group) after the view has been inflated. To inflate a view a the activity level, you usually call setContentView.
Since setContentView has not been called yet, no views are attached to the activity.
Here is how to do it
public class MyClass extends Activity implements View.OnClickListener {
CheckBox cb1;
CheckBox cb2;
public void onCreate(Bundle sis) {
super.onCreate(sis);
setContentView(R.layout.whatever);
cb1 = (CheckBox) findViewById(R.id.cb1);
cb2 = (CheckBox) findViewById(R.id.cb2);
}
public void checkboth(View view) {
cb1.setchecked(true);
cb2.setchecked(true);
}
@Override
public void onClick(View v) {
}
}
You can't call method (findViewById) from here and expect a non null value. The views are only created in android after you call setContentView(R.layout.my_layout). What you have to do is something like this:
public class MyClass extends Activity implements View.OnClickListener {
CheckBox cb1 = null;
CheckBox cb2 = null;
//leaving out most code like onCreate. Just pretend it's there.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate();
setContentView(R.layout.my_layout);
cb1 = (CheckBox) findViewById(R.id.cb1);
cb2 = (CheckBox) findViewById(R.id.cb2);
}
public void checkboth(View view) {
cb1.setchecked(true);
cb2.setchecked(true);
}
@Override
public void onClick(View v) {
}
}
When you move the declarations to the top of the class, you change local variables to fields. These fields are initialized when an instance is created - which means they are initialized "right before" the constructor executes.
Now, you can not call instance methods (like findViewById
) before the implicit call to super()
finishes.
Move them to the constructor, and things should work out.