Android SQLite数据库添加数据出错 at android.database.sqlite.SQLiteStatement.releaseAndUnlock

今天试着做了注册页面往本地数据库添加数据的操作,结果出现了

 

at android.database.sqlite.SQLiteStatement.releaseAndUnlock

这个错误,报错空指针异常,经检查发现原来是当执行插入操作是数据库连接已经关闭掉了,仔细检查改正为每次执行插入操作都关闭数据库连接再重新获取连接,改正后代码如下:

package com.example.sharedpreferences;

import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Register extends Activity{
	private static Button button_register_Register;
	private static EditText edit_username_Register,edit_password_Register;
	private String name;
	private String password;
	private SQLiteDatabase sqldb;
	private SQLiteHelper sqlh;
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.register_layout);
		button_register_Register = (Button)findViewById(R.id.Button_register);
		edit_username_Register = (EditText)findViewById(R.id.Edit_username);
		edit_password_Register = (EditText)findViewById(R.id.Edit_password);
		button_register_Register.setOnClickListener(new ButtonListener());
		
		
		Toast.makeText(this, SQLiteHelper.TABLE_NAME, Toast.LENGTH_SHORT).show();
		
		
	}
	public class ButtonListener implements OnClickListener{
		public void onClick(View v) {
			switch (v.getId()){
			case R.id.Button_register:
				sqlh = new SQLiteHelper(Register.this);
				System.out.println("01");
				//获取数据库写入权限.
				sqldb = sqlh.getWritableDatabase();
				System.out.println("02");
				name = edit_username_Register.getText().toString();
				edit_username_Register.setText("");
				password = edit_password_Register.getText().toString();
				edit_password_Register.setText("");
				String insertquery = "INSERT INTO "+SQLiteHelper.TABLE_NAME+" ("+SQLiteHelper.NAME+","+
				SQLiteHelper.PASSWORD+") VALUES ('"+name+"','"+password+"');";
				sqldb.execSQL(insertquery);
				Toast.makeText(Register.this, "Saved success", Toast.LENGTH_SHORT).show();
				sqldb.close();
			}
		}
		
	}
}

修改了一下午,终于完成了,特此记录,以免日后再次出错

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Alexnul/article/details/51558913
文章标签: sqlite android
个人分类: Android
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

Android SQLite数据库添加数据出错 at android.database.sqlite.SQLiteStatement.releaseAndUnlock

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