-1

I want user to create account before log in by clicking sign up text, and it will proceed to register page. After user register account, they may use the account to log in the system.

Can someone help me to fix it? My problem is I can't register account to login. Application will not responding when I click "submit" button in register activity. I not sure what problem in my coding. I am newbie in android java coding. Thanks for helping.

MainActivity.Java

public class MainActivity extends Activity {

DBHandler dbhandler;

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

    final EditText txtUserName = (EditText)findViewById(R.id.editTextUsername);  
    final EditText txtPassword = (EditText)findViewById(R.id.editTextPassword);  
    Button btnLogin = (Button)findViewById(R.id.button_login);  

    dbhandler = new DBHandler(this);
    dbhandler.open();

    btnLogin.setOnClickListener(new OnClickListener(){  

        @Override  
        public void onClick(View v) {  
            String username = txtUserName.getText().toString();  
            String password = txtPassword.getText().toString();  
            try{  
                if(username.length() >= 0 && password.length() >= 0)  
                {  
                    DBHandler dbUser = new DBHandler(MainActivity.this);  
                    dbUser.open();  

                    if(dbUser.Login(username, password))  
                    {  
                        Toast.makeText(MainActivity.this,"Successfully Logged In", Toast.LENGTH_LONG).show();
                        setContentView(R.layout.fragment_home);
                    }else{  
                        Toast.makeText(MainActivity.this,"Invalid Username/Password", Toast.LENGTH_LONG).show();  
                    }  
                    dbUser.close();  
                }  

            }catch(Exception e)  
            {  
                Toast.makeText(MainActivity.this,e.getMessage(), Toast.LENGTH_LONG).show();  
            }  
        }  
    });

