I'm new to android and making an application in which i need to use spinners and radio group button values for mathematical calculation .I have tried different ways and also Google this issue but didn't succeed . Please some one help me in this. Thanks.
Logcat Error: ClassCastException: android.widget.TextView cannot be cast to android.widget.Spinner
Here is my code:
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 Error after modification:
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