Forums - C# Corner

Forum guidelines
Sofari Agali

Sofari Agali

  • 1.5k
  • 13
  • 728

Application stops when click Button

Oct 6 2017 9:15 AM
Hi !
 
I have a problem with the second button of my MainActivity, if I click on it to open an activity, the app hangs; here are my different codes:
 
MainActivity.java
  1. public class MainActivity extends AppCompatActivity {  
  2.   Button b1;  
  3.     Button b2;  
  4.     @Override  
  5.     protected void onCreate(Bundle savedInstanceState) {  
  6.         super.onCreate(savedInstanceState);  
  7.         setContentView(R.layout.activity_main);  
  8.   
  9.   
  10.         b1=(Button)findViewById(R.id.btn_signIn);  
  11.         b1.setOnClickListener(new View.OnClickListener() {  
  12.             @Override  
  13.             public void onClick(View view) {  
  14.                 Intent intent = new Intent(getApplicationContext(),login.class);  
  15.                 startActivity(intent);  
  16.             }  
  17.         });  
  18.           
  19.         b2 =(Button)findViewById(R.id.btn_signUp);  
  20.         b2.setOnClickListener(new View.OnClickListener() {  
  21.             @Override  
  22.             public void onClick(View view) {  
  23.                 Intent i=new Intent(getApplicationContext(),register.class);  
  24.                 startActivity(i);  
  25.             }  
  26.         });  
  27.     }  
  28.   
  29.   
  30. }  
MainActivity.xml
  1. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     tools:context="com.example.sofari.tiffanezni.MainActivity">  
  7.   
  8.     <LinearLayout  
  9.         android:layout_width="373dp"  
  10.         android:layout_height="495dp"  
  11.         android:orientation="vertical"  
  12.         android:weightSum="1"  
  13.         android:background="@color/colorAccent"  
  14.         tools:layout_editor_absoluteX="6dp"  
  15.         tools:layout_editor_absoluteY="8dp"  
  16.         tools:ignore="MissingConstraints">  
  17.   
  18.   
  19.         <ImageView  
  20.             android:id="@+id/imageView"  
  21.             android:layout_width="match_parent"  
  22.             android:layout_height="150dp"  
  23.             android:layout_gravity="center_horizontal"  
  24.             android:layout_marginBottom="-1dp"  
  25.             android:layout_weight="0.12"  
  26.             app:srcCompat="@drawable/ic_tiffa"  
  27.             tools:layout_editor_absoluteX="-3dp"  
  28.             tools:layout_editor_absoluteY="-2dp"  
  29.             tools:ignore="ContentDescription" />  
  30.   
  31.         <RelativeLayout  
  32.             android:layout_width="match_parent"  
  33.             android:layout_height="325dp"  
  34.             tools:layout_editor_absoluteX="4dp"  
  35.             tools:layout_editor_absoluteY="243dp">  
  36.   
  37.   
  38.             <android.support.v7.widget.AppCompatButton  
  39.                 android:id="@+id/btn_signIn"  
  40.                 android:layout_width="234dp"  
  41.                 android:layout_height="48dp"  
  42.                 android:layout_above="@+id/btn_signUp"  
  43.                 android:layout_centerHorizontal="true"  
  44.                 android:layout_gravity="center_horizontal"  
  45.                 android:layout_marginBottom="37dp"  
  46.                 android:background="@color/colorPrimary"  
  47.                 android:onClick="onClick"  
  48.                 android:text="Membre Login"  
  49.                 android:textColor="@android:color/white"  
  50.                 tools:layout_editor_absoluteX="75dp"  
  51.                 tools:layout_editor_absoluteY="313dp"  
  52.                 tools:ignore="HardcodedText" />  
  53.   
  54.             <android.support.v7.widget.AppCompatButton  
  55.                 android:id="@+id/btn_signUp"  
  56.                 android:layout_width="234dp"  
  57.                 android:layout_height="48dp"  
  58.                 android:layout_alignLeft="@+id/btn_signIn"  
  59.                 android:layout_alignParentBottom="true"  
  60.                 android:layout_alignStart="@+id/btn_signIn"  
  61.                 android:layout_gravity="center_horizontal"  
  62.                 android:layout_marginBottom="76dp"  
  63.                 android:background="@color/colorPrimary"  
  64.                 android:onClick="onClick"  
  65.                 android:clickable="true"  
  66.                 android:text="Devenir donneur"  
  67.                 android:textColor="@android:color/white"  
  68.                 tools:layout_editor_absoluteX="75dp"  
  69.                 tools:layout_editor_absoluteY="386dp"  
  70.                 tools:ignore="HardcodedText" />  
  71.         RelativeLayout>  
  72.     LinearLayout>  
