1

我试图在文本视图中显示用户的评分,而我得到的只是“进程意外终止”错误。一共有三个活动。包含评分栏的活动的 XML 编码是

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".RatingActivity" >

 <TextView
     android:id="@+id/textView3"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_centerHorizontal="true"
     android:layout_centerVertical="true"
     android:hint="@string/hello_world"
     android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

java编码是:

package com.example.myfirstapp;

import android.app.Activity;

import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.TextView;

public class RatingActivity extends Activity 
{

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_rating);
    RatingBar ratingBar1=(RatingBar)findViewById(R.id.ratingBar1);
    TextView textView3 =(TextView) findViewById(R.id.textView3);
                textView3.setTextSize(40);
                textView3.setText("Rating is :"+ratingBar1.getRating());

}

}

我究竟做错了什么?我是android应用程序开发的新手。请帮助,提前谢谢。

4

3 回答 3

1
RatingBar mBar = (RatingBar) findViewById(R.id.mRatingBar);
mBar.setOnClickListener(onclickbutton1);

public void onClick(View v) {

    RatingBar bar = (RatingBar) v;
    statusMessage.setText("value is " + bar.getRating());
}
于 2012-11-22T12:36:55.643 回答
0

ratingBar1您的布局文件中没有带有 id 的视图。

于 2012-11-22T12:31:00.997 回答
0

考虑使用setOnRatingBarChangeListener以获得更好的性能:

    myRatingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
        @Override
        public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
            myTextView.setText(String.format("%2.1f", rating));
        }
    });
于 2016-04-05T16:23:39.710 回答