2017-09-24 81 views
-3

好晚上親愛的朋友,我正在使用SQLite數據庫和android的android應用程序我有一個問題,我的按鈕,當我點擊登錄活動消息「無法執行android onClick」我的代碼分別是:java編程語言和android開發

爲LoginDatabaseAdapter.java類:

package com.delaroystudios.bus_seat_booking_system; 

import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 

/** 
* Created by mugenzi israel on 9/19/2017. 
*/ 

public class DataBaseHelper extends SQLiteOpenHelper { 
    public DataBaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version){ 
     super(context,name,factory,version); 
    } 

    private static final String TABLE_NAME = "LOGIN"; 
    static final String DATABASE_CREATE = "CREATE TABLE" + TABLE_NAME + "(" + " ID " + " INTEGER PRIMARY KEY AUTOINCREMENT," + " USERNAME text, PASSWORD text);"; 
    @Override 
    public void onCreate(SQLiteDatabase _db){ 
    _db.execSQL(LoginDataBaseAdapter.DATABASE_CREATE); 
    } 
    @Override 
    public void onUpgrade(SQLiteDatabase _db,int _oldVersion,int _newVersion){ 
    Log.w("TaskDBAdapter","Upgrading from version"+_oldVersion+"to"+_newVersion+ 
      ",which will destroy all old data") ; 
     _db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); 
     onCreate(_db); 
    } 
} 

代碼Main_activ:

package com.delaroystudios.bus_seat_booking_system; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 

/** 
* Created by mugenzi israel on 9/19/2017. 
*/ 

    public class LoginDataBaseAdapter { 
     static final String DATABASE_NAME = "login.db"; 
     static final int DATABASE_VERSION = 1; 
     static final String TABLE_NAME ="LOGIN"; 
     public static final int NAME_COLUMN = 1; 
     static final String DATABASE_CREATE = "CREATE TABLE" + TABLE_NAME + "(" + " ID " + " INTEGER PRIMARY KEY AUTOINCREMENT," + " USERNAME text, PASSWORD text);"; 
    public SQLiteDatabase db; 
    private Context context ; 
    private DataBaseHelper dbHelper; 
    public LoginDataBaseAdapter(Context _context){ 
     Context context; 
     context = _context; 
     dbHelper = new DataBaseHelper(context,DATABASE_NAME,null,DATABASE_VERSION); 
    } 
    public LoginDataBaseAdapter open() throws SQLException{ 
     db = dbHelper.getWritableDatabase(); 
     return this; 
    } 
    public void close(){ 
     db.close(); 
    } 
    public SQLiteDatabase getDatabaseInstance(){ 

     return db; 
    } 
    public void insertEntry(String userName,String password){ 
     ContentValues newValues = new ContentValues(); 
     newValues.put("USERNAME",userName); 
     newValues.put("PASSWORD",password); 
     db.insert("LOGIN",null,newValues); 
    } 
    public int deleteEntry(String UserName){ 
     String where = "USERNAME=?"; 
     int numberOFEntriesDeleted; 
     numberOFEntriesDeleted = db.delete(
       "LOGIN",where, new String[]{ 
         UserName} 
    ); 
     return numberOFEntriesDeleted; 
    } 

    public String getSingleEntry(String userName){ 
    Cursor cursor = db.query("LOGIN",null,"USERNAME=?",new String[]{userName},null,null,null); 
     if(cursor.getCount()<1){ 
      cursor.close(); 
      return "NOT EXIST"; 
     } 
     cursor.moveToFirst(); 
     String password = cursor.getString(cursor.getColumnIndex("PASSWORD")); 
     cursor.close(); 
     return password; 
    } 
    public void updateEntry(String userName,String password){ 
     ContentValues updatedValues = new ContentValues(); 
     updatedValues.put("USERNAME",userName); 
     updatedValues.put("PASSWORD",password); 
     String where = "USERNAME = ?"; 
     db.update("LOGIN",updatedValues,where,new String[]{userName}); 
    } 
} 

爲DatabaseHelper.java類代碼ity_login.java類:用於SignIn.java

