Этот фрагмент кода должен считать значение imt, используя два (String) значения из моей базы данных.Но моя программа вылетает, я дал свой Logcat ниже!Я думаю, я не прав в преобразовании целых чисел.
public class ScheduleActivity extends AppCompatActivity {
DataBaseHelper myDb;
Button mon_tue,wed_th,fri_sat;
TextView result_title,result_warm_up,result_one,result_two,result_three,result_four,result_five,result_six,result_seven;
long imt;
String name;
String age;
String weight;
String height;
String gender;
String trauma;
String illnesses;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_schedule);
/*DataBase*/
myDb = new DataBaseHelper(this);
/*Buttons*/
mon_tue = (Button) findViewById(R.id.mon_tue);
wed_th = (Button) findViewById(R.id.wed_th);
fri_sat = (Button) findViewById(R.id.fri_sat);
/*TEXTVIEWS*/
result_title = (TextView) findViewById(R.id.result_title);
result_warm_up = (TextView) findViewById(R.id.result_warm_up);
result_one = (TextView) findViewById(R.id.result_one);
result_two = (TextView) findViewById(R.id.result_two);
result_three = (TextView) findViewById(R.id.result_three);
result_four = (TextView) findViewById(R.id.result_four);
result_five = (TextView) findViewById(R.id.result_five);
result_six = (TextView) findViewById(R.id.result_six);
result_seven = (TextView) findViewById(R.id.result_seven);
/*read data*/
Cursor res = myDb.getAllData();
if(res!=null&&res.getCount()>0){
while (res.moveToNext()){
name = res.getString(res.getColumnIndex("NAME"));
age = res.getString(res.getColumnIndex("AGE"));
weight = res.getString(res.getColumnIndex("WEIGHT"));
height = res.getString(res.getColumnIndex("HEIGHT"));
gender = res.getString(res.getColumnIndex("GENDER"));
trauma = res.getString(res.getColumnIndex("TRAUMA"));
illnesses = res.getString(res.getColumnIndex("ILLNESSES"));
/*IMT(imt) - Brain of my App*/
int height_int = Integer.parseInt(height.trim());
int weight_int = Integer.parseInt(weight.trim());
imt = ((weight_int/(height_int*height_int))*10000);
/*It is the end of brain opportunities*/
if(imt<18.5){
result_title.setText(R.string.hi+", "+R.string.name);
result_warm_up.setText(R.string.warm_up);
result_one.setText(R.string.abdominal_muscles+" "+name);
result_two.setText(R.string.the_dumbbell_fly+" "+"");
result_three.setText(R.string.extension_of_the_trunk+" "+"");
result_four.setText(R.string.hand_dumbbell_side_lift+" "+"");
result_five.setText(R.string.pull_up+" "+"");
result_six.setText(R.string.pullover+" "+"");
}
if(imt==0){
result_title.setText(R.string.data);
}
}
}else{
result_title.setText(R.string.result_wrong);
}
}
}
Мой logcat!
/myway E/AndroidRuntime: FATAL EXCEPTION: main
Process: adais.myway, PID: 30254
java.lang.RuntimeException: Unable to start activity ComponentInfo{adais.myway/myway.ScheduleActivity}: java.lang.NumberFormatException: Invalid int: ""
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NumberFormatException: Invalid int: ""
at java.lang.Integer.invalidInt(Integer.java:137)
at java.lang.Integer.parseInt(Integer.java:358)
at java.lang.Integer.parseInt(Integer.java:331)
at adais.myway.ScheduleActivity.onCreate(ScheduleActivity.java:58)
at android.app.Activity.performCreate(Activity.java:5264)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
at dalvik.system.NativeStart.main(Native Method)
Я думаю, что моя база данных неверна?
public class DataBaseHelper extends SQLiteOpenHelper{
public static final String DATABASE_NAME = "Schedule.dp";
public static final String TABLE_NAME = "Schedule_table";
/*Col*/
public static final String COL_1 = "ID";
public static final String COL_2 = "NAME";
public static final String COL_3 = "AGE";
public static final String COL_4 = "WEIGHT";
public static final String COL_5 = "HEIGHT";
public static final String COL_6 = "GENDER";
public static final String COL_7 = "TRAUMA";
public static final String COL_8 = "ILLNESSES";
public DataBaseHelper(Context context) {
super(context,DATABASE_NAME,null,1);
}
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL("CREATE TABLE "+TABLE_NAME+" (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,AGE INTEGER,WEIGHT INTEGER,HEIGHT INTEGER,GENDER TEXT,TRAUMA TEXT,ILLNESSES TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
db.execSQL("DROP TABLE IF EXISTS "+ TABLE_NAME);
}
public boolean updataData(String id,String name,String age,String weight,String height,String gender,String trauma,String illnesses){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2,name);
contentValues.put(COL_3,age);
contentValues.put(COL_4,weight);
contentValues.put(COL_5,height);
contentValues.put(COL_6,gender);
contentValues.put(COL_7,trauma);
contentValues.put(COL_8,illnesses);
int result = db.update(TABLE_NAME,contentValues,"ID =?",new String[]{id});
if (result>0){
return true;
}else{
return false;
}
}
public boolean insertData(String name,String age,String weight,String height,String gender,String trauma,String illnesses){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2,name);
contentValues.put(COL_3,age);
contentValues.put(COL_4,weight);
contentValues.put(COL_5,height);
contentValues.put(COL_6,gender);
contentValues.put(COL_7,trauma);
contentValues.put(COL_8,illnesses);
long result = db.insert(TABLE_NAME,null,contentValues);
db.close();
if (result==-1){
return false;
}else{
return true;
}
}
public Cursor getAllData(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("Select * from "+ TABLE_NAME,null);
return res;
}
}
И мой главныйактивность
открытый класс MainActivity расширяет AppCompatActivity {
DataBaseHelper myDb;
EditText name_edittext,age_edittext,weight_edittext,height_edittext;
Spinner gender_spinner,trauma_spinner,illnesses_spinner;
Button save,test_b;
String name;
TextView test;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*DataBase*/
myDb = new DataBaseHelper(this);
test = (TextView) findViewById(R.id.test_text);
test_b = (Button) findViewById(R.id.test);
test_b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
test();
}
});
/*Spinners*/
gender_spinner = (Spinner) findViewById(R.id.spinner_gender);
trauma_spinner = (Spinner) findViewById(R.id.spinner_trauma);
illnesses_spinner = (Spinner) findViewById(R.id.spinner_illnesses);
String [] gender={"Male","Female"};
ArrayAdapter<String> gender_adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,gender);
gender_spinner.setAdapter(gender_adapter);
String [] trauma={"Back","Hand","Leg"};
ArrayAdapter<String> trauma_adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,trauma);
trauma_spinner.setAdapter(trauma_adapter);
String [] illnesses={"Yes","None"};
ArrayAdapter<String> illnesses_adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,illnesses);
illnesses_spinner.setAdapter(illnesses_adapter);
/*EditTexts*/
name_edittext = (EditText) findViewById(R.id.name);
age_edittext = (EditText) findViewById(R.id.age);
weight_edittext = (EditText) findViewById(R.id.weight);
height_edittext = (EditText) findViewById(R.id.height);
/*Button*/
save = (Button) findViewById(R.id.save_button);
/*Save*/
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ClickMe();
}
});
}
private void ClickMe(){
String name = name_edittext.getText().toString();
String age = age_edittext.getText().toString();
String weight = weight_edittext.getText().toString();
String height = height_edittext.getText().toString();
String gender = gender_spinner.getSelectedItem().toString();
String trauma = trauma_spinner.getSelectedItem().toString();
String illnesses = illnesses_spinner.getSelectedItem().toString();
Boolean result = myDb.insertData(name,age,weight,height,gender,trauma,illnesses);
if (result == true){
Toast.makeText(this,"Data is added successfully",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this,"Data is not added",Toast.LENGTH_SHORT).show();
}
Intent i = new Intent(this,MenuActivity.class);
startActivity(i);
}
public void test(){
Cursor res = myDb.getAllData();
if(res!=null&&res.getCount()>0){
while (res.moveToNext()){
name = res.getString(res.getColumnIndex("NAME"));
}
Toast.makeText(this,"Data is read",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this,"Data is unread",Toast.LENGTH_SHORT).show();
}
test.setText(name);
}
}
В конце моя схема размещения #
<ScrollView 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=".ScheduleActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/mon_tue"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#0091EA"
android:text="@string/mon_tue"/>
<Button
android:id="@+id/wed_th"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#0091EA"
android:text="@string/wed_th"/>
<Button
android:id="@+id/fri_sat"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#0091EA"
android:text="@string/fri_sat"/>
</LinearLayout>
<TextView
android:layout_margin="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/result_title"
android:textSize="26dp"
android:textStyle="italic"
android:text="@string/result_wrong"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="@+id/result_warm_up"
android:textSize="26dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="@+id/result_one"
android:textSize="26dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="@+id/result_two"
android:textSize="26dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="@+id/result_three"
android:textSize="26dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="@+id/result_four"
android:textSize="26dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="@+id/result_five"
android:textSize="26dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="@+id/result_six"
android:textSize="26dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="@+id/result_seven"
android:textSize="26dp"/>
</LinearLayout>
</ScrollView>