findViewById没有重复

时间:2014-07-13 14:38:56

标签: android

我开始学习android,我有疑问。如果我从XML获取字段,我会这样做:

import android.widget.RatingBar;

public class TestActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        RatingBar rate = (RatingBar) findViewById(R.id.ratebar1);
        rate.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {

            public void onRatingChanged(RatingBar ratingBar, float rating,
                    boolean fromTouch) {
                ((TextView)findViewById(R.id.rating_text)).setText("Rating: "+ (int)rating);
            }
        });

        final Button test = (Button) findViewById(R.id.Button123);
        test.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                RatingBar rate = (RatingBar) findViewById(R.id.ratebar1);
                rate.setRating(2);
            }
        }
        );
        });
}

这工作正常,但为什么我不能做:

import android.widget.RatingBar;

public class TestActivity extends Activity {

    RatingBar rate = (RatingBar) findViewById(R.id.ratebar1); //here!!!

    protected void onCreate(Bundle savedInstanceState) {
        //remove!!
        rate.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {

            public void onRatingChanged(RatingBar ratingBar, float rating,
                    boolean fromTouch) {
                ((TextView)findViewById(R.id.rating_text)).setText("Rating: "+ (int)rating);
            }
        });

        final Button test = (Button) findViewById(R.id.Button123);
        test.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //remove!!
                rate.setRating(2);
            }
        }
        );
        });
}

或只是init实例的功能? 我该怎么做?

3 个答案:

答案 0 :(得分:1)

由于活动context而导致其在活动类中不可用,它在onCreate激活方法之后可用。

您可以在班级中定义RatingBar rate,然后使用onCreate()方法启动。 在使用setContentView(R.layout.activity_test);方法之前,还要在onCreate()方法中添加findViewById

并且还使用rating_text作为类成员,因为它在onRatingChanged函数中不可用。 所以使用像

这样的代码
import android.widget.RatingBar;

public class TestActivity extends Activity {
    private RatingBar rate;
    private  TextView rating_text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_test);
        rate = (RatingBar) findViewById(R.id.ratebar1);
        rating_text =  (TextView)findViewById(R.id.rating_text);
        rate.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {

            public void onRatingChanged(RatingBar ratingBar, float rating,
                    boolean fromTouch) {
                rating_text.setText("Rating: "+ (int)rating);
            }
        });

        final Button test = (Button) findViewById(R.id.Button123);
        test.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                rate.setRating(2);
            }
        }
        );
        });
    }

答案 1 :(得分:0)

您可能希望RatingBar是最终的,但在onCreate方法中是这样的:

protected void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.activity_test); // also this as noted by other answers
    final RatingBar rate = (RatingBar) findViewById(R.id.ratebar1); //here!!!
    rate.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {

        public void onRatingChanged(RatingBar ratingBar, float rating,
                boolean fromTouch) {
            ((TextView)findViewById(R.id.rating_text)).setText("Rating: "+ (int)rating);
        }
    });

    final Button test = (Button) findViewById(R.id.Button123);
    test.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            rate.setRating(2);
        }
    }
    );
    });
}

答案 2 :(得分:0)

import android.widget.RatingBar;

public class TestActivity extends Activity {

private RatingBar rate; 

protected void onCreate(Bundle savedInstanceState) {
    //Where is your setcontentview ???
    rate = (RatingBar) findViewById(R.id.ratebar1);
    rate.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {

        public void onRatingChanged(RatingBar ratingBar, float rating,
                boolean fromTouch) {
            ((TextView)findViewById(R.id.rating_text)).setText("Rating: "+ (int)rating);
        }
    });

    final Button test = (Button) findViewById(R.id.Button123);
    test.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            //remove!!
            rate.setRating(2);
        }
    }
    );
    });

}