语法错误,插入“;”完成声明

时间:2012-02-06 00:23:12

标签: android

这是我通过Eclipse在Android中编写的小应用程序的.java文件,我有一个小错误或语法故障。 在星号标记的末尾括号中,eclipse报告错误'语法错误,插入“;”完成声明'。我搜索了代码,发现没有任何不明或不合适的地方。有人可以请确认或告诉我如何解决这个问题?如果您需要其他文件,请在评论中告诉我。提前致谢 :)!

package org.example.knittingframe;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class KnittingFrame extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final TextView textview = (TextView)findViewById(R.id.textview); 
        final EditText op1 = (EditText)findViewById(R.id.NumBox1);
        final EditText op2 = (EditText)findViewById(R.id.NumBox2);
        final Button btnAdd = (Button)findViewById(R.id.addBox);

        btnAdd.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        int a, b;
        a = Integer.parseInt(op1.getText().toString());
        b = Integer.parseInt(op2.getText().toString());
        int sum = a + b;
        textview.setText(String.valueOf(sum));
        }
    **}** // Here is where the error occurs
    }
 }

5 个答案:

答案 0 :(得分:1)

编译器是对的。你需要替换

**}** // Here is where the error occurs

});

完成btnAdd.setOnClickListener方法调用。

答案 1 :(得分:1)

这是onCreate(xx)及其匿名类中的一个语句,因为匿名类是必须终止语句的语句:

btnAdd.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        int a, b;
        a = Integer.parseInt(op1.getText().toString());
        b = Integer.parseInt(op2.getText().toString());
        int sum = a + b;
        textview.setText(String.valueOf(sum));
        }
    **}** // Here is where the error occurs

TO:

btnAdd.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        int a, b;
        a = Integer.parseInt(op1.getText().toString());
        b = Integer.parseInt(op2.getText().toString());
        int sum = a + b;
        textview.setText(String.valueOf(sum));
        }
    });

答案 2 :(得分:0)

由于您使用的是匿名内部类,因此需要使用“);”结束setOnClickListener方法调用。如下:

public class KnittingFrame extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final TextView textview = (TextView)findViewById(R.id.textview); 
        final EditText op1 = (EditText)findViewById(R.id.NumBox1);
        final EditText op2 = (EditText)findViewById(R.id.NumBox2);
        final Button btnAdd = (Button)findViewById(R.id.addBox);

        btnAdd.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                int a, b;
                a = Integer.parseInt(op1.getText().toString());
                b = Integer.parseInt(op2.getText().toString());
                int sum = a + b;
                textview.setText(String.valueOf(sum));
                }
        });
    }
 }

答案 3 :(得分:0)

您开始了一个函数调用:setOnClickListener()。你需要完成它。像这样:

btnAdd.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        int a, b;
        a = Integer.parseInt(op1.getText().toString());
        b = Integer.parseInt(op2.getText().toString());
        int sum = a + b;
        textview.setText(String.valueOf(sum));
    }
});

答案 4 :(得分:0)

如果你缩进正确,也许你会看到它,但你错过了一个额外的括号和分号:

package org.example.knittingframe;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class KnittingFrame extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final TextView textview = (TextView)findViewById(R.id.textview); 
        final EditText op1 = (EditText)findViewById(R.id.NumBox1);
        final EditText op2 = (EditText)findViewById(R.id.NumBox2);
        final Button btnAdd = (Button)findViewById(R.id.addBox);

        btnAdd.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                int a, b;
                a = Integer.parseInt(op1.getText().toString());
                b = Integer.parseInt(op2.getText().toString());
                int sum = a + b;
                textview.setText(String.valueOf(sum));
            }
        });
    }
}