简体   繁体   中英

Cannot retrieve the data when trying to login the system (Android Studio)

I'm begginer in Android programming, I'm trying to make the login function. This system will be accessed by two users which are admin and users. The admin just need to enter the "admin" for the username and password to access the system. However, the users needs to register their account before login the system.

I'm trying to find the errors but still don't know the error occurrence. Please help me to solve the errors. Thank You.

DatabaseOperations.java

public Cursor getInformation(DatabaseOperations dob)
{
    SQLiteDatabase database=dob.getReadableDatabase();
    String[] columns={TableInfo.USERNAME,TableInfo.USERPASS};
    Cursor cr=database.query(TableInfo.TABLE_NAME,columns,null,null,null,null,null);
    return cr;
}

MainActivity.java

public void onClick(View view)
Toast.makeText(getBaseContext(), "Please wait....", Toast.LENGTH_LONG).show();
    txtusername = Username.getText().toString();
    txtpassword = Password.getText().toString();
    DatabaseOperations dop = new DatabaseOperations(this, null, null, 1);
    Cursor CR = dop.getInformation(dop);
    CR.moveToFirst();
    boolean login_status = false;
    String NAME = "";
    if (txtusername.equals("Admin") && (txtpassword).equals("Admin")) {
        Intent in = new Intent(MainActivity.this, AdminMenu.class);
        startActivity(in);
    } else {
        do {
            if (txtusername.equals(CR.getString(0)) && (txtpassword.equals(CR.getString(1)))) {
                login_status = true;
                NAME = CR.getString(0);
            }
        } while (CR.moveToNext());
        if (login_status) {
            Toast.makeText(getBaseContext(), "Login Success....\n Welcome " + NAME, Toast.LENGTH_LONG).show();
            Intent i=new Intent(MainActivity.this,UserMenu.class);
            Bundle bundle=new Bundle();
            bundle.putString("username",NAME);
            i.putExtras(bundle);
            startActivity(i);
            finish();
        } else {
            Toast.makeText(getBaseContext(), "Login Failed...", Toast.LENGTH_LONG).show();
            Intent i = new Intent();
            Bundle bundle=new Bundle();
            bundle.putString("username",NAME);
            i.putExtras(bundle);
            startActivity(i);
            finish();
        }
     }
}

This is the error...

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.google.mobile_flexi_parking, PID: 6777
              java.lang.IllegalStateException: Could not execute method for android:onClick
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                  at android.view.View.performClick(View.java:5198)
                  at android.view.View$PerformClick.run(View.java:21147)
                  at android.os.Handler.handleCallback(Handler.java:739)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:5417)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
               Caused by: java.lang.reflect.InvocationTargetException
                  at java.lang.reflect.Method.invoke(Native Method)
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                  at android.view.View.performClick(View.java:5198) 
                  at android.view.View$PerformClick.run(View.java:21147) 
                  at android.os.Handler.handleCallback(Handler.java:739) 
                  at android.os.Handler.dispatchMessage(Handler.java:95) 
                  at android.os.Looper.loop(Looper.java:148) 
                  at android.app.ActivityThread.main(ActivityThread.java:5417) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
               Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { (has extras) }
                  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1798)
                  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1512)
                  at android.app.Activity.startActivityForResult(Activity.java:3917)
                  at android.app.Activity.startActivityForResult(Activity.java:3877)
                  at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:842)
                  at android.app.Activity.startActivity(Activity.java:4200)
                  at android.app.Activity.startActivity(Activity.java:4168)
                  at com.google.mobile_flexi_parking.MainActivity.onclickLogin(MainActivity.java:67)
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                  at android.view.View.performClick(View.java:5198) 
                  at android.view.View$PerformClick.run(View.java:21147) 
                  at android.os.Handler.handleCallback(Handler.java:739) 
                  at android.os.Handler.dispatchMessage(Handler.java:95) 
                  at android.os.Looper.loop(Looper.java:148) 
                  at android.app.ActivityThread.main(ActivityThread.java:5417) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

In your else part you have blank intent which doesn't point any activity

Intent i = new Intent();

I think this is causing error.

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