星火源码 2021-10-30 20:09 采纳率: 100%
浏览 264
已结题

安卓使用sqlite创建数据库 闪退

这是DBHelper.java


package cn.itcast.userregist;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import static android.R.attr.tag;


/**
 * Created by Ohlaxinyu on 2021/10/30.
 */

public class DBHelper extends SQLiteOpenHelper {
    public DBHelper(Context context) {

        super(context, "ac.db", null, 1);
        Log.v("提示","数据库正在创建");
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        //要使用游标适配器,SQLite表格必须包含一栏“_id”
        db.execSQL( "create table shop(_id integer primary key autoincrement,name VARCHAR(64), life VARCHAR(64),name VARCHAR(64),gongji VARCHAR(64),sudu VARCHAR(64))");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }
}


这是MainActivity.java


package cn.itcast.userregist;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

import pl.com.salsoft.sqlitestudioremote.SQLiteStudioService;

public class MainActivity extends AppCompatActivity {
    private EditText et_password;
    private Button btn_send;
    private EditText et_name;
    private CheckBox rb_2;
    private SharedPreferences sharedPreferences;
    private SimpleCursorAdapter adapter;// 简单的游标适配器
    private  DBHelper dbHelper;
    private SQLiteDatabase database; //用于创建数据库对象

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_name = (EditText) findViewById(R.id.et_name);
        et_password = (EditText) findViewById(R.id.et_password);
        btn_send = (Button) findViewById(R.id.btn_send);
        rb_2=(CheckBox)  findViewById(R.id.rb_2);
        btn_send = (Button) findViewById(R.id.btn_send);
        dbHelper = new DBHelper(this.getApplicationContext());

      //下面这句创建数据库后添加后 真机上就会显示app has stoped

        database = dbHelper.getReadableDatabase();
        //点击开始游戏按钮进行数据传递
        btn_send.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                passDate();
            }
        });
        et_name.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String res = s.toString();
                String res2 = "";
                String res3="";
                SharedPreferences sharedPreferences = MainActivity.this.getSharedPreferences("Myshard",MainActivity.this.MODE_PRIVATE);
//                获取数据因为不需要对数据进行更改,所以不需要Editor对象,可以用sharedPreferences进行获取
                res2 = sharedPreferences.getString("usename","duibi");
                if(res.equals(res2)){
                    res3 = sharedPreferences.getString("usepas","duibi");
                    et_password.setText(res3);
                    Toast toast =  Toast.makeText(getBaseContext(),"检测到用户:"+res2,Toast.LENGTH_LONG);
                    toast.show();
                }else {
                  et_password.setText("");
                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    //传递数据
    public void passDate() {
        //创建Intent对象,启动Activity02
        Intent intent = new Intent(this, ShowActivity.class);
        //将数据存入Intent对象
        intent.putExtra("name", et_name.getText().toString().trim());
        intent.putExtra("password", et_password.getText().toString().trim());
        startActivity(intent);

    if(rb_2.isChecked()){
        SharedPreferences sharedPreferences = MainActivity.this.getSharedPreferences("Myshard",MainActivity.this.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("usepas",et_password.getText().toString());
        editor.putString("usename",et_name.getText().toString());
        editor.commit();
    }else {
        et_password.setText("");
        SharedPreferences sharedPreferences = MainActivity.this.getSharedPreferences("Myshard",MainActivity.this.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("usepas","no");
        editor.putString("usename","no");
        editor.commit();
    }
        et_password.setText("");
        et_name.setText("");

    }
    public void listenInput(){


        };

    }






  • 写回答

1条回答 默认 最新

      报告相同问题?

      问题事件

      • 系统已结题 11月21日
      • 已采纳回答 11月13日
      • 创建了问题 10月30日

      悬赏问题

      • ¥15 用mysql做一个高校工资管理系统
      • ¥21 Multisim设计用三端集成稳压器设计稳压电路
      • ¥15 tp6,在iframe里的网址通过获取url参数失败
      • ¥15 打开opendaylight查看拓扑,为什么直接就显示了两个Switch,在mininet配置了一个topo,这两个Switch还在
      • ¥15 无法远程连接pgsql
      • ¥15 建立DLNM模型时显示基础基矩阵不一致
      • ¥15 贝叶斯重复测量方差分析
      • ¥15 c#五子棋为什么悔棋后全部的棋子都没了 但是数组中的棋子还在只是悔棋后剩余的棋子在窗口没显示
      • ¥15 c#如何将到毫秒的时间字符串转换成byte[8]
      • ¥100 能不能帮帮我,这个数据库我一直做不好