Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 您好,我尝试将数据库中的字符串值转换为int,但没有';不会发生 这段代码必须使用数据库中的两个(字符串)值来计算“imt”值。但是我的程序崩溃了,我把我的日志给了你!我想,我在整数转换上错了。 我认为我的数据库不正确? 我的主要活动是什么_Java_Android_Database_Type Conversion - Fatal编程技术网

Java 您好,我尝试将数据库中的字符串值转换为int,但没有';不会发生 这段代码必须使用数据库中的两个(字符串)值来计算“imt”值。但是我的程序崩溃了,我把我的日志给了你!我想,我在整数转换上错了。 我认为我的数据库不正确? 我的主要活动是什么

Java 您好,我尝试将数据库中的字符串值转换为int,但没有';不会发生 这段代码必须使用数据库中的两个(字符串)值来计算“imt”值。但是我的程序崩溃了,我把我的日志给了你!我想,我在整数转换上错了。 我认为我的数据库不正确? 我的主要活动是什么,java,android,database,type-conversion,Java,Android,Database,Type Conversion,公共类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

公共类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);


    }
}
DataBaseHelper-myDb;
编辑文本名称编辑文本、年龄编辑文本、重量编辑文本、高度编辑文本;
纺纱工性别纺纱工、创伤纺纱工、疾病纺纱工;
按钮保存,测试b;
字符串名;
文本视图测试;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*数据库*/
myDb=新数据库助手(此);
test=(TextView)findViewById(R.id.test\u text);
test_b=(按钮)findviewbyd(R.id.test);
test_b.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
test();
}
});
/*纺纱机*/
性别\u微调器=(微调器)findViewById(R.id.spinner\u性别);
创伤\旋转器=(旋转器)查找视图id(R.id.旋转器\创伤);
疾病\u微调器=(微调器)findViewById(R.id.spinner\u疾病);
字符串[]性别={“男”,“女”};
ArrayAdapter gender\u adapter=新的ArrayAdapter(这个,android.R.layout.simple\u微调器\u下拉菜单\u项,gender);
设置适配器(性别适配器);
字符串[]创伤={“背”、“手”、“腿”};
ArrayAdapter tramage\u adapter=新的ArrayAdapter(这个,android.R.layout.simple\u微调器\u下拉菜单\u项,tramage);
创伤旋转器。设置适配器(创伤转接器);
字符串[]={“是”,“无”};
ArrayAdapter\u adapter=新的ArrayAdapter(这是android.R.layout.simple\u微调器\u下拉菜单\u项,疾病);
疾病\u微调器.setAdapter(疾病\u适配器);
/*编辑文本*/
name_edittext=(edittext)findviewbyd(R.id.name);
age_edittext=(edittext)findViewById(R.id.age);
权重_edittext=(edittext)findViewById(R.id.weight);
高度\ edittext=(edittext)findViewById(R.id.height);
/*钮扣*/
save=(按钮)findviewbyd(R.id.save_按钮);
/*拯救*/
save.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
单击我();
}
});
}
私有void ClickMe(){
String name=name_edittext.getText().toString();
字符串age=age_edittext.getText().toString();
字符串权重=权重_edittext.getText().toString();
字符串高度=高度_edittext.getText().toString();
String gender=gender_spinner.getSelectedItem().toString();
字符串trama=trama_spinner.getSelectedItem().toString();
字符串diseases=diseases\u spinner.getSelectedItem().toString();
布尔结果=myDb.insertData(姓名、年龄、体重、身高、性别、创伤、疾病);
如果(结果==真){
Toast.makeText(此“数据添加成功”,Toast.LENGTH_SHORT.show();
}否则{
Toast.makeText(此“未添加数据”,Toast.LENGTH_SHORT.show();
}
意向i=新意向(此,MenuActivity.class);
星触觉(i);
}
公开无效测试(){
Cursor res=myDb.getAllData();
如果(res!=null&&res.getCount()>0){
while(res.moveToNext()){
name=res.getString(res.getColumnIndex(“name”));
}
Toast.makeText(这是“读取数据”,Toast.LENGTH_SHORT).show();
}否则{
Toast.makeText(这个“数据未读”,Toast.LENGTH_SHORT.show();
}
test.setText(名称);
}
}
在我的日程安排结束时#
NumberFormatException
在尝试将空字符串转换为整数时发生。 您可以通过检查空值来保存代码,如下所示:

if(!height.trim.isEmpty()){
    height_int = Integer.parseInt(height.trim());
} else {
    height_int = 0;
}
try{
    height_int = Integer.parseInt(height.trim());
catch(NumberFormatException e){
    height_int = 0;
}
您还可以使用
try/catch
块来避免如下崩溃:

if(!height.trim.isEmpty()){
    height_int = Integer.parseInt(height.trim());
} else {
    height_int = 0;
}
try{
    height_int = Integer.parseInt(height.trim());
catch(NumberFormatException e){
    height_int = 0;
}

在此处添加日志并检查logcat中显示的输出?我认为这是在获取空值。

这是一个多么糟糕的代码啊,希望我们能够通过
java.lang.NumberFormatException:无效int:“
”。您正在尝试将空字符串转换为int。如何在我的代码中更改它(我是较新的)?不要在注释中发布代码。问题是,我试着写“if(zero)”,但对我没有帮助。你试图把int列作为字符串来写和读,这让我产生了怀疑。尝试将其作为int读取。我上次添加到数据库中的值很多。不是零请发送logcat06-09 23:53:06.865 32253-32253/adais中显示的体重和身高值的屏幕截图。myway I/weight:6506-09 23:53:06.865 32253-32253/adais.myway I/height:18506-09 23:53:06.866 32253-32253/adais.myway I/weight:68这对我没有帮助)
if(!height.trim.isEmpty()){
    height_int = Integer.parseInt(height.trim());
} else {
    height_int = 0;
}
try{
    height_int = Integer.parseInt(height.trim());
catch(NumberFormatException e){
    height_int = 0;
}
Cursor res = myDb.getAllData();
     if(res!=null&&res.getCount()>0){
          while (res.moveToNext()){
               weight = res.getString(res.getColumnIndex("WEIGHT"));
               height = res.getString(res.getColumnIndex("HEIGHT"));

Log.i("Weight", weight);
Log.i("Height", height);