代码末尾的连续语法错误

时间:2016-03-07 05:16:19

标签: java eclipse syntax syntax-error

我不明白为什么在最后一个括号中提示语法错误。无论我如何重新排列代码,错误似乎都停留在代码的末尾并且不会消失。显示了两个错误。 1st:插入“;”完成FieldDeclaration。第二步:插入“}”以完成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("");
            }
            }

4 个答案:

答案 0 :(得分:0)

您正在定义内部类,并且缺少分号。我还建议@Override annotation`。像,

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

答案 1 :(得分:0)

出现错误消息是因为您缺少或有额外的支撑。尝试添加一个支架&#39;}&#39;在代码的最后,如果问题仍然存在,请尝试删除它和另一个支撑。支撑的作用类似于限制器和分隔符。如果没有正确给出,代码将面临编译错误。

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

答案 3 :(得分: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是一个局部变量声明语句。