-3

There goes the code . I just cant figure out what is the problem. I am just taking the email and password as input from user and trying to store into a table named stu. The application crashes on clicking the sign in button which redirects to a next page using "intent" and inserting values into a table. It would be of great help if problem could be identified that cause app to crash . Thanks

Database Helper.java

package com.example.zain.smd1;



import android.content.ContentValues;

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





public class DatabaseHelper extends SQLiteOpenHelper {

public static final String DB_NAME = "student.db";
public static final String Table_name = "stu";
public static final String COL1 = "Id";
public static final String COL2 = "EMAIL";
public static final String COL3 = "PASS";
private static final String PASS = "PASS";
private static final String EMAIL = "EMAIL";

public DatabaseHelper(Context context) {
    super(context,DB_NAME,null,1);
}

@Override
public void onCreate(SQLiteDatabase db) {


    db.execSQL("create table " + Table_name + " (Id INTEGER PRIMARY KEY AUTOINCREMENT, EMAIL TEXT , PASS TEXT)");


}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS "+Table_name);
    onCreate(db);
}
public boolean insertdata(String email , String pass){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues content = new ContentValues();

    content.put(EMAIL,email);
    content.put(PASS,pass);
    long result = db.insert(Table_name,null,content);
    if (result==-1){
        return false;


    }
    else
        return true;

}
}

Main Activity.java

package com.example.zain.smd1;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.preference.PreferenceManager;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
EditText et1,et2;
String email,pass;
RelativeLayout r1;
SharedPreferences sp;
DatabaseHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     et1= (EditText) findViewById(R.id.eTemail);

     et2 = (EditText) findViewById(R.id.eTpassword);

    r1 = (RelativeLayout) findViewById(R.id.colorChange);
}

 public void login(View view){

     email = et1.getText().toString();
     pass = et2.getText().toString();
     if(email.equals("admin")&& pass.equals("admin")){
         Toast.makeText(getApplicationContext(),
                 "Redirecting...",Toast.LENGTH_SHORT).show();
         r1.setBackgroundColor(Color.RED);
          /*sp= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
         SharedPreferences.Editor e= sp.edit();
         e.putString("name",email);
         e.putString("password",pass);
         e.apply();*/
         boolean ischecked = db.insertdata(et1.getText().toString(),et2.getText().toString());
         if(ischecked==true) {

             Toast.makeText(getApplicationContext(),
                     "Added",Toast.LENGTH_SHORT).show();
             Intent intent = new Intent(this, Main2Activity.class);

             startActivity(intent);
         }
     }
     else {
         r1.setBackgroundColor(Color.GREEN);
         Toast.makeText(getApplicationContext(),
                 "Invalid password or email",Toast.LENGTH_SHORT).show();


     }

    /*
     String name=sp.getString("name","");
     String pass= sp.getString("password","");
     et1.setText(name);
     et2.setText(pass);
     */
     }



     }

Application crashes on inserting into SQLite database . I just couldn't figure out whats wrong

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Zain Afzal
  • 45
  • 1
  • 8

2 Answers2

0

Try to uninstall the existing application from your Debugging device and then install it again. Sometimes when you change the structure of your table after installing it and again you are installing then it doesn't create new table.

Ghulam Moinul Quadir
  • 1,638
  • 1
  • 12
  • 17
0

you should try this...

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

     //add this line in your code..
     db = new DatabaseHelper(this);

     et1= (EditText) findViewById(R.id.eTemail);

     et2 = (EditText) findViewById(R.id.eTpassword);

    r1 = (RelativeLayout) findViewById(R.id.colorChange);
}
vivek
  • 36
  • 2