Android Studio Sqlite数据库登录错误

时间:2018-01-15 05:28:40

标签: java android database sqlite

我正在制作一个应用程序以扩展我的知识,我遇到了这个问题,当我点击login按钮登录应用程序时,我有一个用户名登录,它给出了错误。我尝试了几件事但到目前为止都没有。这是错误代码我相信数据库查询或onclick方法有问题,任何帮助将不胜感激,谢谢。

E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.bulkbuys.bulkbuysapp, PID: 23940
      android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.bulkbuys.bulkbuysapp.Menu launchParam=MultiScreenLaunchParams { mDisplayId=0 mBaseDisplayId=0 mFlags=0 } }
          at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1844)
          at android.app.Instrumentation.execStartActivity(Instrumentation.java:1531)
          at android.app.Activity.startActivityForResult(Activity.java:4403)
          at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
          at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:67)
          at android.app.Activity.startActivityForResult(Activity.java:4362)
          at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:720)
          at android.app.Activity.startActivity(Activity.java:4686)
          at android.app.Activity.startActivity(Activity.java:4654)
          at com.bulkbuys.bulkbuysapp.MainActivity$1.onClick(MainActivity.java:52)
          at android.view.View.performClick(View.java:6261)
          at android.widget.TextView.performClick(TextView.java:11180)
          at android.view.View$PerformClick.run(View.java:23748)
          at android.os.Handler.handleCallback(Handler.java:751)
          at android.os.Handler.dispatchMessage(Handler.java:95)
          at android.os.Looper.loop(Looper.java:154)
          at android.app.ActivityThread.main(ActivityThread.java:6776)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

MainActivity.java

package com.bulkbuys.bulkbuysapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

    EditText editTextUsername;
    Button buttonLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editTextUsername = (EditText) findViewById(R.id.editText_Username);
        buttonLogin = (Button) findViewById(R.id.Button_Login);
        //create preferences - used to store currently logged in user
    }

    public void login(View view) {
        String username = editTextUsername.getText().toString(); //get Username from editText box
        DBHandler dbHandler = new DBHandler(this, null, null, 7);
        Admin a = dbHandler.findAdmin(username);
        Log.i("Login: ","Trying to login with " + username);

        if (username.equals("rick"))
        {
            Intent intent = new Intent(this, Menu.class);
            startActivity(intent);
        }
        else
        {
            if (a == null) //if T is null no match came back, therefore incorrect username or password.
            {
                Toast.makeText(this, "Login failed - Username Wrong", Toast.LENGTH_SHORT).show();
            }
            else
            {
                Toast.makeText(this, "Login Successful", Toast.LENGTH_SHORT).show();
            }
        }
    }

    public void clear(View view) {
        editTextUsername.setText("");
    }
}

activity_main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        tools:context=".MainActivity"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="828dp"
        android:layout_height="match_parent"
        android:weightSum="1"
        android:orientation="vertical"
        android:baselineAligned="false"
        android:divider="#262626">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="0dp"
            android:layout_marginLeft="0dp"
            android:layout_marginRight="200dp"
            android:layout_marginTop="250dp"
            android:orientation="vertical">


            <EditText
                android:id="@+id/editText_Username"
                android:layout_width="300dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginLeft="0dp"
                android:hint="Username" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="400dp"
            android:layout_weight="0.01"
            android:orientation="vertical">

            <Button
                android:id="@+id/Button_Login"
                android:layout_width="250dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="20dp"
                android:background="@drawable/roundbutton"
                android:onClick="login"
                android:text="Login"
                android:textColor="#ffffff"
                android:textSize="20sp"
                android:textStyle="normal|bold" />

            <Button
                android:id="@+id/button2"
                android:layout_width="250dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="5dp"
                android:background="@drawable/roundbutton"
                android:onClick="clear"
                android:text="clear"
                android:textColor="#ffffff"
                android:textSize="20sp"
                android:textStyle="bold" />
        </LinearLayout>
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

DBHandler

package com.bulkbuys.bulkbuysapp;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;

import java.util.ArrayList; 

