• 1000 идеи?
Код из класса входа
package com.example.laivumusis;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Login extends AppCompatActivity {
EditText username, password;
Button login, register;
DatabaseLogin databaseLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
username = findViewById(R.id.username);
password = findViewById(R.id.password);
login = findViewById(R.id.login);
register = findViewById(R.id.register);
databaseLogin = new DatabaseLogin(this);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String usernameValue = username.getText().toString(); //???????
String passwordValue = password.getText().toString();
if(databaseLogin.isLoginValid(usernameValue, passwordValue)){ //should go to Home activity but crashes to main
Intent intent = new Intent(Login.this, HomeActivity.class);
startActivity(intent);
Toast.makeText(Login.this, "Login Successful!", Toast.LENGTH_SHORT).show(); //Pop up
}
else{
Toast.makeText(Login.this, "Invalid username or password", Toast.LENGTH_SHORT).show();
}
}
});
register.setOnClickListener(new View.OnClickListener() { //goes to register tab
@Override
public void onClick(View v) {
Intent intent = new Intent(Login.this, Register.class);
startActivity(intent);
}
});
}
}
ошибка отладки:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.example.laivumusis.Login$1.onClick(Login.java:34)
Добавлены вопросительные знаки в строку, где показана проблема
Добавляя xml код myab ie, вы видите проблему: (
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Login">
<TextView
android:text="Login form"
android:textSize="24sp"
android:gravity="center"
android:background="#971919"
android:textColor="#fff"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/Username"
android:layout_gravity="center"
android:hint="Username"
android:layout_width="match_parent"
android:maxLines="1"
android:layout_height="wrap_content" />
<EditText
android:maxLines="1"
android:id="@+id/password"
android:inputType="textPassword"
android:layout_gravity="center"
android:hint="Password"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="24dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/login"
android:text="Login"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp" />
<Button
android:id="@+id/register"
android:text="Register"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp" />
</LinearLayout>
</LinearLayout>
Изменяет R., разве не удаляет еще одну проблему из отладчика
E/SQLiteLog: (1) near "''": syntax error in "Select count (*) from user where username=' 'and password''"
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.laivumusis, PID: 21418
android.database.sqlite.SQLiteException: near "''": syntax error (code 1 SQLITE_ERROR): , while compiling: Select count (*) from user where username=' 'and password''
Код базы данных:
package com.example.laivumusis;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import androidx.annotation.Nullable;
public class DatabaseLogin extends SQLiteOpenHelper {
static String name = "database";
static int version = 1;
String createTableUser = "CREATE TABLE if not exists \"User\" (\n" + //per DB Browseri sukurem lentele
"\t\"ID\"\tINTEGER,\n" +
"\t\"Username\"\tTEXT,\n" +
"\t\"Password\"\tTEXT,\n" +
"\t\"Email\"\tTEXT,\n" +
"\t\"Country\"\tTEXT,\n" +
"\t\"Dob\"\tTEXT,\n" +
"\t\"Gender\"\tTEXT,\n" +
"\tPRIMARY KEY(\"ID\" AUTOINCREMENT)\n" +
")";
public DatabaseLogin(@Nullable Context context) {
super(context, name, null, version);
getWritableDatabase().execSQL(createTableUser);
}
public void insertUser(ContentValues contentValues){
getWritableDatabase().insert("user", "", contentValues);
}
public boolean isLoginValid(String username, String password){
String sql= "Select count (*) from user where username=' " +username+ "'and password'" +password+"'";
SQLiteStatement statment = getReadableDatabase().compileStatement(sql);
long l = statment.simpleQueryForLong();
statment.close();
if (l==1) {
return true;
}
else{
return false;
}
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}