IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

Service et Activite


Sujet :

Android

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Homme Profil pro
    Etudiant du Genie Logiciel
    Inscrit en
    Juillet 2011
    Messages
    397
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Cameroun

    Informations professionnelles :
    Activité : Etudiant du Genie Logiciel
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2011
    Messages : 397
    Par défaut Service et Activite
    Salut a tous. j'essaie de faire démarrer une activité par un service mais a l’exécution de l'application j'ai l'erreur ci-dessous dans le LogCat :

    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
     
    07-31 11:01:23.318: E/AndroidRuntime(1904): FATAL EXCEPTION: main
    07-31 11:01:23.318: E/AndroidRuntime(1904): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.malambi.beamme/com.malambi.beamme.DialogAlert}: java.lang.InstantiationException: com.malambi.beamme.DialogAlert
    07-31 11:01:23.318: E/AndroidRuntime(1904): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
    07-31 11:01:23.318: E/AndroidRuntime(1904): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    07-31 11:01:23.318: E/AndroidRuntime(1904): 	at android.app.ActivityThread.access$2300(ActivityThread.java:125)
    07-31 11:01:23.318: E/AndroidRuntime(1904): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
    07-31 11:01:23.318: E/AndroidRuntime(1904): 	at android.os.Handler.dispatchMessage(Handler.java:99)
    07-31 11:01:23.318: E/AndroidRuntime(1904): 	at android.os.Looper.loop(Looper.java:123)
    07-31 11:01:23.318: E/AndroidRuntime(1904): 	at android.app.ActivityThread.main(ActivityThread.java:4627)
    07-31 11:01:23.318: E/AndroidRuntime(1904): 	at java.lang.reflect.Method.invokeNative(Native Method)
    07-31 11:01:23.318: E/AndroidRuntime(1904): 	at java.lang.reflect.Method.invoke(Method.java:521)
    07-31 11:01:23.318: E/AndroidRuntime(1904): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:885)
    07-31 11:01:23.318: E/AndroidRuntime(1904): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
    07-31 11:01:23.318: E/AndroidRuntime(1904): 	at dalvik.system.NativeStart.main(Native Method)
    07-31 11:01:23.318: E/AndroidRuntime(1904): Caused by: java.lang.InstantiationException: com.malambi.beamme.DialogAlert
    07-31 11:01:23.318: E/AndroidRuntime(1904): 	at java.lang.Class.newInstanceImpl(Native Method)
    07-31 11:01:23.318: E/AndroidRuntime(1904): 	at java.lang.Class.newInstance(Class.java:1429)
    07-31 11:01:23.318: E/AndroidRuntime(1904): 	at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
    07-31 11:01:23.318: E/AndroidRuntime(1904): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
    07-31 11:01:23.318: E/AndroidRuntime(1904): 	... 11 more
    voici mon Activité
    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
     
     
     
    import android.os.Bundle;
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.util.Log;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;
     
    public class DialogAlert extends Activity {
     
     
    	Context context ;
     
    	public DialogAlert (Context context){
    		this.context = context ;
    	}
     
     
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.alert);
    		showDialog() ;
    	}
     
     
    	/*@Override
    	public boolean onCreateOptionsMenu(Menu menu) {
    		// Inflate the menu; this adds items to the action bar if it is present.
    		getMenuInflater().inflate(R.menu.dialog_alert, menu);
    		return true;
    	}*/
     
     
    	public void showDialog() {
     
    		Button btnAlert = (Button) findViewById(R.id.button1);
     
    		btnAlert.setOnClickListener(new View.OnClickListener() {
     
    			public void onClick(View arg0) {
    				// Creating alert Dialog with one Button
     
    				AlertDialog alertDialog = new AlertDialog.Builder(DialogAlert.this).create();
     
    				// Setting Dialog Title
    				alertDialog.setTitle("Alert Dialog");
     
    				// Setting Dialog Message
    				alertDialog.setMessage("Welcome to AndroidHive.info");
     
    				// Setting Icon to Dialog
    				alertDialog.setIcon(R.drawable.tick);
     
    				// Setting OK Button
    				alertDialog.setButton("OK",
    						new DialogInterface.OnClickListener() {
     
    							public void onClick(DialogInterface dialog,
    									int which) {
    								// Write your code here to execute after dialog
    								// closed
    								Toast.makeText(getApplicationContext(),
    										"You clicked on OK", Toast.LENGTH_SHORT)
    										.show();
    							}
    						});
     
    				// Showing Alert Message
    				alertDialog.show();
     
    			}
    		});
    	}
     
    	public void doPositiveClick() {
    		// Do stuff here.
    		Toast.makeText(context, "Click sur Ok", Toast.LENGTH_SHORT).show();	
    		Log.i("FragmentAlertDialog", "Positive click!");
    	}
     
    	public void doNegativeClick() {
    		// Do stuff here.
    		Toast.makeText(context, "click sur Annuler", Toast.LENGTH_SHORT).show();
     
    		Log.i("FragmentAlertDialog", "Negative click!");
     
     
     
     
    	}
    }

    j'effectue le démarrage de cette manière

    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
     
    public void StartActivity(){
    		Intent dialogIntent = new Intent(getBaseContext(), DialogAlert.class);
    		dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    		getApplication().startActivity(dialogIntent);
    	}
    @Override
    	public int onStartCommand(Intent intent, int flags, int startId) {
    		Toast.makeText(this, "Demarrage du service", Toast.LENGTH_SHORT).show();
     
    	      try {
     
    	    	  StartActivity() ;
    			 //bip.playSound(getApplicationContext()) ;
    	    	// tone.startTone(8,8000);
    		} catch (IllegalArgumentException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (SecurityException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IllegalStateException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} 
     
    		//timer.schedule(receivehelper.stock, 1000 , 1000) ;
     
    		return super.onStartCommand(intent, flags, startId);
    	}

  2. #2
    Expert confirmé

    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Février 2007
    Messages
    4 253
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2007
    Messages : 4 253
    Billets dans le blog
    3
    Par défaut
    Il ne faut pas mettre de constructeur dans les activités...

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    	public DialogAlert (Context context){
    		this.context = context ;
    	}
    En plus je ne vois pas l'interêt de récupérer un contexte, vu qu'une activité *EST* un contexte !

Discussions similaires

  1. Réponses: 0
    Dernier message: 31/05/2015, 23h09
  2. Cohabitation service et activité
    Par elmarco06 dans le forum Android
    Réponses: 3
    Dernier message: 15/12/2011, 21h39
  3. Problème de timing Activité/Service
    Par _Xavier_ dans le forum Android
    Réponses: 7
    Dernier message: 13/02/2011, 01h49
  4. Réponses: 2
    Dernier message: 19/01/2011, 11h45
  5. Comment binder un service avec plusieurs activités ?
    Par Spredzy dans le forum Android
    Réponses: 5
    Dernier message: 18/10/2010, 15h47

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo