Je me suis lancé depuis peu sur AndroidStudio. Mon application au début était basique et fonctionnait bien entre les changements d'activités.
Cependant j'ai essayé d'utiliser les fragments et la c'est galère.. Lorsque je ne déclare pas le bouton d'un des fragments dans le java, l'appli se lance. Mais quand j'applique cette méthode sur l'activité principale, l'appli crash dès le démarrage..
Ca fait des heures que j'essaye d'autres solutions, en vain rien ne fonctionne..
Si quelqu'un peut m'aider, voici le logcat ci dessous lors du démarrage et crash de l'appli :
12-20 17:35:57.415 7259-7259/info.androidhive.tabsswipe D/AndroidRuntime﹕ Shutting down VM
12-20 17:35:57.416 7259-7259/info.androidhive.tabsswipe E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: info.androidhive.tabsswipe, PID: 7259
java.lang.RuntimeException: Unable to start activity ComponentInfo{info.androidhive.tabsswipe/info.androidhive.tabsswipe.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at info.androidhive.tabsswipe.MainActivity.onCreate(MainActivity.java:82)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Voila exactement la où ca bug, c'est la où c'est souligné en rouge. Je ne comprends pas pourquoi ça ne marche pas alors que lorsque j'applique cette méthode dans une activité ça marche très bien.. Mon but étant juste d'afficher un autre fragment lors d'un clic sur un bouton..
Merci pour a réponse, mais comment appelle on le context de l'activité ?
Il me met comme erreur 'main activity is not an enclosing class' lorsque je remplace "GameFragment" par "MainActivity" pourtant c'est bien mon activité principale, je comprends pas :/
- Edité par cordlesstuba 24 décembre 2014 à 17:40:15
Désolé de revenir à la charge, mais cette fois si je veux ouvrir un fragment plutôt que "MainActivity3" avec ce même bouton, comme procéder sachant que cette méthode n'est valable que pour une Activity si je ne me trompe pas ?
Il te faut 3 class : celle que tu as la avec ton bouton momentbutton, avec l'intent de ce bouton tu lance la 2e qui extends FragmentActivity (c'est quasi pareil qu'une activity) et dans celle la tu va ajouter ta 3e class qui extends Fragment.
Ton fragment est intégré dans la fragmentactivity tu ne peux pas le lancer tout seul.
The people who are crazy enough to think they can change the world, are the ones who do.
Ok, merci pour l'indication, je vais chercher parce que je ne vois pas du tout la démarche à suivre. C'est pas évident les fragments pour un débutant..
Bah j'ai d'abord réalisé toute la base de mon appli avec des Activités. Je trouvais ça moche le changement d'activité lors de chaque "clic" sur les boutons. J'ai donc découvert les fragments, donc je suis parti sur le concept basique du slide entre 3 fragments de base.
Du coup j'aimerais tout faire en fragment en fait, je trouve ça plus fluide pour la navigation.
Si tu veux faire ça faut + voir du coté des viewpager, c'est encore une autre manière d'intégrer les fragments. Et ça devient encore + compliqué pour un débutant.
L'avantage avec les activités c'est qu'on peut utiliser le bouton back pour revenir au menu précédent.
Sinon tu peux aussi créer quelque chose avec les animations, ça permet de gérer le bouton back, ça évite la transition des activity et c'est + simple que les fragments.
Mais bon y a encore pas mal d'appli qui intègre les intent classique, c'est pas si mal que ça.
The people who are crazy enough to think they can change the world, are the ones who do.
Si je met qu'un bouton, ca marche mais à partir de plusieurs l'application crash, je ne sais pas comment régler le problème. Voici le logcat :
06-07 15:39:27.637 5375-5375/com.anthooop.vandb I/Process: Sending signal. PID: 5375 SIG: 9
06-07 15:56:06.238 5658-5658/com.anthooop.vandb W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
06-07 15:56:06.376 5658-5658/com.anthooop.vandb D/AndroidRuntime: Shutting down VM
06-07 15:56:06.377 5658-5658/com.anthooop.vandb E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.anthooop.vandb, PID: 5658
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.anthooop.vandb/com.anthooop.vandb.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.anthooop.vandb.MainActivity.onCreate(MainActivity.java:35)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
06-07 16:00:47.682 6583-6583/com.anthooop.vandb W/art: Failed to find OatDexFile for DexFile /data/data/com.anthooop.vandb/files/instant-run/dex/slice-slice_8-classes.dex ( canonical path /data/data/com.anthooop.vandb/files/instant-run/dex/slice-slice_8-classes.dex) with checksum 0xd6658911 in OatFile /data/data/com.anthooop.vandb/cache/slice-slice_8-classes.dex
06-07 16:00:49.475 6583-6583/com.anthooop.vandb W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
06-07 16:00:49.557 6583-6636/com.anthooop.vandb D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
[ 06-07 16:00:49.754 6583: 6583 D/ ]
HostConnection::get() New Host Connection established 0x7f2e5bffea80, tid 6583
06-07 16:00:50.156 6583-6583/com.anthooop.vandb D/Atlas: Validating map...
06-07 16:00:50.354 6583-6636/com.anthooop.vandb I/OpenGLRenderer: Initialized EGL, version 1.4
06-07 16:01:01.394 6583-6636/com.anthooop.vandb D/OpenGLRenderer: Enabling debug mode 0
06-07 16:01:01.673 6583-6636/com.anthooop.vandb W/EGL_emulation: eglSurfaceAttrib not implemented
06-07 16:01:01.673 6583-6636/com.anthooop.vandb W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7f2e5252c440, error=EGL_SUCCESS
06-07 16:01:01.770 6583-6583/com.anthooop.vandb I/Choreographer: Skipped 687 frames! The application may be doing too much work on its main thread.
06-07 16:01:09.552 6583-6583/com.anthooop.vandb I/Choreographer: Skipped 332 frames! The application may be doing too much work on its main thread.
06-07 16:02:30.458 6583-6624/com.anthooop.vandb W/art: Failed to find OatDexFile for DexFile /data/data/com.anthooop.vandb/files/instant-run/dex-temp/reload0x0000.dex ( canonical path /data/data/com.anthooop.vandb/files/instant-run/dex-temp/reload0x0000.dex) with checksum 0x075e1f61 in OatFile /data/data/com.anthooop.vandb/cache/reload0x0000.dex
06-07 16:02:30.919 6583-6583/com.anthooop.vandb D/AndroidRuntime: Shutting down VM
06-07 16:02:30.919 6583-6583/com.anthooop.vandb E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.anthooop.vandb, PID: 6583
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.anthooop.vandb/com.anthooop.vandb.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3947)
at android.app.ActivityThread.access$900(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.anthooop.vandb.MainActivity$override.onCreate(MainActivity.java:35)
at com.anthooop.vandb.MainActivity$override.access$dispatch(MainActivity.java)
at com.anthooop.vandb.MainActivity.onCreate(MainActivity.java:0)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3947)
at android.app.ActivityThread.access$900(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
06-07 16:02:30.956 6583-6596/com.anthooop.vandb W/art: Suspending all threads took: 22.929ms
× Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
× Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
Mon site - Mon livre sur le XML - Mon blog
Mon site - Mon livre sur le XML - Mon blog