Android studio: variable intent might not have been initialized

  android
 public void onItemSelected(AdapterView<?> arg0, View view,
                                       int position, long row_id){
                final Intent intent;

                switch (position) {
                    case 1:
                        intent = new Intent(Eng_page1.this, Eng_page1.class);
                        break;
                    case 2:
                        intent = new Intent(Eng_page1.this, Eng_page2.class);
                        break;
                    case 3:
                        intent = new Intent(Eng_page1.this, Eng_page3.class);
                        break;
                    case 4:
                        intent = new Intent(Eng_page1.this, Eng_page4.class);
                        break;
                    case 5:
                        intent = new Intent(Eng_page1.this, Eng_page5.class);
                        break;

                }
                startActivity(intent);

            }

            public void onNothingSelected(AdapterView<?> arg0) {

            }

Here is my android studio code for a spinner to open another activity.
Although I declare the intent in the switch, the studio is still warning me that variable intent might not have been initialized. How can I solve it ? Thank You.

Source: Android Questions

LEAVE A COMMENT