我的程序在最后一输入db.close()语句Logcat就会显示出错,不输入db.close()语句Logcat就没有显示错误,求解老师,哪里错了???
package com.example.lianxi;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
EditText e1,e2,e3;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1=(EditText)findViewById(R.id.e1);
e2=(EditText)findViewById(R.id.e2);
e3=(EditText)findViewById(R.id.e3);
b1=(Button)findViewById(R.id.insert);
final SQLiteDatabase db=openOrCreateDatabase("user2.db", MODE_PRIVATE, null);
db.execSQL("create table if not exists usertb2(_id integer primary key autoincrement,name text not null,sex text not null,age integer not null)");
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String a=e1.getText().toString();
String b=e2.getText().toString();
int c=Integer.parseInt(e3.getText().toString());
ContentValues values=new ContentValues();
values.put("age", c);
values.put("sex", b);
values.put("name", a);
db.insert("usertb2", null, values);
//db.execSQL("insert into userdb1(name,sex,age)values(a,b,11)");
values.clear();
}
});
b3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Cursor c=db.query("usertb2", null, "_id<?",new String[]{"10"}, null, null, "name");
if(c!=null)
{
String[] columns=c.getColumnNames();
while (c.moveToNext()) {
for(String columnName:columns)
{
Log.i("info",c.getString(c.getColumnIndex(columnName)));;
}
}
c.close();
}
}
});
db.close(); //我的程序在这里一输入db.close()语句Logcat就会显示出错,不输入db.close()语句Logcat就没有显示错误????
}
} //其中e1,e2,e3是输入的信息,再插入到数据库的表中,按钮有insert(插入按钮)