如何在Java中将String解析为int [关闭]

有人可以向我解释为什么我不能通过使用将int结果转换为字符串

int result = Integer.parseInt(resultTextView.getText().toString());

码:

package com.example.trialrun;

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

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

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

    Button addBtn = (Button) findViewById(R.id.addBtn);
    addBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            EditText firstNumEditText = (EditText) findViewById(R.id.firstNumEditText);
            EditText secondNumEditText = (EditText) findViewById(R.id.secondNumEditText);
            TextView resultTextView = (TextView) findViewById(R.id.resultTextView);

            int num1 = Integer.parseInt(firstNumEditText.getText().toString());
            int num2 = Integer.parseInt(secondNumEditText.getText().toString());
            int result = Integer.parseInt(resultTextView.getText().toString());
            result = num1 + num2;
            resultTextView.setText(result);


        }
    });
}

}

评论