Android programming, a little issue.

前端 未结 3 1765
眼角桃花
眼角桃花 2021-01-26 06:58

I\'m getting the red squiggle under a parenthesis this time, telling me \"insert \"EnumBody\" to complete EnumDeclaration\"

It is happening on the last line of this cod

相关标签:
3条回答
  • 2021-01-26 07:18

    Replace your activity code with these lines of code and try compiling again:

    public class Main extends Activity {    
        @Override
        public void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.activity_main);
    
               findViewById(R.id.button2).setOnClickListener(new handleButton2());
               findViewById(R.id.button3).setOnClickListener(new handleButton3());
               findViewById(R.id.button4).setOnClickListener(new handleButton4());
    
               Spinner spinner = (Spinner) findViewById(R.id.spinner);
               ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                    this, R.array.work_array, android.R.layout.simple_spinner_item);
               adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
               spinner.setAdapter(adapter);
    
               spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
                    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                        if (position == 1){
                            Intent intent = new Intent(Main.this, Upperbody.class);
                            Main.this.startActivity(intent); 
                        }
                        if (position == 2){
                            Intent intent = new Intent(Main.this, Settings.class);
                            Main.this.startActivity(intent);
                        }
                        if (position == 3){
                            Intent intent = new Intent(Main.this, Progress.class);
                            Main.this.startActivity(intent); 
                        } 
                   } 
                } );
        }        
        class handleButton2 implements OnClickListener {
            public void onClick(View v) {
                Intent intent = new Intent(Main.this, Benchmark.class);
                startActivity(intent); 
            }
        }        
        class handleButton3 implements OnClickListener {
            public void onClick(View v) {
                Intent intent = new Intent(Main.this, Progress.class);
                startActivity(intent);  
            } 
        }        
        class handleButton4 implements OnClickListener {
            public void onClick(View v) {
                Intent intent = new Intent(Main.this, Settings.class);
                startActivity(intent);  
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-26 07:20

    Is this the actual code? Could it be the additional "@" character at the end of class handleButton4?

    class handleButton4 implements OnClickListener {
        public void onClick(View v) {
            Intent intent = new Intent(Main.this, Settings.class);
            startActivity(intent);  } }@ // <------- this
    
    0 讨论(0)
  • 2021-01-26 07:31

    It seems to me that the method onCreate has been closed to early, the spinner.setOnItemSelectedListener should be in that and the defined inner clsses outside of the method. The spinner.setOnItemSelectedListener just floats around in the middle of the class, maybe it should be in the onCreate method. (which might be valid syntax, but I'm not sure if that's the thing what you want to achieve)

    The statement might be handled like a initialization Block in Java, even without parenthesis. (but I'm not 100% sure) See: http://jpz-log.info/archives/2009/03/25/initialization-blocks-in-java/

    0 讨论(0)
提交回复
热议问题