TextView作为属性



我想使用我的TextView作为属性

private TextView up = (TextView) findViewById(R.id.tv_up);
private TextView mid = (TextView) findViewById(R.id.tv_mid);
private TextView down = (TextView) findViewById(R.id.tv_down);

而不是每次都定义它:

public void onButtonClickUp(View v) {
        TextView up = (TextView) findViewById(R.id.tv_up);
        TextView mid = (TextView) findViewById(R.id.tv_mid);
        TextView down = (TextView) findViewById(R.id.tv_down);
        <..>
}
public void onButtonClickDown(View v) {
        TextView up = (TextView) findViewById(R.id.tv_up);
        TextView mid = (TextView) findViewById(R.id.tv_mid);
        TextView down = (TextView) findViewById(R.id.tv_down);
        <..>
}

上面的方法是在我的android设备上生成一个force close error。如何解决这个问题,或者这通常是可能的吗?

谢谢!

public class TestActivity extends Activity {
    private TextView up;
    private TextView mid;
    private TextView down;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
     up = (TextView) findViewById(R.id.tv_up);
     mid = (TextView) findViewById(R.id.tv_mid);
     down = (TextView) findViewById(R.id.tv_down);
}

我不知道你的应用程序为什么会崩溃,但通过这样声明/分配你的小部件,你会省去很多麻烦。。。整个活动只有一次。

最新更新