package com.delaroystudios.bus_seat_booking_system; 
import android.app.Dialog; 
import android.content.Intent; 
import android.graphics.Color; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Spinner; 
import android.widget.SpinnerAdapter; 
import android.widget.TextView; 
import android.widget.Toast; 


public class Main_activity_login extends AppCompatActivity { 
    Button login; 
    Button sign; 
    LoginDataBaseAdapter loginDataBaseAdapter; 
    @Override 
    protected void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main_login); 
     /* final Button login; 
     final Button sign; 
     LoginDataBaseAdapter loginDataBaseAdapter;*/ 
     login = (Button)findViewById(R.id.log); 
     final Spinner spinner = (Spinner)findViewById(R.id.spinner1); 
     sign = (Button)findViewById(R.id.sgn); 
     loginDataBaseAdapter = new LoginDataBaseAdapter(this); 
     loginDataBaseAdapter = loginDataBaseAdapter.open(); 
     ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.roles_selection,android.R.layout.simple_spinner_item); 
     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     spinner.setAdapter(adapter); 
     spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> parent, View view, int position, long l) { 
       Toast.makeText(getBaseContext(),parent.getItemAtPosition(position)+ "Selected",Toast.LENGTH_LONG).show(); 
       ((TextView) parent.getChildAt(0)).setTextColor(Color.RED);//TO change the color of spinner items 

       final Intent intent; 
       switch (position) 
       { 
        case 1: 
         intent = new Intent(Main_activity_login.this,Bus_info_admin.class); 
         startActivity(intent); 
         break; 
        case 2: 
         intent = new Intent(Main_activity_login.this,pick_ticket.class); 
         startActivity(intent); 
         break; 
        case 3: 
         intent = new Intent(Main_activity_login.this,SignIn.class); 
         startActivity(intent); 
         break; 
        case 4: 
         intent = new Intent(Main_activity_login.this,View_Booking.class); 
         startActivity(intent); 
        default: 
         System.out.println("wrong"); 
       } 
       } 

      @Override 
      public void onNothingSelected(AdapterView<?> parent){ 

      } 
     }); 

     /* login.setOnClickListener(
       new View.OnClickListener(){ 
        @Override 
        public void onClick(View v) { 
         // code for button one click 
         Intent intent = new Intent(Main_activity_login.this,DetailsLogin.class);       startActivity(intent); 

        } 

       } 
     );*/ 
    sign.setOnClickListener(
    new View.OnClickListener(){ 
    @Override 
    public void onClick(View view){ 
     Intent intent = new Intent(Main_activity_login.this,SignIn.class); 
     startActivity(intent); 
    } 
    } 
    ); 

    } 
public void logIn(View v){ 
    final Dialog dialog = new Dialog(Main_activity_login.this); 
    dialog.setContentView(R.layout.activity_main_login);// 
    dialog.setTitle("Login"); 
    final EditText editUserName = (EditText)findViewById(R.id.EditLog); 
    final EditText editPassword = (EditText)findViewById(R.id.Edit_menu2); 
    Button btnLogin = (Button)findViewById(R.id.log); 

    final String userName,password,storedPassword; 

    userName = editUserName.getText().toString(); 
    password = editPassword.getText().toString(); 
    storedPassword = loginDataBaseAdapter.getSingleEntry(userName); 
    btnLogin.setOnClickListener(
      new View.OnClickListener(){ 
      public void onClick(View v){ 

       if (password.equals(storedPassword)){ 
        Toast.makeText(Main_activity_login.this,"You made it!,Login successful",Toast.LENGTH_LONG).show(); 
        dialog.dismiss(); 
        Intent intent = new Intent(Main_activity_login.this,DetailsLogin.class); 
        startActivity(intent); 
       } 
       else { 
        Toast.makeText(Main_activity_login.this,"User name or password does not match",Toast.LENGTH_LONG).show(); 
       } 
      } 
      } 
    ); 

} 
    @Override 
    protected void onDestroy(){ 
     super.onDestroy(); 
     loginDataBaseAdapter.close(); 
    } 

    /*public void startActivity() { 
     Intent intent = new Intent(Main_activity_login.this,Bus_info_admin.class); 
     startActivity(intent); 
    }*/ 
