Bonjour,
J'ai un soucis concernant l'implémentation du support de la classe Fragment sous une version 2.3 d'Android.
Voici ce que j'ai fais:
J'ai crée une classe héritant de la classe FragmentActivity: - contenant un spinner afin de sélectionner un élément
Mon layout principal comporte un spinner et un FrameLayout qui contiendra le Fragment associé au visualiseur.
D'où la création d'une classe héritant de Fragment, afin d'afficher le visualiseur en fonction du choix sélectionné.
Voici mon code:
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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108 public class ListFragmentActivity extends FragmentActivity { private List<String> _datas = null; // Contient les données à afficher public int idSelected = -1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.plans); createData(); setupView(); } private void setupView() { // Récupérer la vue final MySpinner spinner = (MySpinner) findViewById(R.id.myspinner); // Créer un adapter avec une liste de String final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, _datas); // Définir le style des éléments de l'adapter adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // Assigner l'adapter à la vue spinner.setAdapter(adapter); spinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { idSelected = arg2; Log.v("spinner.onItemSelected", "idSelected=" + idSelected); runOnUiThread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub File file = new File(AConstant.lignesFolder + AConstant.lignes[idSelected] + ".pdf"); Intent intent; if (file.exists()) { Log.v("PlanActivity.OnselectedListener", "file exist"); Uri path = Uri.fromFile(file); intent = new Intent(Intent.ACTION_VIEW,Uri.parse(""),getApplicationContext(),ViewerFragmentActivity.class); intent.setPackage("com.adobe.reader"); intent.setDataAndType(path, "application/pdf"); try { FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); Fragment viewerDetails = new ViewerFragment(); viewerDetails.onAttach(ListFragmentActivity.this); fragmentTransaction.replace(R.id.mainFragement, viewerDetails); fragmentTransaction.commit(); viewerDetails.startActivity(intent); if(viewerDetails.isAdded()){ Log.e("ListFragmentActivity.","viewDetails is added"); } } catch (ActivityNotFoundException e) { Toast.makeText(getApplicationContext(), "No Application Available to View PDF", Toast.LENGTH_SHORT).show(); } } } }); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); } // /Création statique des données private void createData() { _datas = new ArrayList<String>(); for (int i = 0; i < AConstant.lignes.length; i++) { _datas.add(AConstant.lignes[i]); } }
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 public class ViewerFragment extends Fragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.e("ViewFragment", "hello"); //onAttach(getActivity()); } @Override public void onAttach(Activity activity) { super.onAttach(activity); try { } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnTutSelectedListener"); } } }
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 public class ViewerFragmentActivity extends FragmentActivity{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.viewer_fragment); ViewerFragment viewer = (ViewerFragment) getSupportFragmentManager() .findFragmentById(R.id.viewer_fragment); } }
Le layout:
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 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <com.utils.MySpinner android:id="@+id/myspinner" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@+id/mainFragement" android:layout_width="fill_parent" android:layout_height="wrap_content"> </FrameLayout> </LinearLayout>
Quel est le problème, est-ce un problème d'implémentation?
J'ai essayé de créer 2 fragments dans le layout principal, l'un contiendra le spinner et l'autre le visualiseur mais cela n'a pas l'air de marcher sous la version 2.3.
Voici l'erreur:
02-22 19:29:56.761: E/AndroidRuntime(19034): java.lang.IllegalStateException: Fragment ViewerFragment{40560320 id=0x7f06001d} not attached to Activity
Merci.
Partager