我想在我的班上名列前茅而不是在方法来设置我的变量 [英] I would like to set my variables at the top of my class instead of in the method

查看:147
本文介绍了我想在我的班上名列前茅而不是在方法来设置我的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法来解决这个困惑的问题,我有很多很多的事情,我想补充我的类的顶部,以帮助减少杂波。

由于多种方式使用这些复选框变量。

我想直接在左括号下拥有一切的顶部。

下面是什么在起作用,但是的没有的是我想要的:

 公共类MyClass的延伸活动实现View.OnClickListener {

    //离开了大部分code一样的onCreate。只要pretend它的存在。

    公共无效checkboth(查看视图){

        复选框CB1 =(复选框)findViewById(R.id.cb1);
        复选框CB2 =(复选框)findViewById(R.id.cb2);

            cb1.setchecked(真正的);
            cb2.setchecked(真正的);

    }

    @覆盖
    公共无效的onClick(视图v){
    }
}
 

但对我的生活中,我想不通为什么我不能做到这一点:

 公共类MyClass的延伸活动实现View.OnClickListener {

复选框CB1 =(复选框)findViewById(R.id.cb1);
复选框CB2 =(复选框)findViewById(R.id.cb2);

    //离开了大部分code一样的onCreate。只要pretend它的存在。

    公共无效checkboth(查看视图){

        cb1.setchecked(真正的);
        cb2.setchecked(真正的);

    }

    @覆盖
    公共无效的onClick(视图v){
    }
}
 

解决方案

您无法初始化这样的方法查看,外面

 复选框CB1 =(复选框)findViewById(R.id.cb1);
复选框CB2 =(复选框)findViewById(R.id.cb2);
 

,因为这将意味着这些行之前的onCreate()这将导致这些变量是并抛出运行一个 NPE 当您试图调用这些变量的方法。这是因为你调用的setContentView(R.layout.your_layout)里面的的onCreate()和您的浏览活了布局内。这意味着,他们不能,直到你的布局初始化充气致电的setContentView ()。您必须调用的setContentView()试图初始化之前浏览 。有周围的部分没办法。

有些人这样做可以帮助是创建一个单独的功能,只是在的setContentView初始化这些变量()被调用。事情是这样的。

 公共类MyActivity
    //声明你的意见,使他们全球的类
    TextView的电视;
   //更多的意见
    @覆盖
    公共无效onCreat(东东)
    {
       //超级呼叫
       的setContentView(R.layout.my_layout);
       在里面();
 

然后在的init()函数初始化所有的浏览您已经声明

 私人无效的init()
{
     电视=(TextView中)findViewById(R.id.myTextView);
     //更多的初始化
}
 

但你可以初始化它们在的onCreate() onResume()或其它地方,只要它的< STRONG>在 的setContentView() 声明并初始化它们以这种方式将确保它们都提供给其他的功能,监听器,内部类,等... 活动的。如果你有很多的浏览这可能减少了混乱了一下。

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 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.

这篇关于我想在我的班上名列前茅而不是在方法来设置我的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