Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.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 Android Studio-尝试重新打开已关闭的对象:SQLiteDatabase_Java_Android_Sql_Sqlite - Fatal编程技术网

Java Android Studio-尝试重新打开已关闭的对象:SQLiteDatabase

Java Android Studio-尝试重新打开已关闭的对象:SQLiteDatabase,java,android,sql,sqlite,Java,Android,Sql,Sqlite,我正在制作我的第一个Android应用程序,当我使用Java时,我是一个完全的新手,因为我只使用过Python。我试图创建一个数据库,通过初始设置活动存储用户的详细信息,但是我得到了一个错误 08-13 17:24:27.400 10263-10263/? E/AndroidRuntime: Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLite

我正在制作我的第一个Android应用程序,当我使用Java时,我是一个完全的新手,因为我只使用过Python。我试图创建一个数据库,通过初始设置活动存储用户的详细信息,但是我得到了一个错误

08-13 17:24:27.400 10263-10263/? E/AndroidRuntime:  Caused by:        java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/user/0/fitness.essentials.fitnessessentials/databases/UserData.db
08-13 17:24:27.400 10263-10263/? E/AndroidRuntime:     at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
08-13 17:24:27.400 10263-10263/? E/AndroidRuntime:     at android.database.sqlite.SQLiteDatabase.endTransaction(SQLiteDatabase.java:525)
08-13 17:24:27.400 10263-10263/? E/AndroidRuntime:     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:262)
08-13 17:24:27.400 10263-10263/? E/AndroidRuntime:     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
08-13 17:24:27.400 10263-10263/? E/AndroidRuntime:     at fitness.essentials.fitnessessentials.Database.<init>(Database.java:26)
08-13 17:24:27.400 10263-10263/? E/AndroidRuntime:     at fitness.essentials.fitnessessentials.SetupScreen.onCreate(SetupScreen.java:44)
08-13 17:24:27.400 10263-10263/? E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:6876)
08-13 17:24:27.400 10263-10263/? E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
08-13 17:24:27.400 10263-10263/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
08-13 17:24:27.400 10263-10263/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) 
08-13 17:24:27.400 10263-10263/? E/AndroidRuntime:     at android.app.ActivityThread.access$1100(ActivityThread.java:221) 
08-13 17:24:27.400 10263-10263/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
08-13 17:24:27.400 10263-10263/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
08-13 17:24:27.400 10263-10263/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:158) 
08-13 17:24:27.400 10263-10263/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:7224) 
08-13 17:24:27.400 10263-10263/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
08-13 17:24:27.400 10263-10263/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
08-13 17:24:27.400 10263-10263/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
这是我试图添加详细信息的设置屏幕类

public class SetupScreen extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

private String Name;
private String Gender;
private String Height;
private String CurrentWeight;
private String TargetWeight;
private String DOB;
private String ActivityLevel;
private String WeightDifference;

private Database dbhelper;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_setup_screen);
    Name = "";
    Gender = "";
    Height = "";
    CurrentWeight = "";
    TargetWeight = "";
    DOB = "";
    ActivityLevel = "";
    WeightDifference = "";


    dbhelper = new Database(getApplicationContext());





    Spinner spinnerGender = (Spinner) findViewById(R.id.spinnerGender);

    ArrayAdapter<CharSequence> adapterGender = ArrayAdapter.createFromResource(this,
            R.array.gender, android.R.layout.simple_spinner_item);
    adapterGender.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerGender.setAdapter(adapterGender);

    spinnerGender.setOnItemSelectedListener(this);

    Spinner spinnerActivityLevel = (Spinner) findViewById(R.id.spinnerActivityLevel);

    ArrayAdapter<CharSequence> adapterActivity = ArrayAdapter.createFromResource(this,
            R.array.activity_level, android.R.layout.simple_spinner_item);
    adapterActivity.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerActivityLevel.setAdapter(adapterActivity);

    spinnerActivityLevel.setOnItemSelectedListener(this);

    Spinner spinnerWeightDifference = (Spinner) findViewById(R.id.spinnerWeightDifference);

    ArrayAdapter<CharSequence> adapterWeight = ArrayAdapter.createFromResource(this,
            R.array.weight_difference, android.R.layout.simple_spinner_item);
    adapterWeight.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerWeightDifference.setAdapter(adapterWeight);

    spinnerWeightDifference.setOnItemSelectedListener(this);



}

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    Gender = parent.getItemAtPosition(position).toString();
    ActivityLevel = parent.getItemAtPosition(position).toString();
    WeightDifference = parent.getItemAtPosition(position).toString();
}