public class DBHandler extends SQLiteOpenHelper implements BaseColumns {

    private static final int DATABASE_VERSION = 19;
    private static final String DATABASE_NAME = "GradeDB.db";
    //Declare Tables in Database
    private static final String TABLE_CUSTOMERS = "customers";
    private static final String Admin = "admin";

    public static final String COLUMN_ID = "ID";
    public static final String COLUMN_COMPANYNAME = "Company Name";
    public static final String COLUMN_ADRESS = "Adress";
    public static final String COLUMN_EMAIL = "Email";
    public static final String COLUMN_ABN = "Abn";
    public static final String COLUMN_MOBILENO = "Mobile Number";
    public static final String COLUMN_COMPANYNO = "Company Number";

    public static final String COLUMN_USERNAME = "User Name";


    public DBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        String CREATE_ADMIN_TABLE = "CREATE TABLE " + Admin + "(" +
                COLUMN_USERNAME + " TEXT)  ";
        db.execSQL(CREATE_ADMIN_TABLE);


        String CREATE_CUSTOMERS_TABLE = "CREATE TABLE " + TABLE_CUSTOMERS + "(" +
                COLUMN_ID + " INTEGER PRIMARY KEY, " +
                COLUMN_COMPANYNAME + " TEXT, " +
                COLUMN_ADRESS + " TEXT, " +
                COLUMN_EMAIL + " TEXT, " +
                COLUMN_ABN + " TEXT. " +
                COLUMN_MOBILENO + " TEXT, " +
                COLUMN_COMPANYNO + " TEXT) ";
        db.execSQL(CREATE_CUSTOMERS_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + DBHandler.Admin);
        db.execSQL("DROP TABLE IF EXISTS " + DBHandler.TABLE_CUSTOMERS);

        onCreate(db);
    }

    //search for product
    public Admin findAdmin(String username) {
        String query = "SELECT * FROM " + Admin + " WHERE username = '" +
                username + "'";
        SQLiteDatabase db = this.getWritableDatabase();
        //run query with cursor
        Cursor cursor = db.rawQuery(query, null);
        Admin a = new Admin();

        //move to first existing object
        if (cursor.moveToFirst()) {
            do { //****** This IF loop may be pointless need to mess with...
                if (username.equals(cursor.getString(3))) { //If a username matches one in the DB add info into Teacher object
                    a.setUsername(cursor.getString(0));
                }
            } while (cursor.moveToNext());
            cursor.close();
        }
        else //no match found return a null object
        {
            a = null;
            db.close();
        }

        return a;
    }

    public void addCustomers(Customers customers) {

        ContentValues values = new ContentValues(); // simpler way of inserting into DB
        //ID isn't added as the ID will automatically be assigned
        values.put(COLUMN_ID, customers.getId());
        values.put(COLUMN_COMPANYNAME, customers.getCompanyname());
        values.put(COLUMN_ADRESS, customers.getAdress());
        values.put(COLUMN_EMAIL, customers.getEmail());
        values.put(COLUMN_ABN, customers.getAbn());
        values.put(COLUMN_MOBILENO, customers.getMobileno());
        values.put(COLUMN_COMPANYNO, customers.getCompanyno());

        SQLiteDatabase db = this.getWritableDatabase();

        db.insert(TABLE_CUSTOMERS, null, values);
        db.close();
    }

    public ArrayList<Customers> listCustomers() {
        String query = "SELECT * FROM " + TABLE_CUSTOMERS + " ORDER BY " + COLUMN_ID;
        SQLiteDatabase db = this.getWritableDatabase();
        ArrayList<Customers> list = new ArrayList<>(); //Array to store customer
        Cursor cursor = db.rawQuery(query, null);

        if (cursor.moveToFirst()) {
            do { //run loop through all results and store each customer into arraylist
                Customers p = new Customers();
                p.setId(Integer.parseInt(cursor.getString(0)));
                p.setCompanyname(cursor.getString(1));
                p.setAdress(cursor.getString(2));
                p.setEmail(cursor.getString(3));
                p.setAbn(cursor.getString(4));
                p.setMobileno(cursor.getString(5));
                p.setCompanyno(cursor.getString(6));
                list.add(p);
            } while (cursor.moveToNext());
        }
        cursor.close();
        db.close();
        return list;
    }

    public boolean deleteCustomert(int id) {
        boolean success = false;
        //create query
        String query = "SELECT * FROM " + TABLE_CUSTOMERS + " WHERE " + COLUMN_ID + " = '" + id + "'";
        SQLiteDatabase db = this.getWritableDatabase();
        //run query
        Cursor cursor = db.rawQuery(query, null);
        Customers Customers = new Customers(); //temp
        //move to first object
        if (cursor.moveToFirst()) //if exists delete object
        {
            cursor.moveToFirst();
            Customers.setId(Integer.parseInt(cursor.getString(0)));
            db.delete(TABLE_CUSTOMERS, COLUMN_ID + " = ?", new String[]{String.valueOf(Customers.getId())});
            success = true;
            cursor.close();
        }
        db.close();
        return success;
    }

    public Customers findCustomers(String id) {
        String query = "SELECT * FROM " + TABLE_CUSTOMERS + " WHERE " + COLUMN_ID + " = '" +
                id + "'";
        SQLiteDatabase db = this.getWritableDatabase();
        //run query with cursor
        Cursor cursor = db.rawQuery(query, null);
        Customers p = new Customers();

        return p;
    }
}

