android中java代码中的语法错误[关闭]

Posted

技术标签:

【中文标题】android中java代码中的语法错误[关闭]【英文标题】:syntax mistakes in java code in android [closed] 【发布时间】:2018-12-17 10:29:15 【问题描述】:

我对编码很陌生,开始学习 android 。 我在 java 代码中遇到了一些小的语法错误(我的 activity.java) .请帮助我。 程序是将两个数相加并显示结果

package com.example.apple.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity 

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

    EditText firstNumber;
    EditText secondNumber;
    TextView addResult;
    Button btnAdd;

    double num1,num2,sum;

    firstNumber = findViewById(R.id.txtNumber1);
    secondNumber = findViewById(R.id.txtNumber2);
    addResult = findViewById(R.id.txtResult);
    btnAdd = findViewById(R.id.btnAdd);



 btnAdd.setOnClickListener(new OnClickListener() 

 public void onClick(View v) 
    num1 = Double.parseDouble(firstNumber.getText().toString());
    num2 = Double.parseDouble(secondNumber.getText().toString());
    sum = num1 + num2;
    addResult.setText(Double.toString(sum));
    
    );

【问题讨论】:

你至少得告诉我们错误是什么。 btnAdd.setOnClickListener ... 应该在一个方法中,可能在onCreate 【参考方案1】:

就像@NickL 提到的那样,您的点击事件应该在一个方法中。你可以像这样直接把它放在你的onCreate 中:

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

    Button clickButton = (Button) findViewById(R.id.clickButton);
    clickButton.setOnClickListener( new OnClickListener()  
        @Override 
        public void onClick(View v)  
        //Do what you want with the click here
         
    );


或者您可以像这样在onCreate 中调用您的方法:

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

    myCoolButtonMethod();



private void myCoolButtonMethod()

    Button clickButton = (Button) findViewById(R.id.clickButton);
    clickButton.setOnClickListener( new OnClickListener()  
        @Override 
        public void onClick(View v)  
        //Do what you want with the click here
         
    );


您还错误地声明了您的方法,在您的情况下,它看起来像这样:

public class MainActivity extends AppCompatActivity  

    EditText firstNumber; 
    EditText secondNumber; 
    TextView addResult; 
    Button btnAdd; 
    double num1,num2,sum;

    @Override 
    protected void onCreate(Bundle savedInstanceState)  
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);  
        firstNumber = findViewById(R.id.txtNumber1); 
        secondNumber = findViewById(R.id.txtNumber2); 
        addResult = findViewById(R.id.txtResult); 
        btnAdd = findViewById(R.id.btnAdd);

        myCoolButtonMethod();

    

    private void myCoolButtonMethod()
    Button clickButton = (Button) findViewById(R.id.clickButton);
    clickButton.setOnClickListener( new OnClickListener()  
        @Override 
        public void onClick(View v)  
            num1 = Double.parseDouble(firstNumber.getText().toString()); 
            num2 = Double.parseDouble(secondNumber.getText().toString()); 
            sum = num1 + num2;
            addResult.setText(Double.toString(sum)); 
         
    );

 

【讨论】:

【参考方案2】:

这样做:- 将 onClick 方法放在 onCreate 中。

public class MainActivity extends AppCompatActivity 

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

    EditText firstNumber;
    EditText secondNumber;
    TextView addResult;
    Button btnAdd;

    double num1,num2,sum;

    firstNumber = findViewById(R.id.txtNumber1);
    secondNumber = findViewById(R.id.txtNumber2);
    addResult = findViewById(R.id.txtResult);
    btnAdd = findViewById(R.id.btnAdd);

    btnAdd.setOnClickListener(new OnClickListener() 

        public void onClick(View v) 
            num1 = Double.parseDouble(firstNumber.getText().toString());
            num2 = Double.parseDouble(secondNumber.getText().toString());
            sum = num1 + num2;
            addResult.setText(Double.toString(sum));
        
    );

    

【讨论】:

【参考方案3】:

首先,您必须将视图声明更改为

secondNumber = (EditText) findViewById(R.id.action_save);
    addResult = (TextView) findViewById(R.id.action_save);
    btnAdd = (Button) findViewById(R.id.action_save);

然后将 double 变量更改为 final。 你必须把

btnAdd.setOnClickListener(new View.OnClickListener() 

里面

protected void onCreate(Bundle savedInstanceState) 

块方法。

这是完整的代码:

package com.example.apple.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.example.android.pets.R;

public class MainActivity extends AppCompatActivity 

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

        EditText firstNumber;
        EditText secondNumber;
        final TextView addResult;
        Button btnAdd;

        final double num1, num2, sum;

        firstNumber = (EditText) findViewById(R.id.action_save);
        secondNumber = (EditText) findViewById(R.id.action_save);
        addResult = (TextView) findViewById(R.id.action_save);
        btnAdd = (Button) findViewById(R.id.action_save);


        num1 = Double.parseDouble(firstNumber.getText().toString());
        num2 = Double.parseDouble(secondNumber.getText().toString());
        sum = num1 + num2;

        btnAdd.setOnClickListener(new View.OnClickListener() 

            public void onClick(View v) 
                addResult.setText(Double.toString(sum));
            
        );
    

【讨论】:

你的第一句话是错误的-first of all, you have to change views declaration to

以上是关于android中java代码中的语法错误[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

MTK 关闭耳机调至最大音量时,提示损伤听力

这个 sql 命令有啥语法错误? [关闭]

eclipse上的语法错误[关闭]

Android 我在我的代码中写了从服务器下载 mp3 文件的一些错误 [关闭]

运行.kv文件时,我不断收到语法错误。我该如何解决? [关闭]

“SELECT ID FROM registeruser WHERE username=?andpassword=?”中的语法错误[关闭]