On creating an object RadioGroup in OnCreate() , when later this object is called in another function it shows the error "cannot resolve this symbol"
Here is java code
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RadioGroup group1 = (RadioGroup)findViewById(R.id.que1_rG);
RadioGroup group2 = (RadioGroup)findViewById(R.id.que2rG);
RadioGroup group3 = (RadioGroup)findViewById(R.id.que3rG);
RadioGroup group4 = (RadioGroup)findViewById(R.id.que4rG);
RadioGroup group5 = (RadioGroup)findViewById(R.id.que5rG);
RadioGroup group6 = (RadioGroup)findViewById(R.id.que6rG);
RadioGroup group7 = (RadioGroup)findViewById(R.id.que7rG);
RadioGroup group8 = (RadioGroup)findViewById(R.id.que8rG);
RadioGroup group9 = (RadioGroup)findViewById(R.id.que9rG);
RadioGroup group10 = (RadioGroup)findViewById(R.id.que10rG);
}
public void submitButton(View view)
{
int checkedRadio1= group1.getCheckedRadioButtonId();
Toast.makeText(this,"Score is : " + score,Toast.LENGTH_SHORT);
Log.v("MainActivity","Score is " + score);
score = 0;
}
The reason you are getting cannot resolve symbol is because you haven't declared your RadioGroup group1
at class level. Since you have declared and assigned in onCreate
its scope is limited to onCreate()
only. To access in other method make it as class member.
Declare RadioGroup group1
as class member as follows:
public class MainAcivity extends Activity{
private RadioGroup group1;
//Declare other RadioGroup group2,group3...group 10. if you intend
//to access them outside of onCreate()
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
group1 = (RadioGroup)findViewById(R.id.que1_rG);
...
}
public void submitButton(View view) {
int checkedRadio1= group1.getCheckedRadioButtonId();
Toast.makeText(this,"Score is : " + score,Toast.LENGTH_SHORT).show();
Log.v("MainActivity","Score is " + score);
score = 0;
}
}
Note:
Same thing applies for any other RadioGroup
, which you would like to access outside of onCreate()
.
You are also missing call to show()
method while trying to display Toast
. Without calling show()
, Toast message won't be displayed.
Instead of
Toast.makeText(this,"Score is : " + score,Toast.LENGTH_SHORT)
Use
Toast.makeText(this,"Score is : " + score,Toast.LENGTH_SHORT).show()