Android清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.bulkbuys.bulkbuysapp">

    <supports-screens
        android:largeScreens="true"
        android:normalScreens="true"
        android:smallScreens="true"
        android:xlargeScreens="true" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".CustomerListPage"></activity>
        <activity android:name=".Menu"></activity>

    </application>

</manifest>

我有这个额外的课程,我已经尝试了一些但它没有工作

package com.bulkbuys.bulkbuysapp;

import java.io.Serializable;

public class Admin implements Serializable {

    private String username;

    public Admin() {
    }

    public Admin(String username) {
        this.username = username;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public String toString() {
        return " / Username: " + username;
    }
}

Menu.class

package com.bulkbuys.bulkbuysapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


public class Menu extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_menu);

    Button buttonclick = (Button)findViewById(R.id.button_customer);

    buttonclick.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // use Intent

            Intent intent1= new Intent(Menu.this,CustomerListPage.class);
            startActivity(intent1);
        }
    });

}

}

1 个答案:

答案 0 :(得分:0)

ActivityNotFoundException

  

调用startActivity(Intent)或者一个时抛出此异常   其变体失败,因为无法找到Activity执行   给定的意图。

<强> FYI

 Intent intent = new Intent(this, Menu.class);
            startActivity(intent);

确保菜单 Activity存在与否。

整改声明。使用 , 代替 .

<强>唐&#39;吨

String CREATE_CUSTOMERS_TABLE = "CREATE TABLE " + TABLE_CUSTOMERS + "(" +
        COLUMN_ID + " INTEGER PRIMARY KEY, " +
        COLUMN_COMPANYNAME + " TEXT , " +
        COLUMN_ADRESS + " TEXT , " +
        COLUMN_EMAIL + " TEXT , " +
        COLUMN_ABN + " TEXT. " +
        COLUMN_MOBILENO + " TEXT, " +
        COLUMN_COMPANYNO + " TEXT) ";

<强>不要

    String CREATE_CUSTOMERS_TABLE = "CREATE TABLE " + TABLE_CUSTOMERS + "(" +
        COLUMN_ID + " INTEGER PRIMARY KEY , " +
        COLUMN_COMPANYNAME + " TEXT , " +
        COLUMN_ADRESS + " TEXT , " +
        COLUMN_EMAIL + " TEXT , " +
        COLUMN_ABN + " TEXT , " +
        COLUMN_MOBILENO + " TEXT , " +
        COLUMN_COMPANYNO + " TEXT) ";

修改

  

MainActivity $ 1.onClick(MainActivity.java:52)

public void clear(View view) {
      //  editTextUsername.setText(""); comment setText method
    }