2 wei smile Wei_Smile 于 2016.02.19 17:17 提问

使用Android ORMlite框架时候出现异常 5C

图片说明
主要界面如下图片说明
想要向数据库中存入一条数据图片说明
BEAN类

package com.newtouch.bean;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

@DatabaseTable(tableName="tb_info")
public class User {

@DatabaseField(generatedId=true)
private int id;
@DatabaseField(columnName="name")
private String name;
@DatabaseField(columnName="pwd")
private String pwd;
public User(){}
public User(String name,String pwd){
    this.name=name;
    this.pwd=pwd;
}
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getPwd() {
    return pwd;
}
public void setPwd(String pwd) {
    this.pwd = pwd;
}

}


package com.newtouch.DAO;

import java.sql.SQLException;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import com.newtouch.bean.User;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

public class DataBaseHelper extends OrmLiteSqliteOpenHelper{

private static final String TABLE_NAME = "sqlite-test.db";
private Dao<User, Integer> userDao;  
public DataBaseHelper(Context context) {
    super(context, TABLE_NAME, null, 2);
}
@Override
public void onCreate(SQLiteDatabase arg0, ConnectionSource arg1) {
    try {
        TableUtils.createTable(arg1, User.class);
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
@Override
public void onUpgrade(SQLiteDatabase arg0, ConnectionSource arg1, int arg2, int arg3) {
    try {
        TableUtils.dropTable(arg1, User.class, true);
        onCreate(arg0, arg1);
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
private static DataBaseHelper instance;

public static synchronized DataBaseHelper getHelper(Context context)  
{  
    if (instance == null)  
    {  
        synchronized (DataBaseHelper.class)  
        {  
            if (instance == null)  
                instance = new DataBaseHelper(context);  
        }  
    }  

    return instance;
}
 /** 
 * 获得userDao 
 *  
 *  */
public Dao<User, Integer> getUserDao() throws SQLException  
{  
    if (userDao == null)  
    {  
        userDao = getDao(User.class);  
    }  
    return userDao;  
}  
@Override
public void close() {
    super.close();
    userDao=null;
}

}


package com.lintao.testdemo;

import java.sql.SQLException;

import ormLiteDatabaseHelper.DataBaseHelper;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.lintao.bean.User;

public class Fragment1 extends Fragment {
private EditText edit_save;
private Button btn_save;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment1, container, false);
    edit_save = (EditText) view.findViewById(R.id.edit_save);
    btn_save = (Button) view.findViewById(R.id.btn_save);
    btn_save.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            String name=edit_save.getText().toString();
            User u=new User(name);
            DataBaseHelper helper=DataBaseHelper.getHelper(getActivity());
            try {
                helper.getUserDao().create(u);
                edit_save.setText("");
                Toast.makeText(getActivity(), "信息已插入", Toast.LENGTH_SHORT).show();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    });
    return view;
}
 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

}


1个回答

luochoudan
luochoudan   Ds   Rxr 2016.02.19 18:15

很明显,没有找到orm封装的DataBaseHelper,卤煮太粗心了,用自己的DataBaseHelper继承一下orm的就行了。

Wei_Smile
Wei_Smile 你好,能说的详细点嘛,毕竟刚接触很多东西还不懂,谢了
接近 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!