无法解析方法内的符号[重复]

I am trying to get the Id of a button when it is clicked. I have implemented a switch to get the Id(stored as resourceName) which works as System.out.println works. However when I use resourceName to get my data, there is an error "cannot resolve symbol'.

public class NavigationActivity extends AppCompatActivity {


    ImageButton Apple;
    ImageButton Asus;
    FirebaseFirestore db = FirebaseFirestore.getInstance();
    private CollectionReference details = db.collection("devices");
    ArrayAdapter arrayAdapter;
    TextView device_name;
    TextView device_description;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...

    }




    public void deviceList (View V){

        Apple = (ImageButton) findViewById(R.id.Apple);
        Asus = (ImageButton) findViewById(R.id.Asus);
        final ArrayList<String> deviceNames;
        deviceNames = new ArrayList<>();
        final ArrayList<String> selectedDevice;
        selectedDevice = new ArrayList<>();




        int iddd = V.getId();
        switch (iddd){
            case R.id.Asus:

            case R.id.Apple:
                String resourceName = getResources().getResourceEntryName(iddd);
                System.out.println(resourceName);

                break;

        }



        db.collection("devices")
                .whereEqualTo("brand", resourceName)
                .get()
                ....
               });
    };
}
评论
一起疯了
一起疯了

The variable is defined in a very small scope within that switch case. If it needs a larger scope, define it outside of the switch:

String resourceName = "";
//...
switch (iddd){
    case R.id.Asus:

    case R.id.Apple:
        resourceName = getResources().getResourceEntryName(iddd);
        System.out.println(resourceName);

        break;
}
// resourceName will be available here

Of course, it's up to you to determine what would be a sensible default value or what to do with that default value, or any other way in which you want to handle the case where your switch never assigns the intended value to the variable.

点赞
评论