Sofari Agali

Sofari Agali

  • 1.4k
  • 275
  • 24.7k

App crash on button click

Sep 27 2017 7:25 AM
Hi everybody!
Can someone help me to see more clearly when I click on the Login button, the android studio emulator shows me this: App has stopped, open again,
here are my codes:
 
Databasehelper: 
  1. public class DatabaseHelper extends SQLiteOpenHelper {  
  2.   
  3.     private static final int DATABASE_VERSION=1;  
  4.     private static final String DATABASE_NAME="Donsang.db";  
  5.     private static final String TABLE_NAME="Inscription";  
  6.     private static final String COLUMN_ID="id";  
  7.     private static final String COLUMN_PSEUDO="pseudo";  
  8.     private static final String COLUMN_NAME="nom";  
  9.     private static final String COLUMN_PHONE="phone";  
  10.     private static final String COLUMN_NAISSANCE="naissance";  
  11.     //private static final String COLUMN_VILLE="ville";  
  12.     private static final String COLUMN_DATEDON="datedon";  
  13.    // private static final String COLUMN_SEXE="sexe";  
  14.     private static final String COLUMN_IMAGE="image";  
  15.    // private static final String COLUMN_GROUPE="groupe";  
  16.      SQLiteDatabase db;  
  17.   
  18.     private static final String TABLE_CREATE="create table Inscription (id integer primary key not null auto_increment,"+  
  19.             "pseudo text not null,nom text not null,phone text not null,naissance text not null" +  
  20.             "datedon text not null,image Blob";  
  21.   
  22.   
  23.       public DatabaseHelper(Context context)  
  24.       {  
  25.           super(context,DATABASE_NAME,null,DATABASE_VERSION);  
  26.   
  27.       }  
  28.   
  29.     @Override  
  30.     public void onCreate(SQLiteDatabase db) {  
  31.   
  32.   
  33.                 db.execSQL(TABLE_CREATE);  
  34.         this.db=db;  
  35.   
  36.     }  
  37.   
  38.     @Override  
  39.     public void onUpgrade(SQLiteDatabase db, int i, int i1) {  
  40.         String query="DROP TABLE IF EXISTS " + TABLE_NAME;  
  41.         db.execSQL(query);  
  42.         this.onCreate(db);  
  43.   
  44.     }  
  45.   
  46.     public boolean insertData(String pseudo,String nom,String phone,String naissance,String datedon,  
  47.                               String image)  
  48.     {  
  49.   
  50.         SQLiteDatabase db=this.getWritableDatabase();  
  51.         ContentValues contentValues=new ContentValues();  
  52.         contentValues.put(COLUMN_PSEUDO,pseudo);  
  53.         contentValues.put(COLUMN_NAME,nom);  
  54.         contentValues.put(COLUMN_PHONE,phone);  
  55.         contentValues.put(COLUMN_NAISSANCE,naissance);  
  56.        // contentValues.put(COLUMN_VILLE,ville);  
  57.         contentValues.put(COLUMN_DATEDON,datedon);  
  58.       // contentValues.put(COLUMN_SEXE,sexe);  
  59.         contentValues.put(COLUMN_IMAGE,image);  
  60.        // contentValues.put(COLUMN_GROUPE,groupe);  
  61.       long result=  db.insert(TABLE_NAME,null,contentValues);  
  62.         if(result==-1)  
  63.             return false;  
  64.         else  
  65.             return true;  
  66.     }  
  67.   
  68.   
  69.     public List<Inscription> getAll()  
  70.     {  
  71.         String[] columns = {  
  72.                 COLUMN_PSEUDO,  
  73.                 COLUMN_NAME,  
  74.                 COLUMN_PHONE,  
  75.                 COLUMN_NAISSANCE,  
  76.                 COLUMN_DATEDON,  
  77.                 COLUMN_IMAGE  
  78.         };  
  79.   
  80.         String sortOrder =  
  81.                 COLUMN_PSEUDO + " ASC";  
  82.         List<Inscription> inscritList = new ArrayList<Inscription>();  
  83.   
  84.         SQLiteDatabase db = this.getReadableDatabase();  
  85.   
  86.         Cursor cursor = db.query(TABLE_NAME, //Table to query  
  87.                 columns,    //columns to return  
  88.                 null,        //columns for the WHERE clause  
  89.                 null,        //The values for the WHERE clause  
  90.                 null,       //group the rows  
  91.                 null,       //filter by row groups  
  92.                 sortOrder);  
  93.   
  94.         if (cursor.moveToFirst()) {  
  95.             do {  
  96.                 Inscription user = new Inscription();  
  97.                 user.setId(Integer.parseInt(cursor.getString(cursor.getColumnIndex(COLUMN_ID))));  
  98.                 user.setPseudo(cursor.getString(cursor.getColumnIndex(COLUMN_PSEUDO)));  
  99.                 user.setNom(cursor.getString(cursor.getColumnIndex(COLUMN_NAME)));  
  100.                 user.setPhone(cursor.getInt(cursor.getColumnIndex(COLUMN_PHONE)));  
  101.                 user.setNaissance(cursor.getString(cursor.getColumnIndex(COLUMN_NAISSANCE)));  
  102.                 user.setDate(cursor.getString(cursor.getColumnIndex(COLUMN_DATEDON)));  
  103.                 user.setImage(cursor.getBlob(cursor.getColumnIndex(COLUMN_IMAGE)));  
  104.                 // Adding user record to list  
  105.                 inscritList.add(user);  
  106.             } while (cursor.moveToNext());  
  107.         }  
  108.         cursor.close();  
  109.         db.close();  
  110.   
  111.         // return user list  
  112.         return inscritList;  
  113.     }  
  114.   
  115.     public void updateInscription(Inscription inscris) {  
  116.         SQLiteDatabase db = this.getWritableDatabase();  
  117.   
  118.         ContentValues values = new ContentValues();  
  119.         values.put(COLUMN_PSEUDO, inscris.getPseudo());  
  120.         values.put(COLUMN_NAME, inscris.getNom());  
  121.         values.put(COLUMN_PHONE, inscris.getPhone());  
  122.         values.put(COLUMN_NAISSANCE, inscris.getNaissance());  
  123.         values.put(COLUMN_DATEDON, inscris.getDate());  
  124.         values.put(COLUMN_IMAGE, inscris.getImage());  
  125.   
  126.   
  127.         // updating row  
  128.         db.update(TABLE_NAME, values, COLUMN_ID + " = ?",  
  129.                 new String[]{String.valueOf(inscris.getId())});  
  130.         db.close();  
  131.     }  
  132.   
  133.     public void deleteUser(Inscription user) {  
  134.         SQLiteDatabase db = this.getWritableDatabase();  
  135.         // delete user record by id  
  136.         db.delete(TABLE_NAME, COLUMN_ID + " = ?",  
  137.                 new String[]{String.valueOf(user.getId())});  
  138.         db.close();  
  139.     }  
  140.   
  141.     public boolean checkInscris(String phone) {  
  142.   
  143.         // array of columns to fetch  
  144.         String[] columns = {  
  145.                 COLUMN_ID  
  146.         };  
  147.         SQLiteDatabase db = this.getReadableDatabase();  
  148.   
  149.         // selection criteria  
  150.         String selection = COLUMN_PHONE + " = ?";  
  151.   
  152.         // selection argument  
  153.         String[] selectionArgs = {phone};  
  154.   
  155.         // query user table with condition  
  156.         /** 
  157.          * Here query function is used to fetch records from user table this function works like we use sql query. 
  158.          * SQL query equivalent to this query function is 
  159.          * SELECT user_id FROM user WHERE user_email = '[email protected]'; 
  160.          */  
  161.         Cursor cursor = db.query(TABLE_NAME, //Table to query  
  162.                 columns,                    //columns to return  
  163.                 selection,                  //columns for the WHERE clause  
  164.                 selectionArgs,              //The values for the WHERE clause  
  165.                 null,                       //group the rows  
  166.                 null,                      //filter by row groups  
  167.                 null);                      //The sort order  
  168.         int cursorCount = cursor.getCount();  
  169.         cursor.close();  
  170.         db.close();  
  171.   
  172.         if (cursorCount > 0) {  
  173.             return true;  
  174.         }  
  175.   
  176.         return false;  
  177.     }  
  178.   
  179.     public boolean checkInscris(String pseudo, String phone) {  
  180.   
  181.         // array of columns to fetch  
  182.         String[] columns = {  
  183.                 COLUMN_ID  
  184.         };  
  185.         SQLiteDatabase db = this.getReadableDatabase();  
  186.         // selection criteria  
  187.         String selection = COLUMN_PSEUDO + " = ?" + " AND " + COLUMN_PHONE + " = ?";  
  188.   
  189.         // selection arguments  
  190.         String[] selectionArgs = {pseudo, phone};  
  191.   
  192.         // query user table with conditions  
  193.         /** 
  194.          * Here query function is used to fetch records from user table this function works like we use sql query. 
  195.          * SQL query equivalent to this query function is 
  196.          * SELECT user_id FROM user WHERE user_email = '[email protected]' AND user_password = 'qwerty'; 
  197.          */  
  198.         Cursor cursor = db.query(TABLE_NAME, //Table to query  
  199.                 columns,                    //columns to return  
  200.                 selection,                  //columns for the WHERE clause  
  201.                 selectionArgs,              //The values for the WHERE clause  
  202.                 null,                       //group the rows  
  203.                 null,                       //filter by row groups  
  204.                 null);                      //The sort order  
  205.   
  206.         int cursorCount = cursor.getCount();  
  207.         cursor.close();  
  208.   
  209.         if (cursorCount > 0) {  
  210.             return true;  
  211.         }  
  212.   
  213.         db.close();  
  214.         return false;  
  215.   
  216.   
  217.   
  218.     }  
Login.java.class:
  1. public class login extends AppCompatActivity {  
  2.     DatabaseHelper myDb;  
  3.     EditText TextPseudo,Textphone;  
  4.     Button blogin;  
  5.     View v;  
  6.   
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.activity_login);  
  11.   
  12.         TextPseudo=(EditText)findViewById(R.id.editTextPseudo) ;  
  13.         Textphone=(EditText)findViewById(R.id.input_phone);  
  14.   
  15.   
  16.         blogin=(Button)findViewById(R.id.buttonlogin);  
  17.         v=findViewById(android.R.id.content);  
  18.         blogin.setOnClickListener(new View.OnClickListener() {  
  19.             @Override  
  20.             public void onClick(View view) {  
  21.   
  22.                 switch (view .getId()) {  
  23.                     case R.id.buttonlogin:  
  24.                         verifyFromSQLite();  
  25.                         break;  
  26.                     case R.id.linkSignup:  
  27.                         // Navigate to RegisterActivity  
  28.                         Intent intentRegister = new Intent(getApplicationContext(), register.class);  
  29.                         startActivity(intentRegister);  
  30.                         break;  
  31.             }  
  32.         }  
  33.   
  34.   
  35.   
  36.         private void verifyFromSQLite() {  
  37.   
  38.   
  39.             if (myDb.checkInscris(TextPseudo.getText().toString().trim()  
  40.                     , Textphone.getText().toString().trim())) {  
  41.   
  42.   
  43.                 Intent accountsIntent = new Intent(getApplicationContext(), Menu.class);  
  44.                 accountsIntent.putExtra("phone", Textphone.getText().toString().trim());  
  45.                 emptyInputEditText();  
  46.                 startActivity(accountsIntent);  
  47.   
  48.   
  49.             } else {  
  50.                 // Snack Bar to show success message that record is wrong  
  51.                 Snackbar.make( v,"Pseudo et téléphone incorrect", Snackbar.LENGTH_LONG).show();  
  52.             }  
  53.         }  
  54.         private void emptyInputEditText() {  
  55.             TextPseudo.setText(null);  
  56.             Textphone.setText(null);  
  57.         }  
  58.   
  59.   
  60.   
  61.     });  
  62. }  
