代码末尾连续出现语法错误

问题描述 投票:0回答:5

不明白为什么最后一个括号提示语法错误。无论我如何重新排列代码,错误似乎都停留在代码末尾并且不会消失。显示两个错误。第一:插入“;”完成字段声明。第二:插入“}”来完成ClassBody。如果有人可以帮我弄清楚吗?谢谢!
`

public class AKMainActivity extends Activity {

    private static final String ACTIVITIES = "activities";
    private Spinner spinner1;
    private EditText ETinfo;
    private SharedPreferences savedActivities;
    private ArrayList<String> details;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_akmain);

        Spinner dropdown = (Spinner)findViewById(R.id.spinner1);
        String[] items = new String[]{"Walking", "Running", "Stairs", "Generic"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items);
        dropdown.setAdapter(adapter);
        dropdown.setPrompt("Select Activity");

        ETinfo = (EditText)findViewById(R.id.ETinfo);

        savedActivities = getSharedPreferences(ACTIVITIES, MODE_PRIVATE);

        details = new ArrayList<String>(savedActivities.getAll().keySet());
        Collections.sort(details, String.CASE_INSENSITIVE_ORDER);

        public OnClickListener saveButtonListener = new OnClickListener(){

            public void onClick(View v){

                addDetailedActivity(ETinfo.getText().toString());
                ETinfo.setText("");
            }
            }
java eclipse syntax syntax-error
5个回答
0
投票

您正在定义一个内部类,并且缺少一个分号。我还推荐

@Override
注释`。比如,

public OnClickListener saveButtonListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        addDetailedActivity(ETinfo.getText().toString());
        ETinfo.setText("");
    }
}; // <-- to close OnClickListener

0
投票

出现错误消息是因为您缺少或有多余的支架。尝试在代码末尾添加一个大括号“}”,如果问题仍然存在,请尝试删除它并再删除一个大括号。大括号的作用类似于代码的限制符和分隔符。如果没有正确给出,代码将面临编译错误。


0
投票

代码末尾有类似以下内容:

public OnClickListener saveButtonListener = new OnClickListener(){

            public void onClick(View v){

                addDetailedActivity(ETinfo.getText().toString());
                ETinfo.setText("");
            }
            }

这称为匿名类,实际上是另一个必须是的声明

以分号结束,

public OnClickListener saveButtonListener = new OnClickListener(){

            public void onClick(View v){

                addDetailedActivity(ETinfo.getText().toString());
                ETinfo.setText("");
            }
            };

0
投票
public class a extends Activity {

    private static final String ACTIVITIES = "activities";
    private Spinner spinner1;
    private EditText ETinfo;
    private SharedPreferences savedActivities;
    private ArrayList<String> details;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_akmain);

        Spinner dropdown = (Spinner)findViewById(R.id.spinner1);
        String[] items = new String[]{"Walking", "Running", "Stairs", "Generic"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items);
        dropdown.setAdapter(adapter);
        dropdown.setPrompt("Select Activity");

        ETinfo = (EditText)findViewById(R.id.ETinfo);

        savedActivities = getSharedPreferences(ACTIVITIES, MODE_PRIVATE);

        details = new ArrayList<String>(savedActivities.getAll().keySet());
        Collections.sort(details, String.CASE_INSENSITIVE_ORDER);

        public OnClickListener saveButtonListener = new OnClickListener(){

            public void onClick(View v){

                addDetailedActivity(ETinfo.getText().toString());
                ETinfo.setText("");
            }
        };

    }
    }

试试这个。你必须插入一个分号,因为 onClicklistener 是一个局部变量声明语句。


0
投票

public OnClickListener saveButtonListener = new OnClickListener(){

        public void onClick(View v){

            addDetailedActivity(ETinfo.getText().toString());
            ETinfo.setText("");
        }
        }
© www.soinside.com 2019 - 2024. All rights reserved.