Java 单击登录时出现学校项目错误(用户名和密码)

Java 单击登录时出现学校项目错误(用户名和密码),java,android,sqlite,Java,Android,Sqlite,我有一个小的学校项目;初始部分将要求输入用户名和密码。当我通过输入用户名和密码进行测试,然后时钟登录时,它会立即关闭而不发出警告。我没有看到错误 这是我的mainactivity.java package com.example.stengel.dbtest; import android.database.Cursor; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import andr

我有一个小的学校项目;初始部分将要求输入用户名和密码。当我通过输入用户名和密码进行测试,然后时钟登录时,它会立即关闭而不发出警告。我没有看到错误

这是我的mainactivity.java

package com.example.stengel.dbtest;

import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


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

public void btnLogin_onClick(View oView){

        TextView txtUsername;
        String sUsername;
        TextView txtPassword;
        String sPassword;
        boolean bUserFound = false;
        DBManager oDBManager = new DBManager(this);

        txtUsername = (TextView)findViewById(R.id.edTxtUserName);
        sUsername = txtUsername.getText().toString();
        txtPassword = (TextView)findViewById(R.id.edTxtPassword);
        sPassword = txtPassword.getText().toString();


        oDBManager.open();
        Cursor oDBCursor = oDBManager.getAllContacts();

        if(oDBCursor.moveToFirst()){
            do{
                if(oDBCursor.getString(1).equalsIgnoreCase(sUsername)){

                    bUserFound = true;

                    if(oDBCursor.getString(2).equalsIgnoreCase(sPassword)){
                        Toast.makeText(this,"Successful login!",Toast.LENGTH_SHORT).show();
                    }
                    else{
                        Toast.makeText(this,"Password is wrong!",Toast.LENGTH_SHORT).show();
                    }
                }
            }while(oDBCursor.moveToNext());

            if(!bUserFound){
                Toast.makeText(this,"User not found!",Toast.LENGTH_SHORT).show();
            }
        }
        else{
            Toast.makeText(this,"No users in DB!",Toast.LENGTH_SHORT).show();
        }

        oDBManager.close();
    }

// If creating a new account enable (make visible) icons and colors
public void btnCreateAccount_onClick(View oView) {

    View bView = findViewById(R.id.hScrollView);
    View bView2 = findViewById(R.id.hScrollView2);
    // -- set both to visible - possibly do same for Create Button and disable Create Account button
    bView.setVisibility(bView.VISIBLE);
    bView2.setVisibility(bView.VISIBLE);
}

// create account and add username, password, icon, and color.
// will need to add icon and color options to database
public void btnCreateNewAccount_onClick(View oView){
    DBManager oDBManager = new DBManager(this);

    oDBManager.open();
    TextView txtUsername = (TextView)findViewById(R.id.edTxtUserName);
    TextView txtPassword = (TextView)findViewById(R.id.edTxtUserName);
    //increase size of database and add icon and colors
    oDBManager.insertContact(txtUsername.getText().toString(), txtPassword.getText().toString());
    //// make sure text fields are cleared now (username and password fields)
    oDBManager.close();
}//create account method
}
这是DBManager.java


看起来它在“oDBManager.open();”行的MainActivity中爆炸了

有什么想法吗?

改变

static final String DATABASE_TABLE = "users";

您已使用表名“users”而不是“contacts”创建表名。

问题是您正在创建表
用户

create table IF NOT EXISTS users (_id integer primary key  autoincrement, "
                + "username text not null, password text not null);
现在,在插入操作期间,您正在添加

db.insert(DATABASE_TABLE, null, initialValues); 
没有列
密码
用户名


因此,请正确更改表名。

对于表中的错误和混乱,我深表歉意。修复了这个问题后,我仍然无法在“oDBManager.open();”停止应用程序

我决定清理、重建并使用一个新的API构建,而不是25,我使用的是21,它运行良好。我回到25岁,它失败了。因此,此时不必担心原因,只需知道使用早期版本进行清理和重建是一个有效的测试


谢谢

您可以登录Android Monitor stracktrace。放在这里。这是说列密码不在表中:E/SQLiteLog:(1)表联系人没有名为password的列E/SQLiteDatabase:Error inserting password=password username=Ron android.database.sqlite.SQLiteException:表联系人没有名为password的列(代码1):,编译时:INSERT-INTO contacts(密码、用户名)值(?,)位于
db.insert(DATABASE_TABLE, null, initialValues);