The signUp button is for open registerActivity and the locking is on this button
 
register.java
  1. public class register extends AppCompatActivity {  
  2. EditText editpseudo,editnom,editphone,editnaissance,editville,editsexe,editgroupe,editdatedon;  
  3.     Button btnchose,btnAdd,btnlist;  
  4.     ImageView imageView;  
  5.     final int REQUEST_CODE_GALLERY = 999;  
  6.   
  7.   public static  SQLiteHelper sqLiteHelper;  
  8.     @Override  
  9.     protected void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.activity_register);  
  12.   
  13.         init();  
  14.   
  15.         sqLiteHelper = new SQLiteHelper(this,"Donsang.db",null,1);  
  16.         sqLiteHelper.queryData("CREATE TABLE IF NOT EXISTS INSCRIPTION ( id INTEGER PRIMARY KEY AUTOINCREMENT" +  
  17.                 "pseudo VARCHAR,nom VARCHAR,phone VARCHAR,naissance VARCHAR,ville VARCHAR,sexe VARCHAR,groupe VARCHAR" +  
  18.                 "datedon VARCHAR,image Blob");  
  19.   
  20.         btnchose.setOnClickListener(new View.OnClickListener() {  
  21.             @Override  
  22.             public void onClick(View view) {  
  23.                 ActivityCompat.requestPermissions(register.this,  
  24.                         new String[]{Manifest.permission.READ_EXTERNAL_STORAGE },  
  25.                           REQUEST_CODE_GALLERY);  
  26.             }  
  27.   
  28.         });  
  29.     }  
  30.   
  31.     @Override  
  32.     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {  
  33.         if(requestCode == REQUEST_CODE_GALLERY){  
  34.   
  35.           if(grantResults.length >  0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){  
  36.               Intent intent = new Intent(Intent.ACTION_PICK);  
  37.               intent.setType("image / *");  
  38.               startActivityForResult(intent,REQUEST_CODE_GALLERY);  
  39.           }  
  40.           else {  
  41.               Toast.makeText(getApplicationContext(),"Vous n'avez pas la permission d'accéder à l'emplacement du fichier ",Toast.LENGTH_SHORT).show();  
  42.           }  
  43.           return;  
  44.         }  
  45.   
  46.         super.onRequestPermissionsResult(requestCode, permissions, grantResults);  
  47.     }  
  48.   
  49.     @Override  
  50.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  51.   
  52.          if(requestCode == REQUEST_CODE_GALLERY && resultCode == RESULT_OK && data != null ){  
  53.   
  54.              Uri uri = data.getData();  
  55.              try {  
  56.                  InputStream inputStream = getContentResolver().openInputStream(uri);  
  57.                  Bitmap bitmap = BitmapFactory.decodeStream(inputStream);  
  58.                  imageView.setImageBitmap(bitmap);  
  59.              } catch (FileNotFoundException e) {  
  60.                  e.printStackTrace();  
  61.              }  
  62.   
  63.          }  
  64.   
  65.         super.onActivityResult(requestCode, resultCode, data);  
  66.     }  
  67.   
  68.     private void init(){  
  69.         editpseudo = (EditText)findViewById(R.id.input_pseudo);  
  70.         editnom = (EditText)findViewById(R.id.input_nom);  
  71.         editphone = (EditText)findViewById(R.id.input_phone);  
  72.         editnaissance = (EditText)findViewById(R.id.input_datenaiss);  
  73.         editville = (EditText)findViewById(R.id.input_ville);  
  74.         editsexe = (EditText)findViewById(R.id.input_sexe);  
  75.         editgroupe = (EditText)findViewById(R.id.input_groupe);  
  76.         editdatedon = (EditText)findViewById(R.id.input_datedon);  
  77.         imageView = (ImageView)findViewById(R.id.imageView4);  
  78.   
  79.   
  80.     }  
  81. }  
