为什么我的 textinputs 不接收任何数据并在 android studio 中显示为空?

why my textinputs doesn't take any data and appear empty in android studio?

当我尝试使用workmanager的输入数据时,它抛出异常:java.lang.NumberFormatException:Input"",这意味着使用的变量是空的,但在gui中被填充了。也尝试通过将变量转换为 int 但相同,问题是它没有输入

mainactivity.java

public class MainAvtivity extends AppCompatActivity {
Button btNn;
EditText value1, value2;

public static final String VALUE1 = "value1";
public static final String VALUE2 = "value2";

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

    value1 = (EditText)findViewById(R.id.Firstvalue);
    value2 = (EditText) findViewById(R.id.Secondvalue);
    btNn = (Button)findViewById(R.id.calculate);

    String Value1 = value1.getText().toString().trim();
    String Value2 = value2.getText().toString().trim();

    Data DATa = new Data.Builder()
            .putString(VALUE1, Value1)
            .putString(VALUE2, Value2)
            .build();

    final OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(smabullsetwork.class)
            .setInputData(DATa)
            .build();

    btNn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            WorkManager.getInstance().enqueue(workRequest);
        }
    });
}

工作管理器文件

public class calculator extends Worker {

public claculator(@NonNull Context context, @NonNull WorkerParameters workerParams) {
    super(context, workerParams);
}

@NonNull
@Override
public Result doWork() {

    Data data = getInputData();
    String valuee1 = data.getString(MainActivity.VALUE1);
    String valuee2 = data.getString(MainActivity.VALUE2);

    int Value1 = Integer.parseInt(valuee1);
    int value2 = Integer.parseInt(valuee2);
    int result = value1*value2;
    DisplayNotiFication("Result is...", result);

    return Result.success();
}

您应该调用 getText 并在 setOnClickListener 中构建变量 DATa。

您的代码将是:

    public class MainAvtivity extends AppCompatActivity {
      Button btNn;
      EditText value1, value2;

      public static final String VALUE1 = "value1";
      public static final String VALUE2 = "value2";

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

        value1 = (EditText) findViewById(R.id.Firstvalue);
        value2 = (EditText) findViewById(R.id.Secondvalue);
        btNn = (Button) findViewById(R.id.calculate);

        btNn.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
            String Value1 = value1.getText().toString().trim();
            String Value2 = value2.getText().toString().trim();

            Data DATa = new Data.Builder()
              .putString(VALUE1, Value1)
              .putString(VALUE2, Value2)
              .build();

            final OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(smabullsetwork.class)
              .setInputData(DATa)
              .build();
            WorkManager.getInstance().enqueue(workRequest);
          }
        });
      }