Problème de liaison d'activité avec android studio
Bonjour, je suis en L2 mathématique et informatique, j'ai pour but de faire de faire un petit projet, et pour ca je veux développer une application mobile sur android studio.
Le problème c'est que j'ai voulu lier 2 de mes activités (le lien que je veux créer est MainActivity.java ->GameActivity.java) , mais lorsque je lance le simulateur, cela ne marche pas.
J'ai quelques variables inutilisées car je n'avais pas directement testé si le lien marchait ou pas.
Est ce que vous pourriez m'aider ?
Voici mon code :
activity_main.xml
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical"
tools:context=".controller.MainActivity">
<TextView
android:id="@+id/main_textview_greeting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:padding="8dp"
android:text="Bienvenue dans Find Room. Quel est votre prénom ?" />
<EditText
android:id="@+id/main_edittext_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:hint="Ecrivez votre prénom" />
<Button
android:id="@+id/main_button_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="16dp"
android:text="Let's go" />
</LinearLayout> |
MainActivity.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| package com.example.findroom_bdx.controller;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.findroom_bdx.R;
public class MainActivity extends AppCompatActivity {
private TextView mGreetingTextView;
private EditText mNameEditText;
private Button mPlayButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGreetingTextView = findViewById(R.id.main_textview_greeting);
mNameEditText = findViewById(R.id.main_edittext_name);
mPlayButton = findViewById(R.id.main_button_play);
mPlayButton.setEnabled(false);
mNameEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
mPlayButton.setEnabled(!editable.toString().isEmpty());
}
});
mPlayButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent gameActivityIntent = new Intent(MainActivity.this, GameActivity.class);
startActivity(gameActivityIntent);
}
});
}
} |
activity_game.xml
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textview_jour"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dip"
android:text="Selectionner un jour:"
android:layout_marginBottom="5dp"/>
<Spinner
android:id="@+id/spinner_jour"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:id="@+id/textview_heure"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dip"
android:text="Selectionner un heure:"
android:layout_marginBottom="5dp"/>
<Spinner
android:id="@+id/spinner_heure"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button_play_activitygame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="16dp"
android:text="Let's go" />
</LinearLayout> |
GameActivity.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| package com.example.findroom_bdx.controller;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.findroom_bdx.R;
import com.example.findroom_bdx.model.Model.Heure;
import com.example.findroom_bdx.model.Model.Heure_data;
class GameActivity extends AppCompatActivity {
private TextView mTextView_jour;
private TextView mTextView_heure;
private Spinner mspinner_jour;
private Spinner mspinner_heure;
private Button mPlayButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
this.mspinner_jour = (Spinner) findViewById(R.id.spinner_jour);
this.mspinner_heure = (Spinner) findViewById(R.id.spinner_heure);
mTextView_jour = findViewById(R.id.textview_jour);
mTextView_heure = findViewById(R.id.textview_heure);
mPlayButton = findViewById(R.id.button_play_activitygame);
mPlayButton.setEnabled(false);
Heure[] heures = Heure_data.getHeures();
ArrayAdapter<Heure> adapter = new ArrayAdapter<Heure>(this,
android.R.layout.simple_spinner_item, heures);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
this.mspinner_jour.setAdapter(adapter);
this.mspinner_jour.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
onItemSelectedHandler(parent, view, position, id);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
private void onItemSelectedHandler(AdapterView<?> adapterView, View view, int position, long id) {
Adapter adapter = adapterView.getAdapter();
Heure heure = (Heure) adapter.getItem(position);
Toast.makeText(getApplicationContext(), "Selected Heure: " + heure.getFullName() ,Toast.LENGTH_SHORT).show();
}
public class SpinnerActivity_heure extends Activity implements AdapterView.OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
Spinner spinner = (Spinner) findViewById(R.id.spinner_heure);
}
} |
Heure.java //c'est un document pour créer mon spinner
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| package com.example.findroom_bdx.model.Model;
public class Heure {
private String heure;
public Heure(String heure) {
this.heure = heure;
}
public String getHeure() {
return heure;
}
public void setHeure(String heure) {
this.heure = heure;
}
public String getFullName() {
return this.heure;
}
@Override
public String toString() {
return this.getFullName();
}
} |
Heure_data.java //c'est un document pour créer mon spinner
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package com.example.findroom_bdx.model.Model;
public class Heure_data {
public static Heure[] getHeures() {
Heure h1 = new Heure("08h00");
Heure h2 = new Heure("09h30");
Heure h3 = new Heure("11h00");
Heure h4 = new Heure("12h30");
Heure h5 = new Heure("14h00");
Heure h6 = new Heure("15h30");
Heure h7 = new Heure("17h00");
Heure h8 = new Heure("18h30");
Heure h9 = new Heure("20h00");
return new Heure[] {h1, h2, h3,h4,h5,h6,h7,h8,h9};
}
} |
Merci beaucoup,