Android ClassCastException android.widget.TextView无法强制转换为android.widget.Spinner

时间:2014-07-08 06:04:46

标签: android radio-button spinner radio-group

我是Android新手并制作一个应用程序,我需要使用微调器和无线电组按钮值进行数学计算。我尝试了不同的方法,谷歌这个问题,但没有成功。请有人帮助我。感谢。

Logcat错误:ClassCastException:android.widget.TextView无法强制转换为android.widget.Spinner

这是我的代码:

main.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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Gender"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_marginTop="56dp"
    android:text="Height"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<RadioGroup
    android:id="@+id/radiogender"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true" >

    <RadioButton
        android:id="@+id/rmale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="Male" />

    <RadioButton
        android:id="@+id/rfemale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Female" />
</RadioGroup>

<Spinner
    android:id="@+id/sp_feet"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button1"
    android:layout_marginBottom="22dp"
    android:layout_toLeftOf="@+id/gender" 
    android:entries="@array/feet_values"
    android:prompt="@string/feet" />

<Spinner
    android:id="@+id/sp_inch"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/result"
    android:layout_alignTop="@+id/sp_feet"
    android:entries="@array/inch_values"
    android:prompt="@string/inch" />

<Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/gender"
    android:layout_centerVertical="true"
    android:text="Calculate" />

<TextView
    android:id="@+id/result"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="36dp"
    android:textAppearance="?android:attr/textAppearanceMedium" />

MainActivity.java

 package com.example.wc;

 import android.app.Activity;
 import android.os.Bundle;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.Button;
 import android.widget.RadioButton;
 import android.widget.RadioGroup;
 import android.widget.Spinner;
 import android.widget.TextView;
 import android.widget.Toast;

 public class MainActivity extends Activity {
 private RadioGroup radiogendergroup;
 private RadioButton radiogenderbutton;
 private Spinner feet;
 private Spinner inch;
 private TextView res;
 private Button calculate;

 double cal;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    radiogendergroup = (RadioGroup) findViewById(R.id.radiogender);
    feet= (Spinner) findViewById(R.id.sp_feet);
    inch= (Spinner)findViewById(R.id.sp_inch);

    calculate = (Button) findViewById(R.id.button1);
    res= (TextView) findViewById(R.id.result);

    calculate.setOnClickListener(new View.OnClickListener() {
         public void onClick(View view) {
             int selectedId = radiogendergroup.getCheckedRadioButtonId();
             radiogenderbutton = (RadioButton) findViewById(selectedId);
             String selectedValue = radiogenderbutton.getText().toString();
             double f = Double.parseDouble(feet.getSelectedItem().toString());
             double i = Double.parseDouble(inch.getSelectedItem().toString());

             if(selectedValue=="Male" && f==5){

                 cal= 50 + 2.3*i;
                    String result = new Double(cal).toString();
                    res.setText(result);
             }

             if(selectedValue=="Male" && f==6){

                 cal= 77.6 + 2.3*i;
                    String result = new Double(cal).toString();
                    res.setText(result);
             }
             if(selectedValue=="Female" && f==5){

                 cal= 45.5 + 2.3*i;
                    String result = new Double(cal).toString();
                    res.setText(result);
             }               
         }
       });
}

}

修改后的LogCat错误:

 07-08 12:05:09.171: E/AndroidRuntime(1213): FATAL EXCEPTION: main
 07-08 12:05:09.171: E/AndroidRuntime(1213): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.wc/com.example.wc.MainActivity}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Spinner
 07-08 12:05:09.171: E/AndroidRuntime(1213):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
 07-08 12:05:09.171: E/AndroidRuntime(1213):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
 07-08 12:05:09.171: E/AndroidRuntime(1213):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
 07-08 12:05:09.171: E/AndroidRuntime(1213):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
 07-08 12:05:09.171: E/AndroidRuntime(1213):    at android.os.Handler.dispatchMessage(Handler.java:99)
 07-08 12:05:09.171: E/AndroidRuntime(1213):    at android.os.Looper.loop(Looper.java:137)
 07-08 12:05:09.171: E/AndroidRuntime(1213):    at android.app.ActivityThread.main(ActivityThread.java:4424)
 07-08 12:05:09.171: E/AndroidRuntime(1213):    at java.lang.reflect.Method.invokeNative(Native Method)
 07-08 12:05:09.171: E/AndroidRuntime(1213):    at java.lang.reflect.Method.invoke(Method.java:511)
 07-08 12:05:09.171: E/AndroidRuntime(1213):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
 07-08 12:05:09.171: E/AndroidRuntime(1213):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
 07-08 12:05:09.171: E/AndroidRuntime(1213):    at dalvik.system.NativeStart.main(Native Method)
 07-08 12:05:09.171: E/AndroidRuntime(1213): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Spinner
 07-08 12:05:09.171: E/AndroidRuntime(1213):    at com.example.wc.MainActivity.onCreate(MainActivity.java:30)
 07-08 12:05:09.171: E/AndroidRuntime(1213):    at android.app.Activity.performCreate(Activity.java:4465)
 07-08 12:05:09.171: E/AndroidRuntime(1213):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
 07-08 12:05:09.171: E/AndroidRuntime(1213):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
 07-08 12:05:09.171: E/AndroidRuntime(1213):    ... 11 more

2 个答案:

答案 0 :(得分:0)

首先将所有selectedValue=="Male"更改为

selectedValue.equals("Male")

==总是只比较两个引用(对于非基元,即),即它测试两个操作数是否引用同一个对象。

字符串您应该使用.equals("")

答案 1 :(得分:0)

没有Logcat错误:java.lang.String类型的值No无法转换为JSONObject

相比之下,您的问题似乎正确,因此使用.equals()代替==进行字符串比较 即在您的代码中,更改

selectedValue=="Male"

selectedValue.equals("Male")

selectedValue=="Female"

selectedValue.equals("Female")

==测试参考相等性。

.equals()测试价值平等。

有关详细信息,请参阅How do I compare strings in Java?

修改

请清理您的项目,因为您可能已经为TextView,R.java构建了一个id,但是稍后您将Spinner指定了相同的名称而不构建R.java。< / p>

由于R.java保留了所有控件的引用,所以在第一次分配给Textview时,所以R.java认为这个id是Textview,但现在你将它分配给Spinner并且R.java完全没有意识到你清理和建立。