[英]If there is a blank field on my application it crashes (java, android)
I have tried a few things that I searched here but they didn't work.我已经尝试了一些我在这里搜索的东西,但它们没有用。 I tried a try/catch method and many if-functions it's crazy.
我尝试了 try/catch 方法和许多 if 函数,这太疯狂了。 I basically just want to display a message in a text view box when nothing is entered to tell the user to enter something.
我基本上只想在没有输入任何内容时在文本视图框中显示一条消息,告诉用户输入一些内容。 This is basically just an application to display the average that person has run for a certain amount of km.
这基本上只是一个应用程序,用于显示一个人跑了一定公里数的平均值。
Here is my code below:下面是我的代码:
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.text.DecimalFormat;
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initiating and referencing for Calculate Button
Button button = findViewById(R.id.button);
//onClickListener for the Calculate Button
button.setOnClickListener(v -> {
//Variables to calculate average runtime
final EditText totalHours = findViewById(R.id.totalHours);
final EditText totalMinutes = findViewById(R.id.totalMins);
double kilometer = 42.2;
//Creating a variable to display an error message
final TextView errorMsg = findViewById(R.id.errorMsg);
//Checking to see if anything was entered into the editText fields -- THE ISSUE I HAVE
if(totalHours.length() < 0 || totalMinutes.length() < 0){
errorMsg.setText(R.string.nothingEnteredError);
} else {
//Parse the input from the user (from text to double)
double Hours = Integer.parseInt(totalHours.getText().toString());
double Minutes = Integer.parseInt(totalMinutes.getText().toString());
//Checking requirements - not more than 10 hours entered and not more than 59 minutes entered.
if (Hours > 10) {
errorMsg.setText(R.string.errorOnHours);
} else if (Minutes > 59) {
errorMsg.setText(R.string.errorOnMinutes);
} else {
//calculating average runtime
double average = ((Hours * 60) + Minutes) / kilometer;
//setting the format to 0.00
DecimalFormat aForm = new DecimalFormat("0.00");
//creating a string version of average
String sAverage = Double.toString(average);
//Text to display in the second activity
String textToSet = String.format("Your average time to run one kilometer was %s", aForm.format(average));
//creating net intent to start the results page activity
Intent i = new Intent(MainActivity.this, resultsPage.class);
//adding textToSet to the results activity
i.putExtra("key", textToSet);
i.putExtra("average", sAverage);
//Starting results activity
startActivity(i);
}
}
});
}
}```
totalHours.length() < 0 || totalMinutes.length() < 0
Here you only check if the length is lower than zero.在这里你只检查长度是否小于零。 I would try changing this so it checks for
length <= 0
我会尝试更改它以便检查
length <= 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.