Android编程,有点问题。

时间:2012-09-13 03:28:43

标签: java android

这次我在括号下得到了红色的波浪线,告诉我"插入" 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); } } } )

}

3 个答案:

答案 0 :(得分:1)

在我看来,onCreate的方法已经关闭到早期,spinner.setOnItemSelectedListener应该在该方法中以及定义的内部clsses中。 spinner.setOnItemSelectedListener只是在类的中间浮动,也许它应该在onCreate方法中。 (这可能是有效的语法,但我不确定这是否是你想要实现的东西)

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

答案 1 :(得分: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);  
        }
    }
}

答案 2 :(得分:0)

这是实际的代码吗?可以是类handleButton4的末尾附加的“@”字符吗?

class handleButton4 implements OnClickListener {
    public void onClick(View v) {
        Intent intent = new Intent(Main.this, Settings.class);
        startActivity(intent);  } }@ // <------- this