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

Issue

i am making rock paper sisser app in android studio, i am gating 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;

Answered By – Abdallah A. Odeh

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published