Search code examples
javaandroidradio-buttonradio-group

i am trying to make rock paper scissors game in android , and there a error i can not solve , error is coming in java


I am making rock paper scissors app in android studio. I am getting the error in "answer1 = findViewById(R.id.answer1);". I have used dependency for git for radiogroup.

////////////////////////////////////////////////////////////////////////////////////////////// xml code

 <xyz.teamgravity.imageradiobutton.GravityRadioGroup
            android:id="@+id/answer1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <xyz.teamgravity.imageradiobutton.GravityImageRadioButton
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:padding="20dp"
                app:girbImage="@drawable/rock"
                app:girbPressedTextColor="@color/white"
                app:girbText="rock"
                app:girbUnpressedTextColor="?attr/colorPrimary"
                android:clickable="true"/>


            <xyz.teamgravity.imageradiobutton.GravityImageRadioButton
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:padding="20dp"
                app:girbImage="@drawable/paper"
                app:girbPressedTextColor="@color/white"
                app:girbText="paper"
                app:girbUnpressedTextColor="?attr/colorPrimary" />



            <xyz.teamgravity.imageradiobutton.GravityImageRadioButton
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:padding="20dp"
                app:girbImage="@drawable/scissors"
                app:girbPressedTextColor="@color/white"
                app:girbText="scissors"
                app:girbUnpressedTextColor="?attr/colorPrimary" />

        </xyz.teamgravity.imageradiobutton.GravityRadioGroup>

////////////////////////////////////////////////////////////////////////////////////////java code

public class MainActivity extends AppCompatActivity {

ImageView pc;
RadioGroup answer1;
TextView result;
Button enter;

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

    pc = findViewById(R.id.pc);
    result = findViewById(R.id.result);
    answer1 = findViewById(R.id.answer1);
    enter = findViewById(R.id.enter);




    enter.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            roll();

            int answer = answer1.getCheckedRadioButtonId();
            RadioButton radiobutton1 = findViewById(answer);
            String radio = radiobutton1.getText().toString();
            result.setText(""+radio);

        }

        private  void roll(){

            int num = (int)(Math.random() * 3 + 1);

            int answer = answer1.getCheckedRadioButtonId();
            RadioButton radiobutton1 = findViewById(answer);
            String radio = radiobutton1.getText().toString();
            result.setText(""+radio);




        }


    });

}

}

///////////////////////////////////////////////////////////////////////////////error

Caused by: java.lang.ClassCastException: xyz.teamgravity.imageradiobutton.GravityRadioGroup cannot be cast to android.widget.RadioGroup
    at com.example.rockpapersizer.MainActivity.onCreate(MainActivity.java:28)
    at android.app.Activity.performCreate(Activity.java:7893)
    at android.app.Activity.performCreate(Activity.java:7880)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)

Solution

  • The layout the has the id @+id/answer1 is not a RadioGroup it's GravityRadioGroup
    Does your class GravityRadioGroup extends RadioGroup?
    if not, you can solve this by:

    ImageView pc;
    /*RadioGroup answer1; --> */ GravityRadioGroup answer1;
    TextView result;
    Button enter;