getResources作为全局变量

时间:2014-08-14 19:31:29

标签: java android oop

我只是活动:

public class TestActivity extends MainActivity {    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.test);

        final Button click = (Button) findViewById(R.id.ButtonSave);

        click.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Resources resources = getResources();
                String text = resources.getString(R.string.test);
                Toast.makeText(TestActivity.this, text, Toast.LENGTH_SHORT).show();
            }
        });

        final Button click2 = (Button) findViewById(R.id.ButtonSave);

        click2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Resources resources = getResources();
                String text = resources.getString(R.string.test2);
                Toast.makeText(TestActivity.this, text, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

这很好用,但我正在学习Android,我想修改它。我想使用getResources作为全局变量:

public class TestActivity extends MainActivity {    

    Resources resources = getResources(); //ADD HERE!

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.test);

        final Button click = (Button) findViewById(R.id.ButtonSave);

        click.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //REMOVE!
                String text = resources.getString(R.string.test);
                Toast.makeText(TestActivity.this, text, Toast.LENGTH_SHORT).show();
            }
        });

        final Button click2 = (Button) findViewById(R.id.ButtonSave);

        click2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //REMOVE!
                String text = resources.getString(R.string.test2);
                Toast.makeText(TestActivity.this, text, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

但这会返回MAIN EXCEPTION。为什么?我该怎么做?

3 个答案:

答案 0 :(得分:2)

没有必要将它作为一个全局实例,它已经与活动有关,您可以通过调用其名称直接从活动中获取其实例。

<强>样品:

click.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            String text = TestActivity.this.getResources().getString(R.string.test);
            Toast.makeText(TestActivity.this, text, Toast.LENGTH_SHORT).show();
        }
    });

无需将其分配给getResource将返回相同实例的变量。因此,将它声明为全局变量是有意义的。

第二段代码失败,因为您在活动的getResources()之前直接获得了onCreate(),从而捕获了异常。

答案 1 :(得分:1)

无需使用TestActivity.this代替使用getActivity()getResource().getString()也将替换为getString()

btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String text = getString(R.string.test);
                Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show();
            }
});

答案 2 :(得分:-1)

public class TestActivity extends MainActivity {    

    ***Resources resources;***

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.test);
        ***resources = getResources();***

        final Button click = (Button) findViewById(R.id.ButtonSave);

        click.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String text = resources.getString(R.string.test);
                Toast.makeText(TestActivity.this, text, Toast.LENGTH_SHORT).show();
            }
        });

        final Button click2 = (Button) findViewById(R.id.ButtonSave);

        click2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String text = resources.getString(R.string.test2);
                Toast.makeText(TestActivity.this, text, Toast.LENGTH_SHORT).show();
            }
        });
    }
}