这是我的第一个问题,对于这个个人项目,我的经验很少,但是我会尽力学习,所以这里是我的问题开始的地方。
我试图使我的应用程序在EditTexts没有用户输入时停止崩溃,因此我实现了trycatch方法。但是,我相信我可能在活动中为4个editTexts实施了错误的代码,并破坏了onClick侦听器访问变量和计算的能力。在此重组之后,onClick似乎无法访问公共无效的caudalinput。
package com.example.aguavida;
import android.content.Context;
import android.support.v7.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;
import java.lang.reflect.InvocationTargetException;
public class G_Inflow_Rate extends AppCompatActivity {
EditText medidoreditText;
EditText T1editText;
EditText T2editText;
EditText T3editText;
int m;
int t1;
int t2;
int t3;
public double a;
public double result;
public void caudalinput(View view) {
Context context_caudal = getApplicationContext();
String exceptionMessage = "";
String exceptionMessage1 = "";
String exceptionMessage2 = "";
String exceptionMessage3 = "";
try{
medidoreditText = (EditText) findViewById(R.id.medidoreditText);
m = Integer.parseInt(medidoreditText.getText().toString());
} catch (NumberFormatException e) {
exceptionMessage = "Balde";
}
try{
T1editText = (EditText) findViewById(R.id.t1editText);
t1 = Integer.parseInt(T1editText.getText().toString());
} catch (NumberFormatException e) {
exceptionMessage1 = "time 1";
}
try{
T2editText = (EditText) findViewById(R.id.t2editText);
t2 = Integer.parseInt(T2editText.getText().toString());
} catch (NumberFormatException e) {
exceptionMessage2 = "time 2";
}
try{
T3editText = (EditText) findViewById(R.id.t3editText);
t3 = Integer.parseInt(T3editText.getText().toString());
} catch (NumberFormatException e) {
exceptionMessage3 = "time 3";
}
Toast.makeText(this, exceptionMessage + " " + exceptionMessage1 + " " + exceptionMessage2 + "" + exceptionMessage3, Toast.LENGTH_LONG).show();
a = (t1 + t2 + t3)/3;
result = m / a;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_g__inflow__rate);
Button gcc_button = (Button) findViewById(R.id.gcc_button);
gcc_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView gcc_result_textView = (TextView) findViewById(R.id.g_result_textView);
gcc_result_textView.setText(result + " ");
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".G_Inflow_Rate">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="32dp"
android:text="Tiempo (en segundos)"
app:layout_constraintBottom_toTopOf="@+id/t1editText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="@+id/t2editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="32dp"
android:ems="10"
android:hint="tiempo 2 segundos"
android:inputType="number"
app:layout_constraintBottom_toTopOf="@+id/t3editText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="@+id/t1editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="32dp"
android:ems="10"
android:hint="tiempo 1 segundos"
android:inputType="number"
app:layout_constraintBottom_toTopOf="@+id/t2editText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="@+id/medidoreditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="40dp"
android:ems="10"
android:hint="litros"
android:inputType="number"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="@+id/t3editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="48dp"
android:ems="10"
android:hint="tiempo 3 segundos"
android:inputType="number"
app:layout_constraintBottom_toTopOf="@+id/gcc_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="24dp"
android:text="Litros del balde/botella"
app:layout_constraintBottom_toTopOf="@+id/medidoreditText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.506"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/gcc_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="64dp"
android:text="Calcular"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/g_result_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="60dp"
app:layout_constraintBottom_toTopOf="@+id/textView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.488"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<Button
android:id="@+id/button8"
android:layout_width="423dp"
android:layout_height="88dp"
android:text="info"
tools:layout_editor_absoluteX="-6dp"
tools:layout_editor_absoluteY="-8dp" />
</android.support.constraint.ConstraintLayout>
我的代码在下面,在这一点上,我喜欢关于如何进行此操作和构建此结构的一些指导,因为我100%确信我构建的代码非常错误。
非常感谢你!
最佳答案
您可以避免try-catch
仅检查EditText数据是否为空,然后将其转换为Integer。我已经发布了一个EditText
。你可以为别人做
String editor = medidoreditText.getText().toString();
m = TextUtils.isEmpty(editor) ? 0 : Integer.parseInt(editor);