为什么在计算(视图视图)中提供属性(视图视图)在android中给我“无效的表达式开始”错误,用于定义按钮的功能

Why giving attributes (View view) in Calculate(View view) giving me 'invalid start of expression' error in android for defining function for button

提问人:Lokesh Kumar 提问时间:6/29/2023 更新时间:7/3/2023 访问量:18

问:

我正在制作用于在 android studio 中添加两个数字的应用程序。我试图将函数 Calculate() 分配给一个按钮,以便使用 xml 中的 onClick 属性进行计算。当我将属性(视图视图)放入 Calculate() 函数时,我收到错误“表达式的开始无效”。

这是我的 Java 和 xml 代码 ::

`package com.example.logintypeapp;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Button button;
    private TextView textView;
    private TextView DisplayView;
    private EditText editText;

    private EditText num2;
    private EditText num1;
    private View view;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = findViewById(R.id.button);
        num1= findViewById(R.id.num1);
        num2= findViewById(R.id.num1);
        DisplayView = findViewById(R.id.DIsplayView);


public void Calculate(View view){
            String n1=num1.getText().toString();
            String n2=num2.getText().toString();
            int num1=Integer.parseInt(n1);
            int num2=Integer.parseInt(n2);
            int num3=num1+num2;
            DisplayView.setText("Ans:  "+num3);
        }

    }
}

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </com.google.android.material.appbar.AppBarLayout>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="60dp"
        android:onClick="Calculate"
        android:text="Calculate"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/num2" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:layout_marginTop="68dp"
        android:text="App Login"
        android:textAlignment="center"
        android:textSize="40dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.478"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/num1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:ems="10"
        android:hint="Enter Number 1"
        android:inputType="number"
        android:textAlignment="center"
        android:textColorHint="#B34646"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <EditText
        android:id="@+id/num2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="48dp"
        android:ems="10"
        android:hint="Enter Number 2 "
        android:inputType="number"
        android:textAlignment="center"
        android:textColorHint="#9C3C3C"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.502"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/num1" />

    <TextView
        android:id="@+id/DIsplayView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:textSize="34sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />

</androidx.constraintlayout.widget.ConstraintLayout>`
java android xml onclick 语法错误

评论


答:

0赞 Pawan Harariya 7/3/2023 #1

该方法应位于该方法的外部。Calculate()onCreate()

同样对于 num2,您引用了 num1 视图。num2= findViewById(R.id.num1);

更好的方法:

您可以使用 .你可以在这里阅读更多关于它的信息。onClickViewOnClickListener

package com.example.logintypeapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity2 extends AppCompatActivity {
    private Button button;
    private TextView textView;
    private TextView DisplayView;
    private EditText editText;

    private EditText num2;
    private EditText num1;
    private View view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_2);
        button = findViewById(R.id.button);
        num1= findViewById(R.id.num1);
        num2= findViewById(R.id.num2);
        DisplayView = findViewById(R.id.DIsplayView);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Calculate();
            }
        });
    }

    public void Calculate() {
        String n1=num1.getText().toString();
        String n2=num2.getText().toString();
        int num1=Integer.parseInt(n1);
        int num2=Integer.parseInt(n2);
        int num3=num1+num2;
        DisplayView.setText("Ans:  "+num3);
    }
}