求助各路大神
此处有一个登录验证的小程序,里面有个登录按钮设有监听,按钮监听里有一个验证函数,第一次跑程序,验证涵数没问题但如果第一次验证失败,当第二次触发监听后,验证数就会莫名其妙的跳过去,(我想可能和验证函数第一次返回NULL有关),如过真是这样,我该怎么样解决
监听位置位于LoadActivity.class中的 bt_load.setOnclickListener()内
//登录函数 LoadActivity
package cn.edu.nuc.skid_menu;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.content.DialogInterface;
import java.util.List;
import cn.edu.nuc.skid_menu.sqlite_utils.utils;
/**
* Created by Administrator on 2017/6/9.
*/
public class LoadActivity extends Activity{
private EditText met_name;
private EditText met_pw;
private Button bt_load;
private Button bt_register;
private cn.edu.nuc.skid_menu.sqlite_utils.utils utils;
private List list;
@Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_load);
met_name = (EditText) findViewById(R.id.et_name);
met_pw = (EditText) findViewById(R.id.et_pw);
bt_load = (Button) findViewById(R.id.bt_load);
bt_register = (Button) findViewById(R.id.bt_register);
utils = new utils(LoadActivity.this);
//String s= utils.execDate("delete from db_user where _id = ? ",new Object[]{"wsy123"});//增删改数据
//Log.i("wsy", s+"");
utils.insert("wsy123","123456","male","运汽集团");//插入数据库
// utils.db.delete("db_user","_id = ?",new String[]{"wsy"});//删除数据
//utils.update();//更改数据
list = utils.selectAll();
utils.show_list(list);//显示数据库
Log.i("wsy", "分开!");
//String i ="wsy12345";
//List list1 = utils.select(i);
//utils.show_list(list1); //显示_id为wsy12345的信息
// list = utils.select(met_name.getText().toString());
//!!!!!!!!就是这个监听!!!!!!!!!!!!!!!!!!
bt_load.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("wsy", "触发登录监听");
String checkResult=checkInfo();
//!!!!!!!二次验证上面的函数checkInfo会被跳过!!!!!!!
Log.i("wsy", "完成验证");
list = utils.select(met_name.getText().toString());
if(checkResult==null){
Intent intent=new Intent(LoadActivity.this,Main_activity.class);
startActivity(intent);
Log.i("wsy", "发送成功");
}
else{
Log.i("wsy", "发送失败");
AlertDialog.Builder builder=new AlertDialog.Builder(LoadActivity.this);
builder.setTitle("错误提示");
builder.setMessage(checkResult);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
met_pw.setText("");
met_name.setText("");
}
});
builder.create().show();
}
}
});
bt_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(LoadActivity.this,RegisterActivity.class);
startActivity(intent);
}
});
}
private String checkInfo(){
if(met_name.getText().toString()==null||met_name.getText().toString().equals("")){
Log.i("wsy", "用户名不能为空!");
return "用户名不能为空";
}
if(met_pw.getText().toString().trim().length()<6||met_pw.getText().toString().equals("")||met_pw.getText().toString().trim().length()>15){
Log.i("wsy", "密码长度为6-15位");
return "密码长度为6-15位";
}
if(utils.select(met_name.getText().toString()).size()==0)
{
Log.i("wsy", "账号不存在");
return "账号不存在";
}
if(!met_pw.getText().toString().trim().equals(utils.test_list(list).toString().trim()))
{ Log.i("wsy", "密码错误");
return "密码错误";}
Log.i("wsy", "可以登录");
return null;
}
}
//数据库管理工具 utils.
package cn.edu.nuc.skid_menu.sqlite_utils;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.Cursor;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class utils extends SQLiteOpenHelper {
SQLiteDatabase db = null;
private final static String DBNAME = "db_user";
private final static int VERSION = 1;
public utils(Context context) {
super(context, DBNAME, null, VERSION);
db = this.getReadableDatabase();
}
public Cursor selectCursor(String sql, String[] selectionArgs) {
return db.rawQuery(sql, selectionArgs);
}
public int selectCount(String sql, String[] selectionArgs) {
Cursor cursor = db.rawQuery(sql, selectionArgs);
int result = cursor.getCount();
if (cursor != null) {
cursor.close();
}
return result;
}
public List<Map<String, Object>> selectAll() {
Log.i("wsy", "小成 1");
Cursor cursor = db.rawQuery("select * from db_user limit ?,? ", new String[]{"0", "999"});
Log.i("wsy", "小成2 ");
return cursorToList(cursor);
}
public List<Map<String, Object>> select(String i) {
Log.i("wsy", "成1");
String[] strings = {i};
Cursor cursor = db.query("db_user",new String[]{"_id","password","sex","school_name"},"_id =?",strings,null,null,"_id desc","0,5");
Log.i("wsy", "成2 ");
return cursorToList(cursor);
}
public List<Map<String, Object>> cursorToList(Cursor cursor) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
while (cursor.moveToNext()) {
Log.i("wsy", "有数据 ");
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < cursor.getColumnCount(); i++) {
map.put(cursor.getColumnName(i), cursor.getString(i));
}
list.add(map);
}
return list;
}
public String execDate(String sql, Object[] bindArgs) {
try {
db.execSQL(sql, bindArgs);
return "成功";
} catch (Exception e) {
return e.getMessage();
}
}
public void destroy() {
if (db != null) {
db.close();
}
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS db_user (_id VARCHAR(10) PRIMARY KEY ,password,sex,school_name)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (newVersion > oldVersion) {
db.execSQL("DROP TABLE IF EXISTS db_users");
onCreate(db);
}
}
public void show_list(List list) {
Map map = null;
String netMode1 = null;
String netMode2 = null;
String netMode3 = null;
String netMode4 = null;
for (int i = 0; i < list.size(); i++) {
map = (HashMap) list.get(i);
netMode1 = (String) map.get("_id");
if (netMode1 == null) {
Log.i("wsy", "空了");
} else {
Log.i("wsy", "_id:" + netMode1);
}
netMode2 = (String) map.get("password");
if (netMode2 == null) {
Log.i("wsy", "空了");
} else {
Log.i("wsy", "password:" + netMode2);
}
netMode3 = (String) map.get("sex");
if (netMode3 == null) {
Log.i("wsy", "空了");
} else {
Log.i("wsy", "sex:" + netMode3);
}
netMode4 = (String) map.get("school_name");
if (netMode4 == null) {
Log.i("wsy", "空了");
} else {
Log.i("wsy", "school_name:" + netMode4);
}
}
}
public void insert(String s1,String s2,String s3,String s4) {
ContentValues values = new ContentValues();
values.put("_id", s1);
values.put("password", s2);
values.put("sex", s3);
values.put("school_name", s4);
long rowId = db.insert("db_user", null, values);
}
public void update() {
ContentValues values = new ContentValues();
values.put("_id", "wsy");
values.put("password", "a123a123");
values.put("sex", "female");
values.put("school_name", "运气集团");
int result = db.update("db_user", values, "_id=?", new String[]{"wsy"});
}
public SQLiteDatabase get_db(){
return db;
}
public String test_list(List list){
Map map = null;
String netMode2 = null;
map = (HashMap) list.get(0);
netMode2 = (String) map.get("password");
return netMode2;
}
}
//登录成功后的界面activity
public class Main_activity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
Intent intent = getIntent();
}
}
//登录页面 activity_load.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="cn.edu.nuc.skid_menu.LoadActivity"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="驾考通"
android:textSize="40sp"
android:layout_gravity="center"
android:textColor="#00cc00"
android:layout_marginTop="120dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="一考就通"
android:textSize="25sp"
android:layout_marginRight="15dp"
android:layout_gravity="right"
android:textColor="#CC0000"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号 : "
android:textSize="24sp"
android:textColor="#000000"
android:layout_marginLeft="15dp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:id="@+id/et_name"
android:hint="请输入账号"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码 : "
android:textSize="24sp"
android:textColor="#000000"
android:layout_marginLeft="15dp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:id="@+id/et_pw"
android:hint="请输入密码"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
android:textColor="#000000"
android:textSize="20sp"
android:layout_marginTop="15dp"
android:id="@+id/bt_load"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"
android:textColor="#000000"
android:textSize="20sp"
android:layout_marginTop="15dp"
android:id="@+id/bt_register"/>
</LinearLayout>
</LinearLayout>
//登录成功后的界面 Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#6699ff"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="王绍阳,欢迎您"
android:textSize="30sp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="顺序刷题"
android:layout_marginTop="40dp"
android:textSize="30sp"
android:id="@+id/bt_question"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="错题攻克"
android:layout_marginTop="20dp"
android:textSize="30sp"
android:id="@+id/bt_dif_question"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改信息"
android:layout_marginTop="20dp"
android:textSize="30sp"
android:id="@+id/bt_revise"
/>
</LinearLayout>