public void onNothingSelected(AdapterView<?> parent) {
    // An interface callback
}

public void save(View view) {


    Name = ((EditText)findViewById(R.id.Name)).getText().toString();
    DOB = ((EditText)findViewById(R.id.DOB)).getText().toString();
    Height = ((EditText)findViewById(R.id.Height)).getText().toString();
    CurrentWeight = ((EditText)findViewById(R.id.CurrentWeight)).getText().toString();
    TargetWeight = ((EditText)findViewById(R.id.TargetWeight)).getText().toString();


    if (Name.isEmpty()){
        Toast.makeText(getApplicationContext(), "Name cannot be empty!", Toast.LENGTH_LONG).show();
        return;
    }
    if (Gender.isEmpty()){
        Toast.makeText(getApplicationContext(), "Gender cannot be empty!", Toast.LENGTH_LONG).show();
        return;
    }
    if (DOB.isEmpty()){
        Toast.makeText(getApplicationContext(), "DOB cannot be empty!", Toast.LENGTH_LONG).show();
        return;
    }
    if (Height.isEmpty()){
        Toast.makeText(getApplicationContext(), "Height cannot be empty!", Toast.LENGTH_LONG).show();
        return;
    }
    if (CurrentWeight.isEmpty()){
        Toast.makeText(getApplicationContext(), "Current weight cannot be empty!", Toast.LENGTH_LONG).show();
        return;
    }
    Float CurrentWeightNum = Float.parseFloat(CurrentWeight);
    if (CurrentWeightNum < 43.0){
        Toast.makeText(getApplicationContext(), "Your current weight is not supported by this application", Toast.LENGTH_LONG).show();
        return;
    }

    if (TargetWeight.isEmpty()){
        Toast.makeText(getApplicationContext(), "Target weight cannot be empty!", Toast.LENGTH_LONG).show();
        return;
    }
    Float TargetWeightNum = Float.parseFloat(TargetWeight);
    if (TargetWeightNum > 93.0){
        Toast.makeText(getApplicationContext(), "Your target weight is not supported by this application", Toast.LENGTH_LONG).show();
        return;
    }
    if (ActivityLevel.isEmpty()){
        Toast.makeText(getApplicationContext(), "Activity level cannot be empty!", Toast.LENGTH_LONG).show();
        return;
    }
    if (WeightDifference.isEmpty()){
        Toast.makeText(getApplicationContext(), "Weight difference cannot be empty!", Toast.LENGTH_LONG).show();
        return;
    }

    if (CurrentWeightNum.equals(TargetWeightNum)){
        Toast.makeText(getApplicationContext(), "Current and target weight can not be the same!", Toast.LENGTH_LONG).show();
        return;
    }

    dbhelper.addUserData(Name, Gender, DOB, Height, CurrentWeightNum, TargetWeightNum, ActivityLevel, WeightDifference);

    Toast.makeText(getApplicationContext(), "You have been saved!", Toast.LENGTH_LONG).show();

    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);


}
public class SetupScreen扩展AppCompative活动实现AdapterView.OnItemSelectedListener{
私有字符串名称;
私人字符串性别;
私有字符串高度;
私有字符串当前权重;
私有字符串目标权重;
私有字符串DOB;
私有字符串ActivityLevel;
私有字符串权重差;
专用数据库dbhelper;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u setup\u屏幕);
Name=“”;
性别=”;
高度=”;
当前权重=”;
TargetWeight=“”;
DOB=“”;
ActivityLevel=“”;
权重差=”;
dbhelper=新数据库(getApplicationContext());
Spinner spinnerGender=(Spinner)findViewById(R.id.spinnerGender);
ArrayAdapter adapterGender=ArrayAdapter.createFromResource(此,
R.array.gender,android.R.layout.simple\u spinner\u item);
adapterGender.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
喷丝头性别:setAdapter(adapterGender);
spinnerGender.SetonimSelectedListener(此);
微调器spinnerActivityLevel=(微调器)findViewById(R.id.spinnerActivityLevel);
ArrayAdapter adapterActivity=ArrayAdapter.createFromResource(此,
R.array.activity\u level、android.R.layout.simple\u微调器\u项);
adapterActivity.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
SpinGeneraActivityLevel.setAdapter(适配器活动);
spinnerActivityLevel.setOnItemSelectedListener(此);
喷丝器喷丝器重量差=(喷丝器)findViewById(R.id.喷丝器重量差);
ArrayAdapter adapterWeight=ArrayAdapter.createFromResource(此,
R.array.weight\u difference,android.R.layout.simple\u spinner\u item);
adapterWeight.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
喷丝头重量差。设置适配器(适配器重量);
喷丝头重量差。setOnItemSelectedListener(此);
}
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
Gender=parent.getItemAtPosition(position.toString();
ActivityLevel=parent.getItemAtPosition(position.toString();
WeightDifference=parent.getItemAtPosition(position.toString();
}
未选择公共无效(AdapterView父级){
//接口回调
}
公共作废保存(视图){
Name=((EditText)findViewById(R.id.Name)).getText().toString();
DOB=((EditText)findViewById(R.id.DOB)).getText().toString();
高度=((EditText)findViewById(R.id.Height)).getText().toString();
CurrentWeight=((EditText)findViewById(R.id.CurrentWeight)).getText().toString();
TargetWeight=((EditText)findViewById(R.id.TargetWeight)).getText().toString();
if(Name.isEmpty()){
Toast.makeText(getApplicationContext(),“名称不能为空!”,Toast.LENGTH\u LONG.show();
返回;
}
if(Gender.isEmpty()){
Toast.makeText(getApplicationContext(),“性别不能为空!”,Toast.LENGTH\u LONG.show();
返回;
}
if(DOB.isEmpty()){
Toast.makeText(getApplicationContext(),“DOB不能为空!”,Toast.LENGTH\u LONG.show();
返回;
}
if(Height.isEmpty()){
Toast.makeText(getApplicationContext(),“高度不能为空!”,Toast.LENGTH\u LONG.show();
返回;
}
if(CurrentWeight.isEmpty()){
Toast.makeText(getApplicationContext(),“当前权重不能为空!”,Toast.LENGTH\u LONG.show();
返回;
}
Float CurrentWeightNum=Float.parseFloat(CurrentWeight);
如果(CurrentWeightNum<43.0){
Toast.makeText(getApplicationContext(),“此应用程序不支持您当前的体重”,Toast.LENGTH_LONG.show();
返回;
}
if(TargetWeight.isEmpty()){
Toast.makeText(getApplicationContext(),“目标权重不能为空!”,Toast.LENGTH\u LONG.show();
返回;
}
Float-TargetWeightNum=Float.parseFloat(TargetWeight);
如果(目标权重>93.0){
Toast.makeText(getApplicationContext(),“此应用程序不支持您的目标体重”,Toast.LENGTH_LONG.show();
返回;
}
if(ActivityLevel.isEmpty()){
Toast.makeText(getApplicationContext(),“活动级别不能为空!”,Toast.LENGTH\u LONG.show();
返回;
}
if(WeightDifference.isEmpty()){
Toast.makeText(getApplicationContext(),“重量差不能为空!”,Toast.LENGTH\u LONG.show();
返回;
}
如果(CurrentWeightNum.等于(TargetWeightNum)){
Toast.makeText(getApplicationContext(),“当前和目标权重不能相同!”,Toast.LENGTH_LONG.show();
返回;
}
addUserData(姓名、性别、出生日期、身高、CurrentWeightNum、TargetWeightNum、ActivityLevel、权重差);
Toast.makeText(getApplicationContext(),“您已被保存!”,Toast.LENGTH\u LONG.show();
意向意向=新意向(此,MainActivity.class);
星触觉(意向);
}
}

很抱歉,如果格式不正确,应用程序当前未打开,但是当我删除设置屏幕类中的dbhelper内容时,应用程序会运行,但当我尝试按enter键时,应用程序会崩溃,该键调用adding data方法。我已经调查过这个问题,但看不出哪里出了问题。 谢谢

删除
onCreate(this.getWritableDatabase())来自构造函数。不需要显式地调用它

创建
数据库时调用
OnCreate()
回调
public class SetupScreen extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

private String Name;
private String Gender;
private String Height;
private String CurrentWeight;
private String TargetWeight;
private String DOB;
private String ActivityLevel;
private String WeightDifference;

private Database dbhelper;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_setup_screen);
    Name = "";
    Gender = "";
    Height = "";
    CurrentWeight = "";
    TargetWeight = "";
    DOB = "";
    ActivityLevel = "";
    WeightDifference = "";


    dbhelper = new Database(getApplicationContext());





    Spinner spinnerGender = (Spinner) findViewById(R.id.spinnerGender);

    ArrayAdapter<CharSequence> adapterGender = ArrayAdapter.createFromResource(this,
            R.array.gender, android.R.layout.simple_spinner_item);
    adapterGender.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerGender.setAdapter(adapterGender);

    spinnerGender.setOnItemSelectedListener(this);

    Spinner spinnerActivityLevel = (Spinner) findViewById(R.id.spinnerActivityLevel);

    ArrayAdapter<CharSequence> adapterActivity = ArrayAdapter.createFromResource(this,
            R.array.activity_level, android.R.layout.simple_spinner_item);
    adapterActivity.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerActivityLevel.setAdapter(adapterActivity);

    spinnerActivityLevel.setOnItemSelectedListener(this);

    Spinner spinnerWeightDifference = (Spinner) findViewById(R.id.spinnerWeightDifference);

    ArrayAdapter<CharSequence> adapterWeight = ArrayAdapter.createFromResource(this,
            R.array.weight_difference, android.R.layout.simple_spinner_item);
    adapterWeight.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerWeightDifference.setAdapter(adapterWeight);

    spinnerWeightDifference.setOnItemSelectedListener(this);



}

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    Gender = parent.getItemAtPosition(position).toString();
    ActivityLevel = parent.getItemAtPosition(position).toString();
    WeightDifference = parent.getItemAtPosition(position).toString();
}

public void onNothingSelected(AdapterView<?> parent) {
    // An interface callback
}

public void save(View view) {


    Name = ((EditText)findViewById(R.id.Name)).getText().toString();
    DOB = ((EditText)findViewById(R.id.DOB)).getText().toString();
    Height = ((EditText)findViewById(R.id.Height)).getText().toString();
    CurrentWeight = ((EditText)findViewById(R.id.CurrentWeight)).getText().toString();
    TargetWeight = ((EditText)findViewById(R.id.TargetWeight)).getText().toString();


    if (Name.isEmpty()){
        Toast.makeText(getApplicationContext(), "Name cannot be empty!", Toast.LENGTH_LONG).show();
        return;
    }
    if (Gender.isEmpty()){
        Toast.makeText(getApplicationContext(), "Gender cannot be empty!", Toast.LENGTH_LONG).show();
        return;
    }
    if (DOB.isEmpty()){
        Toast.makeText(getApplicationContext(), "DOB cannot be empty!", Toast.LENGTH_LONG).show();
        return;
    }
    if (Height.isEmpty()){
        Toast.makeText(getApplicationContext(), "Height cannot be empty!", Toast.LENGTH_LONG).show();
        return;
    }
    if (CurrentWeight.isEmpty()){
        Toast.makeText(getApplicationContext(), "Current weight cannot be empty!", Toast.LENGTH_LONG).show();
        return;
    }
    Float CurrentWeightNum = Float.parseFloat(CurrentWeight);
    if (CurrentWeightNum < 43.0){
        Toast.makeText(getApplicationContext(), "Your current weight is not supported by this application", Toast.LENGTH_LONG).show();
        return;
    }

    if (TargetWeight.isEmpty()){
        Toast.makeText(getApplicationContext(), "Target weight cannot be empty!", Toast.LENGTH_LONG).show();
        return;
    }
    Float TargetWeightNum = Float.parseFloat(TargetWeight);
    if (TargetWeightNum > 93.0){
        Toast.makeText(getApplicationContext(), "Your target weight is not supported by this application", Toast.LENGTH_LONG).show();
        return;
    }
    if (ActivityLevel.isEmpty()){
        Toast.makeText(getApplicationContext(), "Activity level cannot be empty!", Toast.LENGTH_LONG).show();
        return;
    }
    if (WeightDifference.isEmpty()){
        Toast.makeText(getApplicationContext(), "Weight difference cannot be empty!", Toast.LENGTH_LONG).show();
        return;
    }

    if (CurrentWeightNum.equals(TargetWeightNum)){
        Toast.makeText(getApplicationContext(), "Current and target weight can not be the same!", Toast.LENGTH_LONG).show();
        return;
    }

    dbhelper.addUserData(Name, Gender, DOB, Height, CurrentWeightNum, TargetWeightNum, ActivityLevel, WeightDifference);

    Toast.makeText(getApplicationContext(), "You have been saved!", Toast.LENGTH_LONG).show();

    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);


}
public void onCreate(SQLiteDatabase db) {
    db.execSQL(SQL_CREATE_USER);
    db.close(); // remove this line
}
onCreate(this.getWritableDatabase());
db.execSQL("DROP TABLE IF EXISTS " + YOUR_TABLE_NAME);
onCreate(db);