登录/标识 - 无法解决

时间:2018-03-03 17:22:03

标签: java android sqlite android-sqlite

嘿伙计们我不知道该怎么办。每当我点击注册按钮时应用程序崩溃 - 与helper.insertContact(c)有一些东西; (RegisterActivity)但我不知道是什么。

我认为这两个类很重要:

DatabaseHelper:

package com.example.rd1000_ptlgtv0pkvbg.barcode_app;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by Julius on 03.03.2018.
 */

public class DatabaseHelper extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "contacts.db";
    private static final String TABLE_NAME = "contacts";
    private static final String COLUMN_ID = "id";
    private static final String COLUMN_NAME = "name";
    private static final String COLUMN_USERNAME = "username";
    private static final String COLUMN_PASSWORD = "password";
    SQLiteDatabase db;
    private static final String TABLE_CREATE = "create table contacts (id integer primary key not null ," +
            "name text not null, username text not null, password text not null);";

    public DatabaseHelper(Context context){
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
    db.execSQL(TABLE_CREATE);
    this.db = db;
    }

    public void insertContact(Contact c){
        db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        //UNIQUE ID
        String query = "select * from contacts";
        Cursor cursor = db.rawQuery(query, null);
        int count = cursor.getCount();

        values.put(COLUMN_ID, count);
        values.put(COLUMN_NAME, c.getName());
        values.put(COLUMN_USERNAME, c.getUsername());
        values.put(COLUMN_PASSWORD, c.getPassword());

        db.insert(TABLE_NAME, null, values);
        db.close();
    }
    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    String query = "DROP TABLE IF EXISTS " + TABLE_NAME;
    db.execSQL(query);
    this.onCreate(db);
    }
    public String searchPassword(String username){
    db = this.getReadableDatabase();
    String query = "select username, password from " + TABLE_NAME;
        Cursor cursor = db.rawQuery(query, null);
        String a, b;
        b = "not found";
        if(cursor.moveToFirst()){
            do{
                a = cursor.getString(1);

                if(a.equals(username)){
                    b = cursor.getString(0);
                    break;
                }
            }
            while(cursor.moveToNext());
        }
        return b;

    }

}

注册活动:

package com.example.rd1000_ptlgtv0pkvbg.barcode_app;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.List;

public class RegisterActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);


       final Button bRegister = (Button) findViewById(R.id.bRegister);


        final DatabaseHelper helper = new DatabaseHelper(this);

        bRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final EditText etName = (EditText) findViewById(R.id.etName);
                final EditText etPassword = (EditText) findViewById(R.id.etPassword);
                final EditText etUsername = (EditText) findViewById(R.id.etUsername);
                final EditText etRegisterKey = (EditText) findViewById(R.id.etRegisterKey);

                final String name = etName.getText().toString();
                final String password = etPassword.getText().toString();
                final String username = etUsername.getText().toString();
                final String registerkey = etRegisterKey.getText().toString();


                if(registerkey.equals("ABI2018")){
                //insert the details in database

                    Contact c = new Contact();
                    c.setName(name);
                    c.setUsername(username);
                    c.setPassword(password);
                    helper.insertContact(c);
                    Toast.makeText(getApplicationContext(), "Erfolgreich Registriert",Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(RegisterActivity.this,MainActivity.class);
                    startActivity(intent);
                }else{
                    Toast.makeText(getApplicationContext(), "Wrong RegisterKey",Toast.LENGTH_LONG).show();
                }

            }
        });}}

0 个答案:

没有答案