我有一个简单的Android应用程序来执行一些计算。 虽然文件中没有错误并且构建成功运行,但应用程序崩溃了。
我正在使用Android Studio 3.1,并在Android API 23上测试此应用程序。
我的代码: (见更新)
我的XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/calculate_flight_time"
android:layout_width="276dp"
android:layout_height="71dp"
android:layout_marginBottom="96dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="Calculate Flight Time"
android:textSize="18sp"
android:onClick="calculateFT"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.54"
app:layout_constraintStart_toStartOf="parent" />
<Spinner
android:id="@+id/droneType"
android:layout_width="245dp"
android:layout_height="44dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="12dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="319dp"
android:layout_height="51dp"
android:layout_marginBottom="30dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:gravity="center"
android:text="This app intends to help drone pilots compute/predict their UAV's flight time beforehand, providing a better insight into prospective builds"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.578"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="By Shlok Jhawar"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<EditText
android:id="@+id/batteryCapacity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="number"
android:hint="Battery Capacity (mAh)"
android:text="3000"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/droneType" />
<EditText
android:id="@+id/MAPM"
android:layout_width="wrap_content"
android:layout_height="43dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="20"
android:ems="10"
android:hint="Max amps per motor"
android:inputType="number"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/batteryCapacity" />
<EditText
android:id="@+id/MTPM"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="Max thrust per motor (g)"
android:text="900"
android:inputType="number"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/MAPM" />
<EditText
android:id="@+id/droneWeight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="Weight of drone"
android:text="1800"
android:inputType="number"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/MTPM" />
</android.support.constraint.ConstraintLayout>
我不确定为什么会这样。 任何形式的帮助将不胜感激。
更新: 领先一步,我的代码不会崩溃。 (感谢Manishoaham和Faysal)。 做了一些其他的调整。 但是现在,time_in_seconds评估为无穷大,我无法弄明白。请帮忙。 这是我的新代码:
package com.example.android.uavuptimecalculator;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
//import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
int number_of_rotors;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner typeOfDrone = (Spinner) findViewById(R.id.droneType);
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.types));
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
typeOfDrone.setAdapter(myAdapter);
typeOfDrone.setSelection(1);
String drone_type = typeOfDrone.getSelectedItem().toString();
if (drone_type=="Tricopter") {
number_of_rotors=3;
}
if (drone_type=="Quadcopter") {
number_of_rotors=4;
}
if (drone_type=="Hexacopter") {
number_of_rotors=6;
}
if (drone_type=="Octacopter") {
number_of_rotors=8;
}
EditText Battery_Capacity = (EditText) findViewById(R.id.batteryCapacity);
String stringA = Battery_Capacity.getText().toString();
EditText Amps_perMotor = (EditText) findViewById(R.id.MAPM);
String stringB = Amps_perMotor.getText().toString();
EditText Thrust_perMotor = (EditText) findViewById(R.id.MTPM);
String stringC = Thrust_perMotor.getText().toString();
EditText Weight_ofDrone = (EditText) findViewById(R.id.droneWeight);
String stringD = Weight_ofDrone.getText().toString();
int battery_capacity_mah =Integer.parseInt(stringA!=null&&!stringA.isEmpty()?stringA:"0");
//int battery_capacity_mah = Integer.parseInt(Battery_Capacity.toString());
//int total_max_ampdraw = Integer.parseInt(Amps_perMotor.toString()) * number_of_rotors;
int total_max_ampdraw = Integer.parseInt(stringB!=null&&!stringB.isEmpty()?stringB:"0") * number_of_rotors;
//int total_thrust = Integer.parseInt(Thrust_perMotor.toString()) * number_of_rotors;
int total_thrust = Integer.parseInt(stringC!=null&&!stringC.isEmpty()?stringC:"0") * number_of_rotors;
//float required_throttle = Integer.parseInt(Weight_ofDrone.toString()) / total_thrust;
float required_throttle = Integer.parseInt(stringD!=null&&!stringD.isEmpty()?stringD:"0") * number_of_rotors;
/*if (required_throttle>=1){
Toast.makeText(MainActivity.this,"UAV will not take off",Toast.LENGTH_SHORT);
}*/
float avg_ampdraw=required_throttle*total_max_ampdraw;
String abc = Float.toString(avg_ampdraw);
Toast.makeText(MainActivity.this,abc,Toast.LENGTH_SHORT);
//Toast.makeText(MainActivity.this,avg_ampdraw,Toast.LENGTH_SHORT);
float battery_capacity_ah=battery_capacity_mah/1000;
float time_in_hours=battery_capacity_ah/avg_ampdraw;
final float time_in_minutes=time_in_hours*60;
final float time_in_seconds=time_in_minutes*60;
//public void calculateFT()
Button CalculateFTime = (Button) findViewById(R.id.calculate_flight_time);
CalculateFTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
mBuilder.setTitle("Calculated Flight Time");
//mBuilder.setTitle(R.string.dialog_title);
mBuilder.setMessage("Your UAV will fly for "+time_in_seconds+" seconds, which is equivalent to "+time_in_minutes+" minutes");
mBuilder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alertDialog = mBuilder.create();
alertDialog.show();
}
});
}
}
所有帮助将不胜感激。
答案 0 :(得分:0)
从EditText获取文本有一些问题。要从EditText获取文本,请使用以下行:
String string = yourEditText.getText().toString();
您需要在getText()
之前添加toString()
才能获取文字。
int battery_capacity_mah = Integer.parseInt(Battery_Capacity.getText().toString());
int total_max_ampdraw = Integer.parseInt(Amps_perMotor.getText().toString()) * number_of_rotors;
int total_thrust = Integer.parseInt(Thrust_perMotor.getText().toString()) * number_of_rotors;
float required_throttle = Integer.parseInt(Weight_ofDrone.getText().toString()) / total_thrust;
希望这会有所帮助。
答案 1 :(得分:0)
首先从您的editText获取文字:
kind
其次,由于您已经将inputStyle定义为数字,因此您只会获得可解析的数字字符串,但也会检查您是否获得空字符串,如下所示
String string = Battery_Capacity.getText().toString();
同样适用于所有其他editTexts。
尝试使用此代码:
int battery_capacity_ham = Integer.parseInt(string!=null&&!string.isEmpty()?string:"0");