Mwafrika Josue Mwafrika Josue - 24 days ago 9
Android Question

java programming language and android development

good evening dear friends, I'm working with SQLite db and android android applications I have a problem with my button when I click on login activity the message "could not execute for android onClick" my code are:

for LoginDatabaseAdapter.java class:

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});
}
}


code for DatabaseHelper.java class:

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);
}
}


code for Main_activity_login.java class:

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);

}

*/

}


code for SignIn.java:

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();
}

}


code for sign_in.xml:

<?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>


code for 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"

/>





Answer Source

In 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"
    />

remove android:onClick="onClick" line. That should be work.