Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Login
Sign Up
Ask Question
2
Reply
Application stops when click Button
Sofari Agali
Oct 6 2017 9:15 AM
222
Reply
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
public
class
MainActivity
extends
AppCompatActivity {
Button b1;
Button b2;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.btn_signIn);
b1.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View view) {
Intent intent =
new
Intent(getApplicationContext(),login.
class
);
startActivity(intent);
}
});
b2 =(Button)findViewById(R.id.btn_signUp);
b2.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View view) {
Intent i=
new
Intent(getApplicationContext(),register.
class
);
startActivity(i);
}
});
}
}
MainActivity.xml
<
android.support.constraint.ConstraintLayout
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"
tools:context
=
"com.example.sofari.tiffanezni.MainActivity"
>
<
LinearLayout
android:layout_width
=
"373dp"
android:layout_height
=
"495dp"
android:orientation
=
"vertical"
android:weightSum
=
"1"
android:background
=
"@color/colorAccent"
tools:layout_editor_absoluteX
=
"6dp"
tools:layout_editor_absoluteY
=
"8dp"
tools:ignore
=
"MissingConstraints"
>
<
ImageView
android:id
=
"@+id/imageView"
android:layout_width
=
"match_parent"
android:layout_height
=
"150dp"
android:layout_gravity
=
"center_horizontal"
android:layout_marginBottom
=
"-1dp"
android:layout_weight
=
"0.12"
app:srcCompat
=
"@drawable/ic_tiffa"
tools:layout_editor_absoluteX
=
"-3dp"
tools:layout_editor_absoluteY
=
"-2dp"
tools:ignore
=
"ContentDescription"
/>
<
RelativeLayout
android:layout_width
=
"match_parent"
android:layout_height
=
"325dp"
tools:layout_editor_absoluteX
=
"4dp"
tools:layout_editor_absoluteY
=
"243dp"
>
<
android.support.v7.widget.AppCompatButton
android:id
=
"@+id/btn_signIn"
android:layout_width
=
"234dp"
android:layout_height
=
"48dp"
android:layout_above
=
"@+id/btn_signUp"
android:layout_centerHorizontal
=
"true"
android:layout_gravity
=
"center_horizontal"
android:layout_marginBottom
=
"37dp"
android:background
=
"@color/colorPrimary"
android:onClick
=
"onClick"
android:text
=
"Membre Login"
android:textColor
=
"@android:color/white"
tools:layout_editor_absoluteX
=
"75dp"
tools:layout_editor_absoluteY
=
"313dp"
tools:ignore
=
"HardcodedText"
/>
<
android.support.v7.widget.AppCompatButton
android:id
=
"@+id/btn_signUp"
android:layout_width
=
"234dp"
android:layout_height
=
"48dp"
android:layout_alignLeft
=
"@+id/btn_signIn"
android:layout_alignParentBottom
=
"true"
android:layout_alignStart
=
"@+id/btn_signIn"
android:layout_gravity
=
"center_horizontal"
android:layout_marginBottom
=
"76dp"
android:background
=
"@color/colorPrimary"
android:onClick
=
"onClick"
android:clickable
=
"true"
android:text
=
"Devenir donneur"
android:textColor
=
"@android:color/white"
tools:layout_editor_absoluteX
=
"75dp"
tools:layout_editor_absoluteY
=
"386dp"
tools:ignore
=
"HardcodedText"
/>
RelativeLayout
>
LinearLayout
>
The signUp button is for open registerActivity and the locking is on this button
register.java
public
class
register
extends
AppCompatActivity {
EditText editpseudo,editnom,editphone,editnaissance,editville,editsexe,editgroupe,editdatedon;
Button btnchose,btnAdd,btnlist;
ImageView imageView;
final
int
REQUEST_CODE_GALLERY =
999
;
public
static
SQLiteHelper sqLiteHelper;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
init();
sqLiteHelper =
new
SQLiteHelper(
this
,
"Donsang.db"
,
null
,
1
);
sqLiteHelper.queryData(
"CREATE TABLE IF NOT EXISTS INSCRIPTION ( id INTEGER PRIMARY KEY AUTOINCREMENT"
+
"pseudo VARCHAR,nom VARCHAR,phone VARCHAR,naissance VARCHAR,ville VARCHAR,sexe VARCHAR,groupe VARCHAR"
+
"datedon VARCHAR,image Blob"
);
btnchose.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View view) {
ActivityCompat.requestPermissions(register.
this
,
new
String[]{Manifest.permission.READ_EXTERNAL_STORAGE },
REQUEST_CODE_GALLERY);
}
});
}
@Override
public
void
onRequestPermissionsResult(
int
requestCode,
@NonNull
String[] permissions,
@NonNull
int
[] grantResults) {
if
(requestCode == REQUEST_CODE_GALLERY){
if
(grantResults.length >
0
&& grantResults[
0
] == PackageManager.PERMISSION_GRANTED){
Intent intent =
new
Intent(Intent.ACTION_PICK);
intent.setType(
"image / *"
);
startActivityForResult(intent,REQUEST_CODE_GALLERY);
}
else
{
Toast.makeText(getApplicationContext(),
"Vous n'avez pas la permission d'accéder à l'emplacement du fichier "
,Toast.LENGTH_SHORT).show();
}
return
;
}
super
.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
@Override
protected
void
onActivityResult(
int
requestCode,
int
resultCode, Intent data) {
if
(requestCode == REQUEST_CODE_GALLERY && resultCode == RESULT_OK && data !=
null
){
Uri uri = data.getData();
try
{
InputStream inputStream = getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
imageView.setImageBitmap(bitmap);
}
catch
(FileNotFoundException e) {
e.printStackTrace();
}
}
super
.onActivityResult(requestCode, resultCode, data);
}
private
void
init(){
editpseudo = (EditText)findViewById(R.id.input_pseudo);
editnom = (EditText)findViewById(R.id.input_nom);
editphone = (EditText)findViewById(R.id.input_phone);
editnaissance = (EditText)findViewById(R.id.input_datenaiss);
editville = (EditText)findViewById(R.id.input_ville);
editsexe = (EditText)findViewById(R.id.input_sexe);
editgroupe = (EditText)findViewById(R.id.input_groupe);
editdatedon = (EditText)findViewById(R.id.input_datedon);
imageView = (ImageView)findViewById(R.id.imageView4);
}
}
SQLIteHelper.java
public
class
SQLiteHelper
extends
SQLiteOpenHelper{
public
SQLiteHelper(Context context, String name, SQLiteDatabase.CursorFactory factory,
int
version) {
super
(context, name, factory, version);
}
public
void
queryData(String sql){
SQLiteDatabase database = getWritableDatabase();
database.execSQL(sql);
database.close();
}
public
void
insertData(String pseudo,String nom,String phone,String naissance,String ville,String sexe,String groupe,String datedon,
byte
[] image ){
SQLiteDatabase database = getWritableDatabase();
String sql =
"INSERT INTO INSCRIPTION VALUES ( NULL,?,?,?,?,?,?,?,?,?)"
;
SQLiteStatement statement = database.compileStatement(sql);
statement.clearBindings();
statement.bindString(
1
,pseudo);
statement.bindString(
2
,nom);
statement.bindString(
3
,phone);
statement.bindString(
4
,naissance);
statement.bindString(
5
,ville);
statement.bindString(
6
,sexe);
statement.bindString(
7
,groupe);
statement.bindString(
8
,datedon);
statement.bindBlob(
9
,image);
statement.executeInsert();
database.close();
}
public
Cursor getData(String sql){
SQLiteDatabase database = getReadableDatabase();
return
database.rawQuery(sql,
null
);
}
@Override
public
void
onCreate(SQLiteDatabase sqLiteDatabase) {
}
@Override
public
void
onUpgrade(SQLiteDatabase sqLiteDatabase,
int
i,
int
i1) {
}
}
Manifest.xml
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
manifest
xmlns:android
=
"http://schemas.android.com/apk/res/android"
package
=
"com.example.sofari.tiffanezni"
>
<
uses-permission
android:name
=
"android.permission.READ_EXTERNAL_STORAGE"
/>
<
application
android:allowBackup
=
"true"
android:icon
=
"@drawable/ic_tiffa"
android:label
=
"@string/app_name"
android:roundIcon
=
"@mipmap/ic_launcher_round"
android:supportsRtl
=
"true"
android:theme
=
"@style/AppTheme"
>
<
activity
android:name
=
".MainActivity"
>
<
intent-filter
>
<
action
android:name
=
"android.intent.action.MAIN"
/>
<
category
android:name
=
"android.intent.category.LAUNCHER"
/>
intent-filter
>
activity
>
<
activity
android:name
=
".login"
/>
<
activity
android:name
=
".register"
>
<
intent-filter
>
<
action
android:name
=
"android.intent.action.MAIN"
/>
<
category
android:name
=
"android.intent.category.DEFAULT"
/>
intent-filter
>
activity
>
<
activity
android:name
=
".Menu"
>
activity
>
application
>
manifest
>
Please someone to help me
Upload Source Code
Select only zip and rar file.
Post
Reset
Cancel
Answers (
2
)
Next Recommended Forum
detect Touch event in edittext in android xamarin?
How to Save Fragment State in Fragment