Bonjour ! Je suis en train de développer une app mobile pour Android en Java, et j'ai besoin d'ajouter un fragment à mon application, cependant quand je lance mon app, le fragment n'apparait pas, pouvez vous m'aider svp ?
Activité principale java "MainActivity":
Activité principal xml "activity_main":
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 package fr.woc.slidermotorisev2; import android.os.Bundle; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import fr.woc.slidermotorisev2.fragments.BluetoothConnection; public class MainActivity extends AppCompatActivity { private fr.woc.slidermotorisev2.fragments.BluetoothConnection BluetoothConnection; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.ShowBluetoothFragment(); } private void ShowBluetoothFragment(){ // A - Create new main fragment BluetoothConnection = new BluetoothConnection(); // B - Add it to FrameLayout container getSupportFragmentManager().beginTransaction().add(R.id.frame_container, BluetoothConnection).commit(); } }
Code XML : Sélectionner tout - Visualiser dans une fenêtre à part
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 <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.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" android:background="@color/white" tools:context=".MainActivity"> <ImageView android:clickable="true" android:id="@+id/ic_slider_move" android:layout_width="125dp" android:layout_height="125dp" android:src="@drawable/arrow_black" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" android:layout_marginLeft="10dp"/> <ImageView android:clickable="true" android:id="@+id/ic_save_position" android:layout_width="125dp" android:layout_height="125dp" android:src="@drawable/sd_black" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintLeft_toLeftOf="parent"/> <ImageView android:clickable="true" android:id="@+id/ic_setting" android:layout_width="125dp" android:layout_height="125dp" android:src="@drawable/setting_black" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintRight_toRightOf="parent" android:layout_marginRight="10dp"/> <FrameLayout android:id="@+id/frame_container" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="125dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.0" /> </androidx.constraintlayout.widget.ConstraintLayout>
Le fragment java "BluetoothConnection":
Et le fragment xml "fragement_bluetooth_connection":
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package fr.woc.slidermotorisev2.fragments; import android.os.Bundle; import androidx.fragment.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import fr.woc.slidermotorisev2.R; public class BluetoothConnection extends Fragment { public class BluetoothFragment extends Fragment { public BluetoothFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_bluetooth_connection, container, false); } } }
Code XML : Sélectionner tout - Visualiser dans une fenêtre à part
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 <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".fragments.BluetoothConnection" android:background="@color/blue_dark"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/select_device" android:theme="@style/title" android:gravity="center" android:layout_marginTop="25dp" android:layout_marginRight="25dp" android:layout_marginLeft="25dp"/> <ListView android:id="@+id/bluetooth_devices" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="200dp" android:layout_marginLeft="25dp" android:layout_marginRight="25dp" android:layout_marginBottom="25dp" android:background="@color/blue_duck"/> </FrameLayout>
Je pense que mon erreur est toute simple mais je suis débutant et sa fait 1 semaine que je suis dessus
Pouvez vous m'aider ?
Merci !
Partager