Prateek Sharma
Prateek Sharma

Reputation: 109

Getting error in Radio button on click Android

I'm creating a form and using radio button for a form. When I click on the radio button app got crash. There is some error in radio button on click method but i'm not getting it.

Here is my code: Class:

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;



public class LoginScreen2 extends Activity{

    SharedPreferences sharedPreferences;
    SharedPreferences.Editor edit;
    EditText comment;
    EditText placeE;
    EditText phoneNumber;

    Spinner employmentType;
    Intent in;
    boolean flag = false;

    String[] employment={"Professor","Enjineer","Others"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_screen2);

        sharedPreferences = getSharedPreferences("userLoginDetails", MODE_PRIVATE);
        edit= sharedPreferences.edit();
        comment= (EditText) findViewById(R.id.comment);
        placeE= (EditText) findViewById(R.id.placeE);
        phoneNumber= (EditText) findViewById(R.id.phoneNumber);

    }



    public void onItemSelected(AdapterView<?> parent, View view, int position,long id) {
        employmentType.setSelection(position);
        String selState = (String) employmentType.getSelectedItem();
    }

    @Override
    protected void onResume() {

        comment.setText(sharedPreferences.getString("com.example.prateeksharma.comment",""));

        phoneNumber.setText(sharedPreferences.getString("com.example.prateeksharma.phoneNumber",""));

        placeE.setText(sharedPreferences.getString("com.example.prateeksharma.placeE",""));

        if(sharedPreferences.getString("com.example.prateeksharma.jobType","") == "Officer"){
            RadioButton radioButton = (RadioButton)findViewById(R.id.radioOfficer);
            radioButton.setText("Officer");
        }

        else if(sharedPreferences.getString("com.example.prateeksharma.jobType","") == "Sales"){
            RadioButton radioButton = (RadioButton)findViewById(R.id.radioSales);
            radioButton.setText("Sales");
        }

        else if(sharedPreferences.getString("com.example.prateeksharma.jobType","") == "Business Planner"){
            RadioButton radioButton = (RadioButton)findViewById(R.id.radioBusinessPlanner);
            radioButton.setText("Business Planner");
        }        else if(sharedPreferences.getString("com.example.prateeksharma.jobType","") == "Enjineer"){
            RadioButton radioButton = (RadioButton)findViewById(R.id.radioEnjineer);
            radioButton.setText("Enjineer");
        }

        else if(sharedPreferences.getString("com.example.prateeksharma.jobType","") == "Others"){
            RadioButton radioButton = (RadioButton)findViewById(R.id.radioOthers);
            radioButton.setText("Others");
        }
        super.onResume();


    }

    public boolean isFullWidth(String s){
        boolean bool = false;
        int length = s.length();

        for (int i = 0; i < length; i++) {
            char c = s.charAt(i);
            if ('\uFF01' <= c && c <= '\uFF60' || '\uFFE0' <= c && c <= '\uFFE6') {
                continue;
            }
            bool = true;
            break;
        }
        return bool;

    }

    public boolean isHalfWidth(String s){
        boolean bool = false;
        int length = s.length();

        for (int i = 0; i < length; i++) {
            char c = s.charAt(i);
            if ('\u0000' <= c && c <= '\u00FF' || '\uFF61' <= c && c <= '\uFFDC' || '\uFFE8' <= c && c <= '\uFFEE') {
                continue;
            }
            bool = true;
            break;
        }
        return bool;

    }

    public void register(View view) {
        RadioButton radioButton = (RadioButton)findViewById(R.id.radioOthers);

        String s = placeE.getText().toString();
        if(s == null){
            placeE.requestFocus();
            placeE.setError("Place of Employment is required");
            flag =true;
        }
        /*if(isFullWidth(s)){
            placeE.requestFocus();
            placeE.setError("Characters must be Full-Width Characters");
            flag =true;
        }*/

        s = phoneNumber.getText().toString();
        if(s == null){
            phoneNumber.requestFocus();
            phoneNumber.setError("Phone Number is required");
            flag =true;
        }
        if(s.length() != 10){
            phoneNumber.requestFocus();
            phoneNumber.setError("Phone Number should be 10 digits only");
            flag =true;
        }
        if(isHalfWidth(s)){
            phoneNumber.requestFocus();
            phoneNumber.setError("Characters must be Half-Width Characters");
            flag =true;
        }
        if(sharedPreferences.getString("com.example.prateeksharma.jobType","") == null){
            radioButton.requestFocus();
            radioButton.setError("Characters must be Full-Width Characters");
            flag =true;
        }

        edit.putString("com.example.prateeksharma.comment", comment.getText().toString());
        edit.putString("com.example.prateeksharma.phoneNumber", phoneNumber.getText().toString());
        edit.putString("com.example.prateeksharma.placeE", placeE.getText().toString());
        edit.apply();
        if(flag) {
            flag =false;
        }
        else {
            Intent in = new Intent(this, Confirm.class);
            startActivity(in);
        }
    }

    public void onRadioButtonClicked(View view) {
        // Is the view now checked?
        boolean checked = ((CheckBox) view).isChecked();

        // Check which checkbox was clicked
        switch(view.getId()) {
            case R.id.radioOfficer:
                if (checked)
                    edit.putString("com.example.prateeksharma.jobType","Officer");
                break;
            case R.id.radioSales:
                if (checked)
                    edit.putString("com.example.prateeksharma.jobType","Sales");
                break;
            case R.id.radioBusinessPlanner:
                if (checked)
                    edit.putString("com.example.prateeksharma.jobType","Business Planner");
                break;

            case R.id.radioEnjineer:
                if (checked)
                    edit.putString("com.example.prateeksharma.jobType","Enjineer");
                break;

            case R.id.radioOthers:
                if (checked)
                    edit.putString("com.example.prateeksharma.jobType","Others");

            }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_next_login_page, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Activity File:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ScrollView02"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scrollbars="vertical">
    <LinearLayout
        android:layout_width="fill_parent"
        android:orientation="vertical"
        android:layout_height="fill_parent">

        <!--Put form controls here-->

        <TextView
            android:id="@id/TextViewTitle"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="@string/FromTitle"
            android:textSize="20pt"
            android:gravity="center">
        </TextView>

        <View
            android:layout_width="fill_parent"
            android:layout_height="30dp">
        </View>


        <TextView
            android:id="@+id/pEmp"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="@string/placeofEmploymnet"
            android:textSize="10pt">
        </TextView>

        <EditText
            android:id="@+id/placeE"
            android:layout_height="wrap_content"
            android:maxLength="30"
            android:layout_width="fill_parent">
        </EditText>

        <View
            android:layout_width="fill_parent"
            android:layout_height="30dp">
        </View>


        <TextView
            android:id="@+id/pNumber"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="@string/phoneNumber"
            android:textSize="10pt">
        </TextView>

        <EditText
            android:id="@+id/phoneNumber"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:layout_width="fill_parent">
        </EditText>

        <View
            android:layout_width="fill_parent"
            android:layout_height="30dp">
        </View>


        <TextView
            android:id="@+id/jobType"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="@string/jobType"
            android:textSize="10pt">
        </TextView>

        <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <RadioButton android:id="@+id/radioOfficer"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/radioOfficer"
                android:onClick="onRadioButtonClicked"/>
            <RadioButton android:id="@+id/radioSales"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/radioSales"
                android:onClick="onRadioButtonClicked"/>
            <RadioButton android:id="@+id/radioBusinessPlanner"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/businessPlanner"
                android:onClick="onRadioButtonClicked"/>
            <RadioButton android:id="@+id/radioEnjineer"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/enjineer"
                android:onClick="onRadioButtonClicked"/>
            <RadioButton android:id="@+id/radioOthers"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/others"
                android:onClick="onRadioButtonClicked"/>
        </RadioGroup>

        <View
            android:layout_width="fill_parent"
            android:layout_height="30dp">
        </View>


        <TextView
            android:id="@+id/cmt"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="@string/enterComment"
            android:textSize="10pt">
        </TextView>


        <EditText
            android:id="@+id/comment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:ems="10"
            android:maxLength="99"
            android:gravity="left|top"
            android:inputType="textMultiLine" >

            <requestFocus />
        </EditText>

        <View
            android:layout_width="fill_parent"
            android:layout_height="30dp">
        </View>


        <Button
            android:id="@+id/Register"
            android:layout_height="wrap_content"
            android:text="@string/register"
            android:onClick="register"
            android:layout_width="fill_parent">
        </Button>

    </LinearLayout>
</ScrollView>

Any help!!!

Upvotes: 0

Views: 559

Answers (2)

Yeehaw
Yeehaw

Reputation: 105

Try to reference your activity class name in the .xml file, like this

<ScrollView
    ...
    tools:context=".YourActivity" >

You might need to keep this consistent for the compiler to realize it needs to reference the method from YourActivity.java.

Upvotes: 0

samgak
samgak

Reputation: 24417

You are trying to cast a RadioButton to a Checkbox, which will give you a ClassCastException error. Change your code to this:

// Is the view now checked?
boolean checked = ((RadioButton) view).isChecked();

Upvotes: 1

Related Questions