    TextView location = (TextView) findViewById(R.id.signUp);
      location.setMovementMethod(LinkMovementMethod.getInstance());
      Spannable spans = (Spannable) location.getText();
      ClickableSpan clickSpan = new ClickableSpan() {

         @Override
         public void onClick(View arg0)
         {
             setContentView(R.layout.activity_register);
         }
      };
      spans.setSpan(clickSpan, 0, spans.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

      TextView location2 = (TextView) findViewById(R.id.forgetPassword);
      location2.setMovementMethod(LinkMovementMethod.getInstance());
      Spannable spans2 = (Spannable) location2.getText();
      ClickableSpan clickSpan2 = new ClickableSpan() {

         @Override
         public void onClick(View widget)
         {
             setContentView(R.layout.fragment_home);
         }
      };
      spans2.setSpan(clickSpan2, 0, spans2.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}

Register.Java

public class Register extends Activity {

DBHandler dbhandler;

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

    dbhandler = new DBHandler(this);
    dbhandler.open();
    }

public void registerbtn(View v){
    final EditText txtUsername = (EditText)findViewById(R.id.username);  
    final EditText txtPassword = (EditText)findViewById(R.id.password);
    final EditText txtName = (EditText)findViewById(R.id.name);  
    final EditText txtMatricNumber = (EditText)findViewById(R.id.matric_number);
    final EditText txtEmail = (EditText)findViewById(R.id.email);  
    final EditText txtPhoneNumber = (EditText)findViewById(R.id.phone_number);
    final EditText txtAddress = (EditText)findViewById(R.id.address);   

    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(txtUsername.getWindowToken(), 0);
    imm.hideSoftInputFromWindow(txtPassword.getWindowToken(), 0);
    imm.hideSoftInputFromWindow(txtName.getWindowToken(), 0);
    imm.hideSoftInputFromWindow(txtMatricNumber.getWindowToken(), 0);
    imm.hideSoftInputFromWindow(txtEmail.getWindowToken(), 0);
    imm.hideSoftInputFromWindow(txtPhoneNumber.getWindowToken(), 0);
    imm.hideSoftInputFromWindow(txtAddress.getWindowToken(), 0);
    try {
        String username = txtUsername.getText().toString();
        String password = txtPassword.getText().toString();
        String name = txtName.getText().toString();
        String matric_number = txtMatricNumber.getText().toString();
        String email = txtEmail.getText().toString();
        String phone_number = txtPhoneNumber.getText().toString();
        String address = txtAddress.getText().toString();
        long i = dbhandler.AddUser(username, password, name, matric_number, email, phone_number, address);
        if(i != -1)
        Toast.makeText(Register.this, "You have successfully Key in your information",Toast.LENGTH_LONG).show();
        setContentView(R.layout.activity_main);
        } catch (SQLException e) {
        Toast.makeText(Register.this, "Some problem occurred",
        Toast.LENGTH_LONG).show();

    }

}
}

This is my activity_register.xml file register button part

<EditText
    android:id="@+id/password"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView2"
    android:layout_alignLeft="@+id/username"
    android:layout_alignParentRight="true"
    android:ems="10"
    android:inputType="textPassword" />

<EditText
    android:id="@+id/username"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView1"
    android:layout_alignBottom="@+id/textView1"
    android:layout_alignParentRight="true"
    android:ems="10" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/textView1"
    android:layout_below="@+id/username"
    android:layout_marginTop="34dp"
    android:text="Password:"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView3"
    android:layout_below="@+id/textView3"
    android:layout_marginTop="22dp"
    android:text="Matric No:"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView4"
    android:layout_below="@+id/textView7"
    android:layout_marginTop="61dp"
    android:text="Address"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView3"
    android:layout_alignLeft="@+id/password"
    android:ems="10"
    android:inputType="textPersonName" />

<EditText
    android:id="@+id/email"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView4"
    android:layout_alignLeft="@+id/name"
    android:ems="10"
    android:inputType="textEmailAddress" />

<TextView
    android:id="@+id/textView7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView5"
    android:layout_below="@+id/textView5"
    android:layout_marginTop="22dp"
    android:text="E-mail:"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_below="@+id/textView7"
    android:layout_marginTop="18dp"
    android:text="Phone No:"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/address"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView6"
    android:layout_alignLeft="@+id/email"
    android:ems="10"
    android:inputType="textPostalAddress" />

<TextView
    android:id="@+id/textView9"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/username"
    android:layout_centerHorizontal="true"
    android:text="Login Information"
    android:textColor="#81F7BE" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="46dp"
    android:text="Username:"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_below="@+id/textView2"
    android:layout_marginTop="51dp"
    android:text="Full Name:"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView8"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/name"
    android:layout_centerHorizontal="true"
    android:text="User Infomation"
    android:textColor="#81F7BE" />

<Button
    android:id="@+id/button_register"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView9"
    android:layout_alignRight="@+id/textView9"
    android:layout_below="@+id/address"
    android:layout_marginTop="18dp"
    android:onClick="registerbtn"
    android:text="Submit" />

<EditText
    android:id="@+id/matric_number"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView7"
    android:layout_alignLeft="@+id/textView8"
    android:ems="10"
    android:inputType="number" />

<EditText
    android:id="@+id/phone_number"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView4"
    android:layout_alignLeft="@+id/matric_number"
    android:ems="10"
    android:inputType="phone" />

DBHandler.java

public class DBHandler {

public static final String KEY_ROWID = "_id";  
public static final String KEY_USERNAME= "username";  
public static final String KEY_PASSWORD = "password";
public static final String KEY_NAME= "name";  
public static final String KEY_MATRIC_NUMBER = "matric_number"; 
public static final String KEY_EMAIL= "email";  
public static final String KEY_PHONE_NUMBER = "phone_number"; 
public static final String KEY_ADDRESS = "address";  
private static final String TAG = "DBHandler";  

private static final String DATABASE_NAME = "usersdb";  
private static final String DATABASE_TABLE = "users";  
private static final int DATABASE_VERSION = 1;  

private static final String DATABASE_CREATE =  
    "create table users (_id integer primary key autoincrement, "  
    + "username text not null, "  
    + "password text not null,"
    + "name text not null,"
    + "matric_number text not null, "  
    + "email text not null,"
    + "phone_numbe text not null, "  
    + "address text not null);";  

private Context context = null;  
private DatabaseHelper DBHelper;  
private SQLiteDatabase db;  

public DBHandler(Context ctx)  
{  
    this.context = ctx;  
    DBHelper = new DatabaseHelper(context);  
}  

private static class DatabaseHelper extends SQLiteOpenHelper  
{  
    DatabaseHelper(Context context)  
    {  
        super(context, DATABASE_NAME, null, DATABASE_VERSION);  
    }  

    @Override  
    public void onCreate(SQLiteDatabase db)  
    {  
        db.execSQL(DATABASE_CREATE);  
    }  

    @Override  
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)  
    {  
        Log.w(TAG, "Upgrading database from version " + oldVersion  
                + " to "  
                + newVersion + ", which will destroy all old data");  
        db.execSQL("DROP TABLE IF EXISTS users");  
        onCreate(db);  
    }  
}      

public void open() throws SQLException  
{  
    db = DBHelper.getWritableDatabase();  
}  

public void close()  
{  
    DBHelper.close();  
}      

public long AddUser(String username, String password, String name, String matric_number, String email, String phone_number, String address)  
{  
     ContentValues initialValues = new ContentValues();  
     initialValues.put(KEY_USERNAME, username);  
     initialValues.put(KEY_PASSWORD, password);
     initialValues.put(KEY_NAME, name);  
     initialValues.put(KEY_MATRIC_NUMBER, matric_number);  
     initialValues.put(KEY_EMAIL, email);  
     initialValues.put(KEY_PHONE_NUMBER, phone_number);  
     initialValues.put(KEY_ADDRESS, address);  
     return db.insert(DATABASE_TABLE, null, initialValues);  

} 

public boolean Login(String username, String password) throws SQLException  
{  
    Cursor mCursor = db.rawQuery("SELECT * FROM " + DATABASE_TABLE + " WHERE username=? AND password=?", new String[]{username,password});  
    if (mCursor != null) {  
        if(mCursor.getCount() > 0)  
        {  
            return true;  
        }  
    }  
 return false;  
}  

}

