0

这次我得到了括号下的红色波浪线,告诉我“插入“EnumBody”以完成 EnumDeclaration”

它发生在这段代码的最后一行,最后一个括号上;

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

这是我的整个活动代码;

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

}
4

3 回答 3

1

在我看来,onCreate 方法已经关闭到早期,spinner.setOnItemSelectedListener 应该在其中,并且在方法之外定义了内部类。spinner.setOnItemSelectedListener 只是漂浮在类的中间,也许它应该在 onCreate 方法中。(这可能是有效的语法,但我不确定这是否是您想要实现的目标)

该语句可以像 Java 中的初始化块一样处理,即使没有括号。(但我不是 100% 确定)请参阅:http: //jpz-log.info/archives/2009/03/25/initialization-blocks-in-java/

于 2012-09-13T04:04:24.863 回答
1

用这些代码行替换您的活动代码,然后再次尝试编译:

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);  
        }
    }
}
于 2012-09-13T04:52:41.257 回答
0

这是实际的代码吗?可能是句柄Button4 类末尾的附加“@”字符吗?

class handleButton4 implements OnClickListener {
    public void onClick(View v) {
        Intent intent = new Intent(Main.this, Settings.class);
        startActivity(intent);  } }@ // <------- this
于 2012-09-13T03:49:51.267 回答