Bonsoir,
Je débute sous Android et je souhaites faire appel à une activité d'un projet à partir d'une activité d'un autre projet mais j'ai une erreur du type : ActivityNotFoundException![]()
J'ai deux projets dans mon repertoire AndroidStudioProjects:
- AppelCalculSomme contenant l'activité AppelCalculSommeActivity (activité appelante)
- Calculatrice contenant l'activité CalculatriceActivity (activité appelée)
Voici le code pour le projet AppelCalculSomme :
AppelCalculSommeActivity :
manifest.xml
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 package df.cours5; import java.util.ArrayList; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.TextView; public class AppelCalculSommeActivity extends Activity { private static final int REQUEST_CODE = 999; private EditText parametre ; private TextView resultat ; private ArrayList<Integer> entiers; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); parametre = (EditText)findViewById(R.id.parametre); resultat = (TextView)findViewById(R.id.resultat); entiers = new ArrayList<Integer>(); } public void onClickAjouter(View view) { int entier; try { entier = Integer.parseInt(parametre.getText().toString()); } catch (Exception e) { return; } entiers.add(entier); if (entiers.size()==1) resultat.setText(entier+" "); else resultat.append("+ "+entier+" "); } public void onClickSommer(View view) { Intent intention = new Intent("Calculatrice.intent.action.Launch"); intention.putExtra("liste", entiers); intention.putExtra("operation", "+"); startActivityForResult(intention, REQUEST_CODE); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(resultCode == RESULT_OK && requestCode == REQUEST_CODE && data.hasExtra("result")) { Bundle extras = data.getExtras(); int res = extras.getInt("result", 0); System.out.println(res); resultat.setText(res+" "); } } public void onClickEffacer(View view) { parametre.setText(""); resultat.setText(""); entiers = new ArrayList<Integer>(); } }
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 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="df.cours5" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="15" /> <application android:icon="@drawable/upjv_launcher" android:label="@string/app_name" > <activity android:name=".AppelCalculSommeActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
main.xml
Et le code du projet Calculatrice :
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 <?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" > <EditText android:id="@+id/parametre" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="text" android:text="parametre entier à ajouter" /> <Button android:id="@+id/ajouter" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:onClick="onClickAjouter" android:text="ajouter" /> <Button android:id="@+id/sommer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:onClick="onClickSommer" android:text="sommer" /> <Button android:id="@+id/effacer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:onClick="onClickEffacer" android:text="effacer" /> <TextView android:id="@+id/resultat" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Resultat" /> </LinearLayout>
CalculatriceActivity
manifest.xml
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 package df.cours6; import java.util.ArrayList; import android.app.Activity; import android.content.Intent; import android.os.Bundle; public class CalculatriceActivity extends Activity { private static final int PLUS=1, MOINS=2, MULT=3, DIV=4; public void onCreate(Bundle bundle) { super.onCreate(bundle); if (getIntent().hasExtra("liste") && getIntent().hasExtra("operation")) { ArrayList<Integer> entiers = getIntent().getIntegerArrayListExtra("liste"); String operation = getIntent().getStringExtra("operation"); int op = 0; if (operation.equals("+")) op = PLUS; else if (operation.equals("-")) op = MOINS; else if (operation.equals("*")) op = MULT; else if (operation.equals("/")) op = DIV; if ((op != 0) && (entiers.size() > 0)) { int result = entiers.get(0); int nbre = entiers.size(); for (int i = 1; i < nbre; ++i) result = (op==PLUS?result+entiers.get(i): (op==MOINS?result-entiers.get(i): (op==MULT?result*entiers.get(i):result/entiers.get(i)))); Intent intentionResult = new Intent(); intentionResult.putExtra("result", result); setResult(RESULT_OK, intentionResult); this.finish(); } } setResult(RESULT_CANCELED, null); this.finish(); } }
main.xml
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 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="df.cours6" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="15" /> <application android:icon="@drawable/upjv_launcher" android:label="@string/app_name" > <activity android:name=".CalculatriceActivity" android:label="@string/app_name" > <intent-filter> <action android:name="Calculatrice.intent.action.Launch" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> </application> </manifest>
Quelqu'un a une solution à me proposer
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 <?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" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout>
Merci d'avance![]()
Partager