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 :

App simple créée avec android studio crash


Sujet :

Android

  1. #1
    Candidat au Club
    Homme Profil pro
    Defense
    Inscrit en
    Octobre 2024
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Charente (Poitou Charente)

    Informations professionnelles :
    Activité : Defense

    Informations forums :
    Inscription : Octobre 2024
    Messages : 3
    Par défaut App simple créée avec android studio crash
    bonjour,
    j'ai crée une simple application sur android studio dont la fonction est de lire un tag nfc et de le comparer a une database.
    je ne suis pas du domaine ce qui n'aide pas du tout...
    Bref après avoir corrigé beaucoup d'erreurs lors de build de l'app je suis confronté à un autre problème.

    Sur mon téléphone, l'app crash dès l'ouverture (android 8.1)
    idem sur d'autres téléphone (android 13 et +)

    Cependant sur ma tablette honor celle-ci s'ouvre sans problème. ne pouvant pas tester l'appli puisque la tablette n'a pas de NFC je suis un peu bloqué.
    De plus, je ne peux pas brancher mon tel en usb pour le mode debogage...

    J'ai tenté une version app-debug.apk puis app-release.apk puis une version signée etc...

    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
    1-29 18:02:02.509 10182 25204 25204 E AndroidRuntime: FATAL EXCEPTION: main
    11-29 18:02:02.509 10182 25204 25204 E AndroidRuntime: Process: com.example.simplicims, PID: 25204
    11-29 18:02:02.509 10182 25204 25204 E AndroidRuntime: java.lang.RuntimeException: Unable to resume activity {com.example.simplicims/com.example.simplicims.MainActivity}: java.lang.NullPointerException
    11-29 18:02:02.509 10182 25204 25204 E AndroidRuntime: 	at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3622)
    11-29 18:02:02.509 10182 25204 25204 E AndroidRuntime: 	at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3662)
    11-29 18:02:02.509 10182 25204 25204 E AndroidRuntime: 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2903)
    11-29 18:02:02.509 10182 25204 25204 E AndroidRuntime: 	at android.app.ActivityThread.-wrap11(Unknown Source:0)
    11-29 18:02:02.509 10182 25204 25204 E AndroidRuntime: 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1623)
    11-29 18:02:02.509 10182 25204 25204 E AndroidRuntime: 	at android.os.Handler.dispatchMessage(Handler.java:106)
    11-29 18:02:02.509 10182 25204 25204 E AndroidRuntime: 	at android.os.Looper.loop(Looper.java:164)
    11-29 18:02:02.509 10182 25204 25204 E AndroidRuntime: 	at android.app.ActivityThread.main(ActivityThread.java:6548)
    11-29 18:02:02.509 10182 25204 25204 E AndroidRuntime: 	at java.lang.reflect.Method.invoke(Native Method)
    11-29 18:02:02.509 10182 25204 25204 E AndroidRuntime: 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    11-29 18:02:02.509 10182 25204 25204 E AndroidRuntime: 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:865)
    11-29 18:02:02.509 10182 25204 25204 E AndroidRuntime: Caused by: java.lang.NullPointerException
    11-29 18:02:02.509 10182 25204 25204 E AndroidRuntime: 	at android.nfc.NfcAdapter.enableForegroundDispatch(NfcAdapter.java:1319)
    11-29 18:02:02.509 10182 25204 25204 E AndroidRuntime: 	at com.example.simplicims.MainActivity.onResume(MainActivity.kt:49)
    11-29 18:02:02.509 10182 25204 25204 E AndroidRuntime: 	at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1355)
    11-29 18:02:02.509 10182 25204 25204 E AndroidRuntime: 	at android.app.Activity.performResume(Activity.java:7131)
    11-29 18:02:02.509 10182 25204 25204 E AndroidRuntime: 	at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3597)
    11-29 18:02:02.509 10182 25204 25204 E AndroidRuntime: 	... 10 more
    11-29 18:02:04.780 10182 25228 25228 E AndroidRuntime: FATAL EXCEPTION: main
    11-29 18:02:04.780 10182 25228 25228 E AndroidRuntime: Process: com.example.simplicims, PID: 25228
    11-29 18:02:04.780 10182 25228 25228 E AndroidRuntime: java.lang.RuntimeException: Unable to resume activity {com.example.simplicims/com.example.simplicims.MainActivity}: java.lang.NullPointerException
    11-29 18:02:04.780 10182 25228 25228 E AndroidRuntime: 	at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3622)
    11-29 18:02:04.780 10182 25228 25228 E AndroidRuntime: 	at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3662)
    11-29 18:02:04.780 10182 25228 25228 E AndroidRuntime: 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2903)
    11-29 18:02:04.780 10182 25228 25228 E AndroidRuntime: 	at android.app.ActivityThread.-wrap11(Unknown Source:0)
    11-29 18:02:04.780 10182 25228 25228 E AndroidRuntime: 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1623)
    11-29 18:02:04.780 10182 25228 25228 E AndroidRuntime: 	at android.os.Handler.dispatchMessage(Handler.java:106)
    11-29 18:02:04.780 10182 25228 25228 E AndroidRuntime: 	at android.os.Looper.loop(Looper.java:164)
    11-29 18:02:04.780 10182 25228 25228 E AndroidRuntime: 	at android.app.ActivityThread.main(ActivityThread.java:6548)
    11-29 18:02:04.780 10182 25228 25228 E AndroidRuntime: 	at java.lang.reflect.Method.invoke(Native Method)
    11-29 18:02:04.780 10182 25228 25228 E AndroidRuntime: 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    11-29 18:02:04.780 10182 25228 25228 E AndroidRuntime: 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:865)
    11-29 18:02:04.780 10182 25228 25228 E AndroidRuntime: Caused by: java.lang.NullPointerException
    11-29 18:02:04.780 10182 25228 25228 E AndroidRuntime: 	at android.nfc.NfcAdapter.enableForegroundDispatch(NfcAdapter.java:1319)
    11-29 18:02:04.780 10182 25228 25228 E AndroidRuntime: 	at com.example.simplicims.MainActivity.onResume(MainActivity.kt:49)
    11-29 18:02:04.780 10182 25228 25228 E AndroidRuntime: 	at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1355)
    11-29 18:02:04.780 10182 25228 25228 E AndroidRuntime: 	at android.app.Activity.performResume(Activity.java:7131)
    11-29 18:02:04.780 10182 25228 25228 E AndroidRuntime: 	at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3597)
    11-29 18:02:04.780 10182 25228 25228 E AndroidRuntime: 	... 10 more
    Cordialement

  2. #2
    Invité de passage
    Homme Profil pro
    Enseignant
    Inscrit en
    Septembre 2015
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Cameroun

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Septembre 2015
    Messages : 3
    Par défaut
    Solution

    Vous devez vérifier que NfcAdapter n'est pas null avant de l'utiliser.

    Correction dans MainActivity.kt
    override fun onResume() {
    super.onResume()

    if (nfcAdapter != null) {
    nfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techListsArray)
    }
    }

  3. #3
    Invité de passage
    Homme Profil pro
    Enseignant
    Inscrit en
    Septembre 2015
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Cameroun

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Septembre 2015
    Messages : 3
    Par défaut
    nfcAdapter = NfcAdapter.getDefaultAdapter(this)

    if (nfcAdapter == null) {
    Toast.makeText(this, "NFC not supported on this device", Toast.LENGTH_LONG).show()
    }


    if (!nfcAdapter.isEnabled) {
    Toast.makeText(this, "Please enable NFC", Toast.LENGTH_LONG).show()
    }

  4. #4
    Invité de passage
    Homme Profil pro
    Enseignant
    Inscrit en
    Septembre 2015
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Cameroun

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Septembre 2015
    Messages : 3
    Par défaut Application corrigee...
    package com.example.simplicims

    import android.app.PendingIntent
    import android.content.Intent
    import android.content.IntentFilter
    import android.nfc.NfcAdapter
    import android.nfc.Tag
    import android.os.Bundle
    import android.provider.Settings
    import android.widget.Toast
    import androidx.appcompat.app.AppCompatActivity

    class MainActivity : AppCompatActivity() {

    private var nfcAdapter: NfcAdapter? = null
    private var pendingIntent: PendingIntent? = null
    private var intentFiltersArray: Array<IntentFilter>? = null
    private var techListsArray: Array<Array<String>>? = null

    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    // Initialisation NFC
    nfcAdapter = NfcAdapter.getDefaultAdapter(this)

    if (nfcAdapter == null) {
    Toast.makeText(this, "NFC not supported on this device", Toast.LENGTH_LONG).show()
    finish()
    return
    }

    if (!nfcAdapter!!.isEnabled) {
    Toast.makeText(this, "Please enable NFC", Toast.LENGTH_LONG).show()

    val intent = Intent(Settings.ACTION_NFC_SETTINGS)
    startActivity(intent)
    }

    // Intent pour NFC foreground dispatch
    pendingIntent = PendingIntent.getActivity(
    this,
    0,
    Intent(this, javaClass).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),
    PendingIntent.FLAG_MUTABLE
    )

    // Filtre NFC
    val tagDetected = IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED)
    intentFiltersArray = arrayOf(tagDetected)

    techListsArray = arrayOf(
    arrayOf(android.nfc.tech.NfcA::class.java.name),
    arrayOf(android.nfc.tech.NfcB::class.java.name),
    arrayOf(android.nfc.tech.NfcF::class.java.name),
    arrayOf(android.nfc.tech.NfcV::class.java.name)
    )
    }

    override fun onResume() {
    super.onResume()

    nfcAdapter?.enableForegroundDispatch(
    this,
    pendingIntent,
    intentFiltersArray,
    techListsArray
    )
    }

    override fun onPause() {
    super.onPause()

    nfcAdapter?.disableForegroundDispatch(this)
    }

    override fun onNewIntent(intent: Intent) {
    super.onNewIntent(intent)

    val tag: Tag? = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG)

    if (tag != null) {
    Toast.makeText(this, "NFC Tag Detected", Toast.LENGTH_SHORT).show()

    val tagId = tag.id.joinToString("") { "%02X".format(it) }

    Toast.makeText(this, "Tag ID: $tagId", Toast.LENGTH_LONG).show()
    }
    }
    }

Discussions similaires

  1. App de streaming avec Android studio
    Par ltatiekam dans le forum Android
    Réponses: 0
    Dernier message: 18/12/2019, 02h03
  2. Programmer en C/C++ avec Android studio
    Par sisi_06 dans le forum Android Studio
    Réponses: 6
    Dernier message: 19/08/2015, 17h26
  3. Réponses: 4
    Dernier message: 28/05/2015, 10h18
  4. Jeu en reseau avec android studio?
    Par mattiew dans le forum Android Studio
    Réponses: 1
    Dernier message: 26/05/2015, 18h52
  5. probleme avec android studio
    Par kapac dans le forum Android Studio
    Réponses: 0
    Dernier message: 24/04/2014, 16h04

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