简体   繁体   中英

Clicking on a button closes app running on emulator

I am creating a login for android mobile app, but for some reasons that I don't know, when I click on the login button it crashes the app. Is there something wrong with my codes that I cannot see? I have looked at the codes line after line but I cannot find any thing abnormal. See codes below

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Login extends AppCompatActivity implements View.OnClickListener {
Button _login_button;
EditText _AbovePNDUsername, _AbovePNDPassword;
SQLiteDatabase db;
SQLiteOpenHelper openHelper;
Cursor cursor;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    openHelper = new RegisterUserinDatabase(this);
    db = openHelper.getReadableDatabase();
    _login_button = (Button) findViewById(R.id.login_button);
    _AbovePNDUsername = (EditText) findViewById(R.id.AbovePNDUsername);
    _AbovePNDPassword = (EditText) findViewById(R.id.AbovePNDPassword);
    _login_button.setOnClickListener(this);

}

@Override
public void onClick(View v) {


    if (v.getId() == R.id.login_button) {

        String username = _AbovePNDUsername.getText().toString();
        String password = _AbovePNDPassword.getText().toString();
        cursor = db.rawQuery("SELECT * FROM " + RegisterUserinDatabase.TABLE_NAME + "WHERE"
                + RegisterUserinDatabase.COLUMN_USERNAME + "=? AND" + RegisterUserinDatabase.COLUMN_PASSWORD
                + "=?", new String [] {username, password});
        if(cursor != null)
        {
            if(cursor.getCount()> 0){
                cursor.moveToNext();

                Toast.makeText(getApplicationContext(), "login successfully", Toast.LENGTH_LONG).show();
                startActivity(new Intent(this, add_treatment.class));
            }
            else {

                Toast.makeText(getApplicationContext(), "error in login", Toast.LENGTH_LONG).show();
            }

        }

    }
}

}

Your query is wrong:

"SELECT * FROM " + RegisterUserinDatabase.TABLE_NAME + "WHERE"

Unless your TABLE_NAME ends with space, this will produce

SELECT * FROM TABLEWHERE

Which is of course incorrect SQL

This should work:

cursor = db.rawQuery("SELECT * FROM " + RegisterUserinDatabase.TABLE_NAME + " WHERE "
                + RegisterUserinDatabase.COLUMN_USERNAME + "=? AND " + RegisterUserinDatabase.COLUMN_PASSWORD
                + "=?", new String [] {username, password});

You should leave spaces beside the keywords (WHERE, AND) of your SQLite query.

cursor = db.rawQuery("SELECT * FROM " + RegisterUserinDatabase.TABLE_NAME + " WHERE "
                + RegisterUserinDatabase.COLUMN_USERNAME + "=? AND " + RegisterUserinDatabase.COLUMN_PASSWORD
                + "=?", new String [] {username, password});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM