Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
73 views
in Technique[技术] by (71.8m points)

java - I would like to set my variables at the top of my class instead of in the method

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) {
    }
}
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can't initialize Views 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...