DBHelper.java

public class DBHelper  extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "membersdb";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE = "CREATE TABLE  (_id integer primary key autoincrement,username text not null,password text not null);";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);

}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
db.execSQL("DROP TABLE IF EXISTS members");
onCreate(db);

}

}

When I click "submit" button, Logcat error show below

11-29 13:16:48.590: E/AndroidRuntime(798): FATAL EXCEPTION: main
11-29 13:16:48.590: E/AndroidRuntime(798): Process: com.example.myrecordapps, PID: 798
11-29 13:16:48.590: E/AndroidRuntime(798): java.lang.IllegalStateException: Could not find a method registerbtn(View) in the activity class com.example.myrecordapps.MainActivity for onClick handler on view class android.widget.Button with id 'button_register'
11-29 13:16:48.590: E/AndroidRuntime(798):  at android.view.View$1.onClick(View.java:3810)
11-29 13:16:48.590: E/AndroidRuntime(798):  at android.view.View.performClick(View.java:4438)
11-29 13:16:48.590: E/AndroidRuntime(798):  at android.view.View$PerformClick.run(View.java:18422)
11-29 13:16:48.590: E/AndroidRuntime(798):  at android.os.Handler.handleCallback(Handler.java:733)
11-29 13:16:48.590: E/AndroidRuntime(798):  at android.os.Handler.dispatchMessage(Handler.java:95)
11-29 13:16:48.590: E/AndroidRuntime(798):  at android.os.Looper.loop(Looper.java:136)
11-29 13:16:48.590: E/AndroidRuntime(798):  at android.app.ActivityThread.main(ActivityThread.java:5017)
11-29 13:16:48.590: E/AndroidRuntime(798):  at java.lang.reflect.Method.invokeNative(Native Method)
11-29 13:16:48.590: E/AndroidRuntime(798):  at java.lang.reflect.Method.invoke(Method.java:515)
11-29 13:16:48.590: E/AndroidRuntime(798):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-29 13:16:48.590: E/AndroidRuntime(798):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-29 13:16:48.590: E/AndroidRuntime(798):  at dalvik.system.NativeStart.main(Native Method)
11-29 13:16:48.590: E/AndroidRuntime(798): Caused by: java.lang.NoSuchMethodException: onClick [class android.view.View]
11-29 13:16:48.590: E/AndroidRuntime(798):  at java.lang.Class.getConstructorOrMethod(Class.java:472)
11-29 13:16:48.590: E/AndroidRuntime(798):  at java.lang.Class.getMethod(Class.java:857)
11-29 13:16:48.590: E/AndroidRuntime(798):  at android.view.View$1.onClick(View.java:3803)
Kelvin Hii
  • 21
  • 1
  • 2
  • 5

1 Answers1

0

Its a guess, I thing your using android:onClick=""; field in your xml layout of register button and there is no function created in java page for that, So remove the field in layout and try. If you want to use the android:onClick="somefunction"; in your code you shoud create a function in java your activity which same as in the onclick.Refer this for more details.

Eg: Activity

 public void mySampleMethod(View v) {
        // does something 
    } 

Button layout:

<?xml version="1.0" encoding="utf-8"?>
<!-- layout elements -->
<Button android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="SUBMIT"
    android:onClick="mySampleMethod" />
Community
  • 1
  • 1
Remees M Syde
  • 2,564
  • 1
  • 19
  • 42
  • if I remove the android:onClick, then the submit button directly no more function, when I click it, it will nothing respond. – Kelvin Hii Nov 29 '14 at 18:50
  • I had change my coding in Register.java shown in post, but still have error below: 11-29 14:31:47.323: E/AndroidRuntime(1119): java.lang.IllegalStateException: Could not find a method registerbtn(View) in the activity class com.example.myrecordapps.MainActivity for onClick handler on view class android.widget.Button with id 'button_register' – Kelvin Hii Nov 29 '14 at 19:35
  • your "registerbtn" function should be out side oncreate, eg: oncreate(){ } registerbtn(View view){ } got me? – Remees M Syde Nov 29 '14 at 19:58
  • Do you mean in side or out side? Now I am putting it out side already. – Kelvin Hii Nov 30 '14 at 03:59