Solution 1 :
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;
Solution 2 :
Chnage RadioGroup answer1;
to GravityRadioGroup answer1;
.
Problem :
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)
Comments
Comment posted by ssojitra62
solution not working
Comment posted by Abdallah A. Odeh
What is the error? did it change?
Comment posted by ssojitra62
its worked but now i am not able to take input of radio group . its showing error in “int answer = answer1.getCheckedRadioButtonId();”