Josh Beckwith
Josh Beckwith

Reputation: 1540

Android programming, a little issue.

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 code, on the very last parenthesis;

if (position == 3){ 
            Intent intent = new Intent(Main.this, Progress.class); 
            Main.this.startActivity(intent); } } } )

Here is my whole activity code;

package com.example.gymbuddy;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

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

    }

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

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

}

Upvotes: 0

Views: 141

Answers (3)

Hiral Vadodaria
Hiral Vadodaria

Reputation: 19250

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

Upvotes: 1

romedius
romedius

Reputation: 775

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/

Upvotes: 1

Hang Guan
Hang Guan

Reputation: 102

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

Upvotes: 0

Related Questions