fengyun30boy
不秃丶
2017-10-13 01:22

为什么我点击注册按钮程序就什么都不显示关闭了

  • 登陆验证

package com.example.myapplication;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.CursorIndexOutOfBoundsException;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
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;
import android.widget.Toast;

import java.sql.Date;
import java.sql.SQLException;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity{
private Button buttonLoginIn;
private Button buttonRegister;
private EditText editTextUser;
private EditText editTextPW;
private MyDatabaseHelper dbHelper;
private SQLiteDatabase db;
private Calendar calendar;
private int id = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    buttonLoginIn = (Button) this.findViewById(R.id.button2);
    buttonRegister = (Button) this.findViewById(R.id.button);
    editTextUser = (EditText) this.findViewById(R.id.editText2);
    editTextPW = (EditText) this.findViewById(R.id.editText);

    dbHelper = new MyDatabaseHelper(this, "test.db", null, 1);
    //登录按钮,验证成功则跳转进入用户界面,失败则提示验证错误
    buttonLoginIn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            String userName = editTextUser.getText().toString().trim();
            String password = editTextPW.getText().toString().trim();
            if (login(userName, password) == true) {
                insertLoginRecord(id,userName,getTime());
                Intent intent = new Intent(MainActivity.this, loginPage.class);
                MainActivity.this.startActivity(intent);
            } else {
                Toast.makeText(MainActivity.this, "用户名不存在或密码错误", Toast.LENGTH_SHORT).show();
            }
        }
    });

    //重置按钮,清空用户名密码
    buttonRegister.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            String userName = editTextUser.getText().toString().trim();
            String password = editTextPW.getText().toString().trim();
            if(checkUserInDBorNot(userName)){
                Toast.makeText(MainActivity.this, "用户名已注册", Toast.LENGTH_SHORT).show();
            }else{
                if(register(userName,password) == true){
                    Toast.makeText(MainActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(MainActivity.this, "Something wrong happened!", Toast.LENGTH_SHORT).show();
                }
            }
        }
    });
}

//获取时间
public String getTime(){
    calendar = Calendar.getInstance();
    int year = calendar.get(calendar.YEAR);
    int month = calendar.get(calendar.MONTH);
    int day = calendar.get(calendar.DATE);
    int hour = calendar.get(calendar.HOUR);
    int minute = calendar.get(calendar.MINUTE);
    int second = calendar.get(calendar.SECOND);
    String time = "" + year + "/" + month + "/" + day
            + " " + hour + "-" + minute + "-" + second;
    return time;
}

//向登录记录表插入登录用户名,登录时间
public void insertLoginRecord(int id,String userName,String loginTime){
    db = dbHelper.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put("id",id);
    contentValues.put("userName",userName);
    contentValues.put("loginTime",loginTime);
    db.insert("loginRecord",null,contentValues);
    db.close();
    id++;
}

//登陆验证,返回布尔型
public boolean login(String userName,String password){
    db = dbHelper.getReadableDatabase();
    String sql = "Select * from users where userName = ? and password = ?";

    Cursor cursor = db.rawQuery(sql,new String[]{userName,password});
    if(cursor.getCount() > 0){
        cursor.close();
        db.close();
        return true;
    }
    else {
        cursor.close();
        db.close();
        return false;
    }
}

public boolean checkUserInDBorNot(String userName){
    db = dbHelper.getWritableDatabase();
    String sql = "Select * from users where userName =?";
    Cursor cursor = db.rawQuery(sql,new String[] { userName });

    if (cursor.getCount() > 0){
        cursor.close();
        db.close();
        return true;
    }
    cursor.close();
    db.close();
    return false;
}


public boolean register(String userName,String password){
    db= dbHelper.getWritableDatabase();
    String sql = "insert into users(userName,password) value(?,?)";
    try{
        Cursor cursor = db.rawQuery(sql,new String[]{userName, password});
        cursor.close();
        db.close();
        return true;
    }catch (CursorIndexOutOfBoundsException e){

    }
    //ContentValues values=new ContentValues();
    //values.put("name",username);
   // values.put("password",password);
   // db.insert("userData",null,values);

    return false;
}

}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答