gpt4 book ai didi

java - 崩溃到主 Activity 屏幕

转载 作者:行者123 更新时间:2023-12-01 16:53:26 24 4
gpt4 key购买 nike

我从我创建的数据库登录,当我尝试登录时,屏幕应该转到 HomeActivity,但看起来它崩溃回到 mainActivity 尝试更改位置仍然崩溃,myabie有人有什么想法吗?

来自登录类的代码

    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 代码 myabie 你会看到问题:(

    <?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.id,但不会从调试器中删除另一个问题

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) {

}
}

最佳答案

此问题是您添加了错误的引用。这里的R.id.username需要改为R.id.Username [findViewById(R.id.username)]

关于java - 崩溃到主 Activity 屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61638315/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com