gpt4 book ai didi

Java 语言 IllegalStateException

转载 作者:行者123 更新时间:2023-12-01 18:04:50 27 4
gpt4 key购买 nike

我是安卓新手。我正在尝试进行简单的登录 Activity ,但当我单击“登录”按钮时出现运行时错误。我认为我没有正确获取数据。我已经检查过,SQLite 中有一个与该 PK 相对应的数据。

日志猫。

FATAL EXCEPTION: main
Process: com.example.champ.remindme, PID: 4043
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:289)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
at android.view.View.performClick(View.java:4780) 
at android.view.View$PerformClick.run(View.java:19866) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5254) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.rawQuery(java.lang.String, java.lang.String[])' on a null object reference
at com.example.champ.remindme.Login.LogIn(Login.java:45)
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284) 
at android.view.View.performClick(View.java:4780) 
at android.view.View$PerformClick.run(View.java:19866) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5254) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

java 代码。

package com.example.champ.remindme;
import android.app.AlertDialog;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Login extends AppCompatActivity {
EditText edtUsername, edtPass;
String Username,Password;
int counter=3;
Button LoginButton;
SQLiteDatabase db;
// TextView txtAttempts;
//private final int interval = 3000;
/*private Runnable runnable = new Runnable(){
public void run() {
LoginButton.setEnabled(true);
}
}; */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
LoginButton= (Button)findViewById(R.id.LoginButton);
edtUsername=(EditText)findViewById(R.id.edtitem);
// Username=edtUsername.getText().toString();
edtPass=(EditText)findViewById(R.id.password);
}


public void LogIn(View v){
if((edtUsername.getText().toString().trim().length()==0)||(edtPass.getText().toString().trim().length()==0))
{
showMessage("Error", "Please enter the empty Text Box");
return;
}

Cursor c=db.rawQuery("SELECT * FROM User WHERE Username='"+ edtUsername.getText()+"'", null);
if(c.moveToFirst())
{
Username=c.getString(1);
Password=c.getString(2);

if (edtUsername.getText().toString().equals(Username) && edtPass.getText().toString().equals(Password)){
Toast.makeText(Login.this, "Login Successful", Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, Menu.class);
intent.putExtra("Username",Username);
startActivity(intent);
}
else {
Toast.makeText(Login.this, "Login Failed", Toast.LENGTH_LONG).show();
counter--;
}
// txtAttempts.setText("Attempts Left: " + counter);

if (counter == 0) {
LoginButton.setEnabled(false);
// new Timer().schedule((TimerTask) runnable,interval);
}
}
else
{
showMessage("Error", "Invalid Username");
}
/*-----------------------------------------------------------------*/

}

public void Signup(View v) {
Intent intent = new Intent(this, SignUp.class);
startActivity(intent);
}

public void showMessage(String title,String message)
{
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}

}

XML 代码。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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="com.example.champ.remindme.Login"
android:background="@drawable/back">


<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView2"
android:src="@drawable/remind_me_logo"
android:contentDescription="Logo" />
<EditText
android:id="@+id/edtitem"
android:layout_width="265dp"
android:layout_height="wrap_content"
android:padding="10dip"
android:background="@drawable/username_rounded_edited_text"
android:inputType="text"
android:hint="Username"
android:textAlignment="center"
android:layout_marginTop="37dp"
android:layout_below="@+id/imageView2"
android:layout_centerHorizontal="true" />
<EditText
android:id="@+id/password"
android:layout_width="265dp"
android:layout_height="wrap_content"
android:padding="10dip"
android:background="@drawable/pass_rounded_edited_text"
android:inputType="text"
android:hint="Password"
android:textAlignment="center"

android:layout_below="@+id/edtitem"
android:layout_centerHorizontal="true" />

<Button
android:id="@+id/LoginButton"
android:background="@drawable/blue_botton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/login"
style="@style/ButtonText"
android:onClick="LogIn"
android:layout_below="@+id/password"
android:layout_alignLeft="@+id/password"
android:layout_alignStart="@+id/password"
android:layout_alignRight="@+id/password"
android:layout_alignEnd="@+id/password" />
<Button
android:id="@+id/txtSignup"
android:background="@drawable/blue_botton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Signup"
style="@style/ButtonText"
android:onClick="Signup"
android:layout_below="@+id/LoginButton"
android:layout_alignLeft="@+id/LoginButton"
android:layout_alignStart="@+id/LoginButton" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView3"
android:src="@drawable/fb_loging"
android:layout_alignBottom="@+id/txtSignup"
android:layout_toRightOf="@+id/txtSignup"
android:layout_toEndOf="@+id/txtSignup" />
</RelativeLayout>

最佳答案

您在使用数据库之前没有初始化它。将其添加到您的 onCreate()..

SQLiteDatabase db = this.getWritableDatabase();

关于Java 语言 IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37372622/

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