When I try to use the input data with workmanager, it throws an exception: java.lang.NumberFormatException:Input"", it means that the variables used are empty but they are filled in the gui. Also tried by converting the variable to int but is the same, the problem is that it doesn't have the input
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);
}
});
}
workmanager file
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();
}
CodePudding user response:
You should call getText and build your variable DATa inside the setOnClickListener.
Your code will be :
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);
}
});
}