/*public void buttonLoginClick(View v){ 

     Intent intent = new Intent(v.getContext(),DetailsLogin.class); 
     v.getContext().startActivity(intent); 

    } 

    */ 

    } 

代碼:

<?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="com.delaroystudios.bus_seat_booking_system.SignIn"> 
<TextView 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:text="@string/register_here" 
    android:textSize="32sp" 
    android:gravity="center" 
android:layout_marginTop="30dp" 
    /> 
<EditText 
    android:layout_marginTop="40dp" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:hint="@string/user_id" 
    android:id="@+id/id_user" 

    /> 
    <EditText 
     android:layout_marginTop="30dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="@string/full_name" 
     android:id="@+id/id_name" 


     /> 
    <EditText 
     android:layout_marginTop="30dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="@string/phone_no" 
     android:id="@+id/phone" 


     /> 

    <EditText 
     android:id="@+id/email" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="30dp" 
     android:hint="@string/email_id" 
     /> 

    <EditText 
     android:layout_marginTop="30dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="@string/password" 
     android:id="@+id/password" 
     android:layout_marginBottom="20dp" 
     /> 

    <Button 

     android:id="@+id/register" 
     android:layout_width="150dp" 
     android:layout_height="50dp" 
     android:layout_gravity="center" 
     android:text="@string/register" 
     android:onClick="onClick" 
     /> 
</LinearLayout> 

爲activity_main_login.xml代碼:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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="horizontal" 
    tools:context="com.delaroystudios.bus_seat_booking_system.Main_activity_login" 
android:background="@drawable/bus1" 
    > 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:id="@+id/menu1" 
     android:layout_marginBottom="100dp" 
     android:layout_marginTop="20dp" 
     android:gravity="center" 
     android:layout_marginStart="30dp" 
     > 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/home" 
      android:textSize="20sp" 
      android:textColor="#9b59b6" 
      /> 

     <Button 
      android:id="@+id/textView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/about" 
      android:textColor="#9b59b6" 
      android:textSize="20sp" /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/contact" 
      android:textSize="20sp" 
      android:textColor="#9b59b6" 
      /> 
     <!--a verifier --> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:layout_below="@+id/menu1" 
     android:id="@+id/menu2" 
     android:layout_marginBottom="30dp" 
     > 
    <TextView 
     android:layout_width="150dp" 
     android:layout_height="wrap_content" 
     android:text="@string/Login2" 
     android:textSize="28sp" 
android:textColor="#9b59b6" 
     /> 

     <EditText 
      android:id="@+id/EditLog" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:hint="@string/login" 
      android:textColorHint="#9b59b6" 
      android:textColor="#ffffff" 
      /> 
    </LinearLayout> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:layout_below="@+id/menu2" 
     android:id="@+id/menu3" 
     android:layout_marginBottom="30dp" 
     > 

     <TextView 
      android:id="@+id/textView" 
      android:layout_width="150dp" 
      android:layout_height="wrap_content" 
      android:text="@string/Pass" 
      android:textSize="28sp" 
      android:textColor="#9b59b6" 
      /> 

     <EditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:hint="@string/your_password" 
      android:id="@+id/Edit_menu2" 
      android:textColorHint="#9b59b6" 
      android:textColor="#ffffff" 
      /> 
    </LinearLayout> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:layout_below="@+id/menu3" 
     android:layout_marginBottom="10dp" 
     android:id="@+id/role" 
     > 
     <TextView 
      android:id="@+id/T_role" 
      android:layout_width="150dp" 
      android:layout_height="wrap_content" 
      android:text="@string/role" 
      android:textSize="28sp" 
      android:textColor="#9b59b6" 
      /> 
<Spinner 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/spinner1" 

    /> 
    </LinearLayout> 
    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:id="@+id/men1" 
     android:layout_marginTop="70dp" 
     android:layout_below="@+id/role" 
     android:gravity="center" 
     android:layout_marginStart="30dp" 
     > 
    <Button 
     android:id="@+id/log" 
     android:layout_width="150dp" 
     android:layout_height="wrap_content" 
     android:layout_marginEnd="30dp" 
     android:onClick="logIn" 
     android:text="@string/login" 
     android:textColor="#9b59b6" 
     android:textColorHint="#9b59b6" 
     android:textSize="20sp" 

     /> 
    <Button 
     android:id="@+id/sgn" 
     android:layout_width="150dp" 
     android:layout_height="wrap_content" 
     android:layout_marginEnd="30dp" 
     android:onClick="onClick" 
     android:text="@string/sign_in" 
     android:textColor="#9b59b6" 
     android:textColorHint="#9b59b6" 
     android:textSize="20sp" 

     /> 
