Java lang IllegalStateException

时间:2016-05-22 09:32:36

标签: java android xml

我是android新手。我正在尝试进行简单的登录活动,但是当我单击“登录”按钮时出现运行时错误。我想我没有正确获取数据。我已经检查过,并且SQLite中有与PK相对应的数据。

logcat的。

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>

2 个答案:

答案 0 :(得分:2)

在使用之前,您没有初始化db。将其添加到您的onCreate() ..

SQLiteDatabase db = this.getWritableDatabase();

答案 1 :(得分:2)

无所事事。只需在您的XML文件的Button属性中删除

android:onClick

在您的活动类型中: -

LoginButton.setOnClickListener(new OnClickListener(){
@Override
    onClick(){
        LogIn();
      }
});

我也遇到了这个问题。是的,我知道你的过程也没错。但事实是,我只是不知道为什么这个过程不起作用(对我而言)。但是当我以编程方式实现onClick方法时,它就像魅力一样。我希望你也能让你的代码正常工作

并添加onCreate方法

db = getWritableDatabase();