SQLIteHelper.java
  1. public class SQLiteHelper extends SQLiteOpenHelper{  
  2.     public SQLiteHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {  
  3.         super(context, name, factory, version);  
  4.     }  
  5.   
  6.     public void queryData(String sql){  
  7.       SQLiteDatabase database = getWritableDatabase();  
  8.         database.execSQL(sql);  
  9.         database.close();  
  10.     }  
  11.   
  12.     public void insertData(String pseudo,String nom,String phone,String naissance,String ville,String sexe,String groupe,String datedon,byte[] image ){  
  13.   
  14.         SQLiteDatabase database = getWritableDatabase();  
  15.         String sql = "INSERT INTO INSCRIPTION VALUES ( NULL,?,?,?,?,?,?,?,?,?)";  
  16.         SQLiteStatement statement = database.compileStatement(sql);  
  17.         statement.clearBindings();  
  18.   
  19.         statement.bindString(1,pseudo);  
  20.         statement.bindString(2,nom);  
  21.         statement.bindString(3,phone);  
  22.         statement.bindString(4,naissance);  
  23.         statement.bindString(5,ville);  
  24.         statement.bindString(6,sexe);  
  25.         statement.bindString(7,groupe);  
  26.         statement.bindString(8,datedon);  
  27.         statement.bindBlob(9,image);  
  28.   
  29.         statement.executeInsert();  
  30.         database.close();  
  31.   
  32.     }  
  33.       public Cursor getData(String sql){  
  34.        SQLiteDatabase database = getReadableDatabase();  
  35.           return database.rawQuery(sql,null);  
  36.   
  37.   
  38.       }  
  39.   
  40.   
  41.     @Override  
  42.     public void onCreate(SQLiteDatabase sqLiteDatabase) {  
  43.   
  44.     }  
  45.   
  46.     @Override  
  47.     public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {  
  48.   
  49.     }  
  50. }  
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.READ_EXTERNAL_STORAGE"/>  
  5.     <application  
  6.         android:allowBackup="true"  
  7.         android:icon="@drawable/ic_tiffa"  
  8.         android:label="@string/app_name"  
  9.         android:roundIcon="@mipmap/ic_launcher_round"  
  10.         android:supportsRtl="true"  
  11.         android:theme="@style/AppTheme">  
  12.         <activity android:name=".MainActivity">  
  13.             <intent-filter>  
  14.                 <action android:name="android.intent.action.MAIN" />  
  15.   
  16.                 <category android:name="android.intent.category.LAUNCHER" />  
  17.             intent-filter>  
  18.         activity>  
  19.         <activity android:name=".login" />  
  20.   
  21.         <activity android:name=".register" >  
  22.         <intent-filter>  
  23.             <action android:name="android.intent.action.MAIN" />  
  24.   
  25.             <category android:name="android.intent.category.DEFAULT" />  
  26.         intent-filter>  
  27.         activity>  
  28.   
  29.         <activity android:name=".Menu">  
  30.   
  31.         activity>  
  32.     application>  
  33.   
  34. manifest>  
Please someone to help me 
 

Answers (2)