爲sign_in.xml

package com.delaroystudios.bus_seat_booking_system; 

    import android.app.Activity; 
    import android.content.Context; 
    import android.content.Intent; 
    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 SignIn extends AppCompatActivity { //AppCompatActivity 
     EditText editUserName,editPassword,editConfirmPassword; 
     Button btnCreateAccount,btn; 
     Context context = this; 
     LoginDataBaseAdapter loginDataBaseAdapter; 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_sign_in); 

    loginDataBaseAdapter = new LoginDataBaseAdapter(this); 
    loginDataBaseAdapter = loginDataBaseAdapter.open(); 
    editUserName =(EditText)findViewById(R.id.id_name); 
    editPassword = (EditText)findViewById(R.id.email); 
      editConfirmPassword = (EditText)findViewById(R.id.password); 
      btnCreateAccount = (Button)findViewById(R.id.register) ; 

      btnCreateAccount.setOnClickListener(
        new View.OnClickListener(){ 
      public void onClick(View v){ 
     String userName = editUserName.getText().toString(); 
     String password = editPassword.getText().toString(); 
     String confirmPassword = editConfirmPassword.getText().toString(); 

     if (userName.equals("")||password.equals("")||confirmPassword.equals("")){ 
      Toast.makeText(getApplicationContext(),"Field Vacant",Toast.LENGTH_LONG).show(); 
    return; 
     } 
     if (!password.equals(confirmPassword)){ 
     Toast.makeText(getApplicationContext(),"Password does not match",Toast.LENGTH_LONG).show(); 
     return; 
     } 
     else { 
    loginDataBaseAdapter.insertEntry(userName,password); 
    Toast.makeText(getApplicationContext(),"Account Successfully created",Toast.LENGTH_LONG).show(); 
     Intent i = new Intent(SignIn.this,Main_activity_login.class); 
      startActivity(i); 
      finish(); 
     } 
      } 
     } 
      ); 

     /*btn.setOnClickListener(
     new View.OnClickListener(){ 
     @Override 
     public void onClick(View view){ 
      Intent intent = new Intent(SignIn.this,Main_activity_login.class); 
      startActivity(intent); 
     } 
     );*/ 


     } 
     @Override 
     protected void onDestroy(){ 
      super.onDestroy(); 
      loginDataBaseAdapter.close(); 
     } 

    } 

代碼

+2

1)如果這是一個崩潰,發佈完整的堆棧跟蹤。 2)不要在這裏發佈你的整個應用程序,只發布相關的部分。如果我們需要更多,我們會要求它。 –

+0

感謝您的建議,我把所有的因爲我看到在每個類中的問題可能被發現!謝謝。 –

+0

要明確,您要求我們爲您調試此程序。你有什麼嘗試? – jdv

回答

0

sign_in.xml

<Button 
    android:id="@+id/register" 
    android:layout_width="150dp" 
    android:layout_height="50dp" 
    android:layout_gravity="center" 
    android:text="@string/register" 
    android:onClick="onClick" 
    /> 

除去android:onClick="onClick"線。這應該是工作。

+0

感謝親愛的,我試過,但出現另一個錯誤: –

+0

第二個錯誤是:了java.lang.RuntimeException:無法啓動活動ComponentInfo {com.mugenziisrael.Bus_booking/com.delaroystudios.bus_seat_booking_system.Main_activity_login}:android.database。 sqlite.SQLiteException:鄰近 「TABLELOGIN」:語法錯誤(碼1):在編譯:CREATE TABLELOGIN(ID INTEGER PRIMARY KEY AUTOINCREMENT,USERNAME文本,密碼文本); –

+0

您收到的錯誤是SQLite數據庫錯誤。 TABLE和LOGIN之間沒有空格(而不是TABLELOGIN)。 –