Cannot resolve this symbol for an Object

Multi tool use
Multi tool use


Cannot resolve this symbol for an Object



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


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;
}




2 Answers
2



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.


RadioGroup group1


onCreate


onCreate()



Declare RadioGroup group1 as class member as follows:


RadioGroup group1


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().


RadioGroup


onCreate()



You are also missing call to show() method while trying to display Toast. Without calling show(), Toast message won't be displayed.


show()


Toast


show()



Instead of



Toast.makeText(this,"Score is : " + score,Toast.LENGTH_SHORT)


Toast.makeText(this,"Score is : " + score,Toast.LENGTH_SHORT)



Use



Toast.makeText(this,"Score is : " + score,Toast.LENGTH_SHORT).show()


Toast.makeText(this,"Score is : " + score,Toast.LENGTH_SHORT).show()





Thank you for giving that extra piece of advised I had totaly forgot about show() and hence I was using Log.v() to see the result , I kept wondering why its not working. Thanks
– Vedant Tripathi
Jun 30 at 14:43





@VedantTripathi no problem. Enjoy coding :-) Remember to mark answer as accepted if it was helpful
– Sagar
Jun 30 at 14:47



Declare Radio group button at class level. You are using veriable at function level so you can not use that variable out of the function.


private RadioGroup group1, group2, group3, group4, group5, group6, group7,group8,group9,group10;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
group1 = (RadioGroup)findViewById(R.id.que1_rG);
group2 = (RadioGroup)findViewById(R.id.que2rG);
group3 = (RadioGroup)findViewById(R.id.que3rG);
group4 = (RadioGroup)findViewById(R.id.que4rG);
group5 = (RadioGroup)findViewById(R.id.que5rG);
group6 = (RadioGroup)findViewById(R.id.que6rG);
group7 = (RadioGroup)findViewById(R.id.que7rG);
group8 = (RadioGroup)findViewById(R.id.que8rG);
group9 = (RadioGroup)findViewById(R.id.que9rG);
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;
}





Thanks, I am an Idiot after seeing the solution ,I went and wrote the code while I could've had just copied from you.
– Vedant Tripathi
Jun 30 at 16:36






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

yGjfmTfZEcmtJGNNGa9u KDgBBnol2 r1eQdHJZQni TZ6St0aLANxXX,MIK,Y,7
Kw35EDReEltuZPt,TV1,4yh8KwfqyHSyRdfleBQJUzhSb

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

django NoReverseMatch Exception

List of Kim Possible characters