RatingBar rb;
TextView rate;
SharedPreferences wm;
SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_puan);
rate = (TextView) findViewById(R.id.text);
((RatingBar) findViewById(R.id.ratingBar))
.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean b) {
final int numStars = ratingBar.getNumStars();
editor = wm.edit();
editor.putFloat("numStars", rating);
editor.commit();
int ratings = wm.getInt("numStars", 0);
rate.setText(rating + "/" + String.valueOf(ratings));
}
});
wm = PreferenceManager.getDefaultSharedPreferences(this);
float rating = wm.getFloat("numStars", 0f);
rb.setRating(rating);
}
我有这个代码,但是当我运行它时,我得到了#p; puan已停止"错误。我怎么解决这个问题?请多多帮助我。
答案 0 :(得分:1)
您没有初始化RatingBar rb
,但您在其上使用方法setRating
。这就是问题所在。添加到您的代码
rb = (RatingBar) findViewById(R.id.ratingBar);
它应该有效。
您也可以在setOnRatingBarChangeListener()
中使用稍后的rb。