当我尝试执行我的应用程序时,我在RatingBar
上获得了类强制转换异常。这是logcat
:
06-15 11:27:55.585: ERROR/AndroidRuntime(385): FATAL EXCEPTION: main
06-15 11:27:55.585: ERROR/AndroidRuntime(385): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.apps.niit.rating/com.apps.niit.rating.MovieRatingActivity}: java.lang.ClassCastException: android.widget.RatingBar
06-15 11:27:55.585: ERROR/AndroidRuntime(385): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-15 11:27:55.585: ERROR/AndroidRuntime(385): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-15 11:27:55.585: ERROR/AndroidRuntime(385): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-15 11:27:55.585: ERROR/AndroidRuntime(385): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-15 11:27:55.585: ERROR/AndroidRuntime(385): at android.os.Handler.dispatchMessage(Handler.java:99)
06-15 11:27:55.585: ERROR/AndroidRuntime(385): at android.os.Looper.loop(Looper.java:123)
06-15 11:27:55.585: ERROR/AndroidRuntime(385): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-15 11:27:55.585: ERROR/AndroidRuntime(385): at java.lang.reflect.Method.invokeNative(Native Method)
06-15 11:27:55.585: ERROR/AndroidRuntime(385): at java.lang.reflect.Method.invoke(Method.java:521)
06-15 11:27:55.585: ERROR/AndroidRuntime(385): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-15 11:27:55.585: ERROR/AndroidRuntime(385): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-15 11:27:55.585: ERROR/AndroidRuntime(385): at dalvik.system.NativeStart.main(Native Method)
06-15 11:27:55.585: ERROR/AndroidRuntime(385): Caused by: java.lang.ClassCastException: android.widget.RatingBar
06-15 11:27:55.585: ERROR/AndroidRuntime(385): at com.apps.niit.rating.MovieRatingActivity.onCreate(MovieRatingActivity.java:27)
06-15 11:27:55.585: ERROR/AndroidRuntime(385): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-15 11:27:55.585: ERROR/AndroidRuntime(385): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-15 11:27:55.585: ERROR/AndroidRuntime(385): ... 11 more
我的主要活动代码如下:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.Toast;
public class MovieRatingActivity extends Activity {
/** Called when the activity is first created. */
private EditText movieName;
private EditText userReview;
private EditText releaseYear;
private EditText movieDuration;
RatingBar yourRating;
private EditText movieStarring;
private EditText movieDirector;
private ImageView movieImage;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
movieName = (EditText)findViewById(R.id.editText2);
userReview = (EditText)findViewById(R.id.editText1);
releaseYear = (EditText)findViewById(R.id.editText3);
movieDuration = (EditText)findViewById(R.id.editText4);
yourRating = (RatingBar)findViewById(R.id.ratingBar1);
movieStarring = (EditText)findViewById(R.id.editText5);
movieDirector = (EditText)findViewById(R.id.editText6);
movieImage = (ImageView)findViewById(R.id.imageView1);
yourRating.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener(){
@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
// TODO Auto-generated method stub
yourRating.setRating(rating);
Toast.makeText(MovieRatingActivity.this, "rating:"+String.valueOf(rating),
Toast.LENGTH_LONG).show();
}});
movieImage.setImageResource(R.drawable.icon);
}
public void myCickHandler(View view){
Intent myIntent = new Intent(MovieRatingActivity.this, null);
Bundle myBundle = new Bundle();
myBundle.putString("key1", movieName.getText().toString());
myBundle.putString("key1", userReview.getText().toString());
myBundle.putString("key1", releaseYear.getText().toString());
myBundle.putString("key1", movieDuration.getText().toString());
myBundle.putDouble("key1", yourRating.getRating());
myBundle.putString("key1", movieStarring.getText().toString());
myBundle.putString("key1", movieDirector.getText().toString());
myIntent.putExtras(myBundle);
startActivity(myIntent);
}
}
布局文件标记如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical" android:gravity="top">
<EditText android:layout_height="wrap_content" android:layout_width="match_parent" android:hint="Movie name" android:id="@+id/editText2" android:text=""></EditText>
<LinearLayout android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="horizontal">
<LinearLayout android:id="@+id/linearLayout2" android:layout_height="match_parent" android:layout_width="wrap_content">
<ImageView android:src="@drawable/icon" android:id="@+id/imageView1" android:layout_width="match_parent" android:layout_height="match_parent"></ImageView>
</LinearLayout>
<LinearLayout android:id="@+id/linearLayout3" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1">
<LinearLayout android:id="@+id/linearLayout4" android:layout_height="match_parent" android:orientation="vertical" android:layout_width="match_parent">
<LinearLayout android:id="@+id/linearLayout6" android:layout_height="wrap_content" android:layout_width="match_parent">
<EditText android:text="" android:hint="Year" android:id="@+id/editText3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:inputType="number"></EditText>
<EditText android:layout_height="wrap_content" android:hint="Duration" android:layout_weight="1" android:layout_width="wrap_content" android:id="@+id/editText4" android:text=""></EditText>
</LinearLayout>
</LinearLayout>
<LinearLayout android:id="@+id/linearLayout5" android:layout_height="match_parent" android:layout_width="wrap_content" android:orientation="vertical">
<RatingBar android:id="@+id/ratingBar1" android:layout_width="wrap_content" android:layout_height="wrap_content"></RatingBar>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<EditText android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/editText1" android:text="" android:hint="Write review here" android:layout_weight="1" android:gravity="top"></EditText>
<EditText android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/editText5" android:text="" android:hint="Starring"></EditText>
<EditText android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/editText6" android:text="" android:hint="Director"></EditText>
<Button android:layout_height="wrap_content" android:text="Save" android:id="@+id/button1" android:layout_width="match_parent"></Button>
</LinearLayout>
我无法弄清楚问题,因为施法是正确的。