Manifest.xml:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.example.sofari.tiffanezni">  
  4.     <uses-permission android:name="android.permission.CAMERA" />  
  5.     <uses-feature android:name="android.hardware.camera" />  
  6.     <uses-feature android:name="android.hardware.camera.autofocus" />  
  7.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
  8.   
  9.     <uses-sdk  
  10.         android:minSdkVersion="7"  
  11.         android:targetSdkVersion="16" />  
  12.     <application  
  13.   
  14.         android:allowBackup="true"  
  15.         android:icon="@drawable/ic_tiffa"  
  16.         android:label="@string/app_name"  
  17.         android:roundIcon="@drawable/ic_tiffa"  
  18.         android:supportsRtl="true"  
  19.         android:theme="@style/AppTheme">  
  20.         <activity android:name=".MainActivity">  
  21.             <intent-filter>  
  22.                 <action android:name="android.intent.action.MAIN" />  
  23.   
  24.                 <category android:name="android.intent.category.LAUNCHER" />  
  25.             </intent-filter>  
  26.         </activity>  
  27.         <activity android:name=".login"  
  28.             android:label="Login">  
  29.   
  30.             <intent-filter>  
  31.                 <action android:name="android.intent.action.MAIN" />  
  32.   
  33.                 <category android:name="android.intent.category.DEFAULT" />  
  34.             </intent-filter>  
  35.         </activity>  
  36.   
  37.   
  38.   
  39.         <activity android:name=".register"  
  40.             android:label="Inscription">  
  41.   
  42.             <intent-filter>  
  43.             <action android:name="android.intent.action.MAIN" />  
  44.   
  45.             <category android:name="android.intent.category.DEFAULT" />  
  46.             </intent-filter>  
  47.         </activity>  
  48.   
  49.   
  50.   
  51.         <activity android:name=".Menu"  
  52.             android:label="Menu"></activity>  
  53.     </application>  
  54.   
  55. </manifest>  
Thanks! 
 

Answers (2)