簡體   English   中英

TextView作為屬性?

[英]TextView as attribute?

我想使用我的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);

}

我不確定為什么您的應用會崩潰,但是通過這樣聲明/分配小部件,您將避免很多麻煩……整個活動只有一次。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM