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

Composants graphiques Android Discussion :

Toast sur Activity à partir d'une classe abstraite


Sujet :

Composants graphiques Android

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Inscrit en
    Octobre 2009
    Messages
    24
    Détails du profil
    Informations forums :
    Inscription : Octobre 2009
    Messages : 24
    Par défaut Toast sur Activity à partir d'une classe abstraite
    Bonjour,

    J'ai un petit souci.
    J'ai plusieurs activités qui héritent d'une classe abstraite (c'est pour faire de la lecture NFC).
    Voici le code de l'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
     
    public class simple_Activity extends BaseActivityRfidScan {
     
     
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            ActionBar actionBar = getSupportActionBar();
            //ActionBar actionBar = null;
            setContentView(R.layout.simple_Activity);
            Bundle b = getIntent().getExtras();
            String value = "";
     
             if(b != null)
    La classe abstraite:

    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
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
     
     
    public abstract class BaseActivityRfidScan extends AppCompatActivity {
    //public abstract class BaseActivityRfidScan extends Activity {
     
     
     
     
     
     
        @Override
        public void onCreate(Bundle savedInstanceState) {
            ActionBar actionBar = getSupportActionBar();
            MyContext=this.getApplicationContext();
            super.onCreate(savedInstanceState);
            setContentView(getLayoutResourceId());
            grey=ContextCompat.getDrawable(this, R.drawable.nfc_grey).getConstantState();
            mp = MediaPlayer.create(this, R.raw.jackpot);
            first_cycle=true;
            // Check if NFC is supported and enabled
            nfcAdapter = NfcAdapter.getDefaultAdapter(this);
            try {
                //Log.d("NFC supporté", (Boolean.toString(nfcAdapter != null)));
                if (nfcAdapter!=null) {
                    //Log.d("NFC activé", (Boolean.toString(nfcAdapter.isEnabled())));
                }
            } finally {
     
             }
            //Log.d("on create firstcycle", first_cycle.toString());
            nfcPendingIntent = PendingIntent.getActivity(this, 0,
                    new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
     
            b = getIntent().getExtras();
            numbertags = b.getInt("numbertags");
            String value="";
     
                value = b.getString("title");
            //Log.d("title:", value);
            TextView title = (TextView) findViewById(R.id.title);
            Log.d("title text view:", title.toString());
            title.setText(value);
            value = b.getString("comment");
            TextView comment = (TextView) findViewById(R.id.comment);
            if (comment!=null){comment.setText(value);}
            value = b.getString("picture");
            ImageView picture = (ImageView) findViewById(R.id.picture);
            if (picture!=null){
                int resourceID = getResources().getIdentifier(
                        value,
                        "drawable",
                        getPackageName()
                );
                picture.setImageResource(resourceID);
     
            }
     
            if (actionBar != null) {
                actionBar.setHomeButtonEnabled(true);
                actionBar.setDisplayHomeAsUpEnabled(true);
                actionBar.setDisplayShowTitleEnabled(true);
                actionBar.setTitle("activité");
              }
     
            nfcgreen = getResources().getIdentifier(
                    "nfc_green",
                    "drawable",
                    getPackageName()
            );
            nfcgray= getResources().getIdentifier(
                    "nfc_grey",
                    "drawable",
                    getPackageName()
            );
     
        }
     
        private void resolveIntent(Intent intent) {
            if (intent.getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED)) {
                int value_task = b.getInt("task_number");
                EventBus.getDefault().post(new EventNfcDetect(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID),value_task));
            }
        }
     
     
        @Override
        public void onNewIntent(Intent intent) {
            super.onNewIntent(intent);
            setIntent(intent);
            Log.d("NFC", "découvert");
            if (intent != null)
            {
                resolveIntent(intent);
            }
     
        }
     
        @Override
        protected void onResume() {
            super.onResume();
            mem_sound=false;
            if (nfcAdapter!=null) {
                nfcAdapter.enableForegroundDispatch(this, nfcPendingIntent, null, null);
            }
            EventBus.getDefault().register(this);
            timer = new Timer();
            timer.scheduleAtFixedRate(new TimerTask() {
                @Override
                public void run() {
                    int valueint = b.getInt("db_offset");
                    //Log.d("receive test",Integer.toString(valueint));
                    EventBus.getDefault().post(new EventTaskReadRequest(valueint));
                }
            }, 0, 150);
     
     
     
        }
     
     
     
        @Override
        protected void onPause() {
            if (timer!=null){
                timer.cancel();
                timer=null;
            }
     
            super.onPause();
            if (nfcAdapter != null) {
                nfcAdapter.disableForegroundDispatch(this);
                EventBus.getDefault().unregister(this);
            }
            //Log.d("on pause firstcycle", first_cycle.toString());
        }
     
     
     
     
       public void play_sound(){
           Log.d("stop_!!","stop");
            if (mem_sound){
                //Log.d("stop_soud!!","stop");
                mp.stop();
           }
            else{
                mp.start();
                mem_sound=true;
            }
        }
     
        protected abstract int getLayoutResourceId();
     
        public boolean GetBit(byte thebyte, int position)
        {
            return (1 == ((thebyte >> position) & 1));
        }
     
     
        public static String bytesToHex(byte[] bytes) {
            char[] hexChars = new char[bytes.length * 2];
            for (int j = 0; j < bytes.length; j++) {
                int v = bytes[j] & 0xFF;
                hexChars[j * 2] = HEX_ARRAY[v >>> 4];
                hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
            }
            return new String(hexChars);
        }
     
     
     
        public void onEvent(SendResultNfc event) {
            if (event != null) {
                int i=1;
     
                //Log.d("on event firstcycle", first_cycle.toString());
                // code à améliorer pour factoriser
                while(i<=numbertags){
                    switch (i){
                        case 1: ImageView nfc1 = (ImageView) findViewById(R.id.rfid1);
                                if (GetBit(event.getMyResultNfc(),0)){
                                    if(grey==nfc1.getDrawable().getConstantState() && !first_cycle){
                                        play_sound();
                                    }
                                    nfc1.setImageResource(nfcgreen);
                                } else
                                {
                                    nfc1.setImageResource(nfcgray);
                                }
                                break;
     
                        case 2: ImageView nfc2 = (ImageView) findViewById(R.id.rfid2);
                            if (GetBit(event.getMyResultNfc(),1)){
                                if(grey==nfc2.getDrawable().getConstantState() && !first_cycle){
                                    play_sound();
                                }
                                nfc2.setImageResource(nfcgreen);
                            } else
                            {
                                nfc2.setImageResource(nfcgray);
                            }
                            break;
     
                        case 3: ImageView nfc3 = (ImageView) findViewById(R.id.rfid3);
                            if (GetBit(event.getMyResultNfc(),2)){
                                if(grey==nfc3.getDrawable().getConstantState() && !first_cycle){
                                    play_sound();
                                }
                                nfc3.setImageResource(nfcgreen);
                            } else
                            {
                                nfc3.setImageResource(nfcgray);
                            }
                            break;
     
                        case 4: ImageView nfc4 = (ImageView) findViewById(R.id.rfid4);
                            if (GetBit(event.getMyResultNfc(),3)){
                                if(grey==nfc4.getDrawable().getConstantState()&& !first_cycle){
                                    play_sound();
                                }
                                nfc4.setImageResource(nfcgreen);
                            } else
                            {
                                nfc4.setImageResource(nfcgray);
                            }
                            break;
     
                        case 5: ImageView nfc5 = (ImageView) findViewById(R.id.rfid5);
                            if (GetBit(event.getMyResultNfc(),4)){
                                if(grey==nfc5.getDrawable().getConstantState()&& !first_cycle){
                                    play_sound();
                                }
                                nfc5.setImageResource(nfcgreen);
                            } else
                            {
                                nfc5.setImageResource(nfcgray);
                            }
                            break;
     
                        case 6: ImageView nfc6 = (ImageView) findViewById(R.id.rfid6);
                            if (GetBit(event.getMyResultNfc(),5)){
                                if(grey==nfc6.getDrawable().getConstantState()&& !first_cycle){
                                    play_sound();
                                }
                                nfc6.setImageResource(nfcgreen);
                            } else
                            {
                                nfc6.setImageResource(nfcgray);
                            }
                            break;
     
                        case 7: ImageView nfc7 = (ImageView) findViewById(R.id.rfid7);
                            if (GetBit(event.getMyResultNfc(),6)){
                                if(grey==nfc7.getDrawable().getConstantState()&& !first_cycle){
                                    play_sound();
                                }
                                nfc7.setImageResource(nfcgreen);
                            } else
                            {
                                nfc7.setImageResource(nfcgray);
                            }
                            break;
     
                        default: break;
                    }
                    i+=1;
                }
                if (GetBit(event.getMyResultScan(),0)){
                    Log.d("rfid", "connu " + this.getClass().getName());
                    Log.d("rfid", "connu " + this.getApplicationContext().getPackageName());
                    Toast.makeText(this, "connu ", Toast.LENGTH_SHORT).show();
     
                }
     
                if (GetBit(event.getMyResultScan(),1)){
                    Log.d("rfid", "inconnu " + this.getClass().getName());
                    Log.d("rfid", "connu " + this.getApplicationContext().getPackageName());
     
                    Toast.makeText(this, "inconnu ", Toast.LENGTH_SHORT).show();
     
                }
     
            }
            first_cycle=false;
     
        }
    }


    et voici l'erreur:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
     Could not dispatch event: class com.example.mid1_v1.SendResultNfc to subscribing class class com.example.mid1_v1.simple_Activity
        java.lang.RuntimeException: Can't toast on a thread that has not called Looper.prepare()
    J'ai l'impression que c'est le contexte qui ne va pas mais j'ai du mal...
    Merci pour votre aide.

  2. #2
    Membre averti
    Inscrit en
    Octobre 2009
    Messages
    24
    Détails du profil
    Informations forums :
    Inscription : Octobre 2009
    Messages : 24
    Par défaut RESOLU
    j'ai résolu par ceci:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
     
    if (GetBit(event.getMyResultScan(),1)){
                    Log.d("rfid", "inconnu " + this.getClass().getName());
                    Log.d("rfid", "connu " + this.getApplicationContext().getPackageName());
     
                    //Toast.makeText(this, "inconnu ", Toast.LENGTH_SHORT).show();
                    this.runOnUiThread(new Runnable() {
                        public void run() {
                            Toast.makeText(this.getApplicationContext(), "inconnu", Toast.LENGTH_SHORT).show();
                        }
                    });
     
                }
    Merci quand même

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 1
    Dernier message: 02/10/2016, 22h51
  2. Introspection sur une classe abstraite
    Par sanchou dans le forum Général Java
    Réponses: 2
    Dernier message: 22/04/2010, 14h16
  3. Réponses: 3
    Dernier message: 18/11/2009, 16h48
  4. Message sur une form a partir d'une class
    Par topolino dans le forum Windows Forms
    Réponses: 4
    Dernier message: 10/06/2009, 08h57
  5. Réponses: 6
    Dernier message: 21/05/2006, 20h37

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