Autore Topic: Problema nel far partire un Dialog dentro un'activity dopo ricezione intent  (Letto 845 volte)

Offline psicomant

  • Utente junior
  • **
  • Post: 95
  • Respect: 0
    • Mostra profilo
  • Sistema operativo:
    OSX 10.6.5
Salve,
il mio problema e' il seguente.
Nell'activity dove dovrei visualizzare un dialog duccessivamente alla ricezione di un intent, gestito tramite BroadcastIntent, faccio cosi:
Codice (Java): [Seleziona]
[...]
private final BroadcastReceiver mIntentReceiverNormal = new  BroadcastReceiver(){
                @Override
                public void onReceive(Context ctx, Intent intent) {
                        Log.e(TAG, "INTENT RECEIVED after a normal boot phase end!");
                        progressDialog.dismiss();
                        showNotificationNormal();
                        //finish();
                }
        };

        private void showNotificationNormal() {
                final Dialog dialog = new Dialog(StartStop.this);
                dialog.setContentView(R.layout.generalsettingsmaindialog);
                dialog.setTitle("Service Infos");
                dialog.setCancelable(true);

                // there are a lot of settings, for dialog, check them all out!
                // set up text
                TextView text = (TextView) dialog.findViewById(R.id.TextView01);
                text.setText("Boot phase ended well!!\n" +
                                "Now your device start to send data to CloudSensor service.\n" +
                                "Thank you for your patience.\n" +
                "Have a nice day!\n");
                // set up image view
                ImageView img = (ImageView) dialog.findViewById(R.id.ImageView01);
                img.setImageResource(R.drawable.csamscloudynexus);
                // set up button
                Button button = (Button) dialog.findViewById(R.id.Button01);
                button.setOnClickListener(new OnClickListener() {
                        public void onClick(View v) {
                                dialog.cancel();
                                // close StartStop view
                                finish();
                        }
                });
                // now that the dialog is set up, it's time to show it
                dialog.show();
        }
       
        private final BroadcastReceiver mIntentReceiverError = new  BroadcastReceiver(){
                @Override
                public void onReceive(Context ctx, Intent intent){
                        Log.e(TAG, "INTENT RECEIVED after an ERROR boot phase end!");
                        progressDialog.dismiss();
                        showNotificationError(ctx, intent);
                        //finish();
                }
        };
       
        private void showNotificationError(Context ctx, Intent i) {
                final Dialog dialog = new Dialog(StartStop.this);
                dialog.setContentView(R.layout.generalsettingsmaindialog);
                dialog.setTitle("Service Infos");
                dialog.setCancelable(true);

                // there are a lot of settings, for dialog, check them all out!
                // set up text
                TextView text = (TextView) dialog.findViewById(R.id.TextView01);
                Log.v(TAG, "Creating a String to show on dialog");
                StringBuilder sb = new StringBuilder();
                sb.append("Some errors during Boot phase!!\n");
                sb.append(i.getIntExtra("NOT_REGISTRED", 0)+" not registered sensors over "+i.getIntExtra("TO_REGISTER", 0)+" enabled by you.\n");
                int[] indexes = i.getIntArrayExtra("INDEX_ARRAY");
                int[] registred = i.getIntArrayExtra("REGISTRATION_ARRAY");
                for(int ind = 0; ind < i.getIntExtra("TO_REGISTER", 0); ind++){
                        if(registred[ind] == 0){
                                Log.v(TAG, "A string for every sensor not registered");
                                sb.append("Sensor with index "+indexes[ind]+"not registered.\n");
                        }
                }
                sb.append("Retry another time.\nThank you.");
                text.setText(sb);
                // set up image view
                ImageView img = (ImageView) dialog.findViewById(R.id.ImageView01);
                img.setImageResource(R.drawable.csamscloudynexus);
                // set up button
                Button button = (Button) dialog.findViewById(R.id.Button01);
                button.setOnClickListener(new OnClickListener() {
                        public void onClick(View v) {
                                dialog.cancel();
                                pref = getSharedPreferences(PREF_USER_CSAMS, Context.MODE_PRIVATE);
                                SharedPreferences.Editor editor = pref.edit();
                                editor.putBoolean(START, false);
                                editor.commit();
                                // terminate StartStop activity
                                finish();
                        }
                });
                // now that the dialog is set up, it's time to show it
                dialog.show();
        }
       
        @Override
        public void onStart(){
                Log.e(TAG, "REGISTER BROADCAST RECEIVER");
                // register intent receiver to listen for a normal boot end
                registerReceiver(mIntentReceiverNormal, new IntentFilter(CUSTOM_INTENT_NORMAL));
                // register intent receiver to listen for an error boot end
                registerReceiver(mIntentReceiverError, new IntentFilter(CUSTOM_INTENT_ERROR));
                super.onStart();
        }
[...]

Ma quando arriva l'intent, crasha tutto e nel log cat ottengo:
Codice: [Seleziona]
01-15 19:00:28.293: ERROR/AndroidRuntime(4531): Uncaught handler: thread main exiting due to uncaught exception
01-15 19:00:28.363: ERROR/AndroidRuntime(4531): java.lang.RuntimeException: Error receiving broadcast Intent { act=STOP_NOTIFICATION_ERROR (has extras) } in cloudsensor.csams.UserInterface.StartStop$2@43b9b0e0
01-15 19:00:28.363: ERROR/AndroidRuntime(4531):     at android.app.ActivityThread$PackageInfo$ReceiverDispatcher$Args.run(ActivityThread.java:765)
01-15 19:00:28.363: ERROR/AndroidRuntime(4531):     at android.os.Handler.handleCallback(Handler.java:587)
01-15 19:00:28.363: ERROR/AndroidRuntime(4531):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-15 19:00:28.363: ERROR/AndroidRuntime(4531):     at android.os.Looper.loop(Looper.java:123)
01-15 19:00:28.363: ERROR/AndroidRuntime(4531):     at android.app.ActivityThread.main(ActivityThread.java:4363)
01-15 19:00:28.363: ERROR/AndroidRuntime(4531):     at java.lang.reflect.Method.invokeNative(Native Method)
01-15 19:00:28.363: ERROR/AndroidRuntime(4531):     at java.lang.reflect.Method.invoke(Method.java:521)
01-15 19:00:28.363: ERROR/AndroidRuntime(4531):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
01-15 19:00:28.363: ERROR/AndroidRuntime(4531):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
01-15 19:00:28.363: ERROR/AndroidRuntime(4531):     at dalvik.system.NativeStart.main(Native Method)
01-15 19:00:28.363: ERROR/AndroidRuntime(4531): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@43b85da0 is not valid; is your activity running?
01-15 19:00:28.363: ERROR/AndroidRuntime(4531):     at android.view.ViewRoot.setView(ViewRoot.java:468)
01-15 19:00:28.363: ERROR/AndroidRuntime(4531):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
01-15 19:00:28.363: ERROR/AndroidRuntime(4531):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
01-15 19:00:28.363: ERROR/AndroidRuntime(4531):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
01-15 19:00:28.363: ERROR/AndroidRuntime(4531):     at android.app.Dialog.show(Dialog.java:239)
01-15 19:00:28.363: ERROR/AndroidRuntime(4531):     at cloudsensor.csams.UserInterface.StartStop.showNotificationError(StartStop.java:143)
01-15 19:00:28.363: ERROR/AndroidRuntime(4531):     at cloudsensor.csams.UserInterface.StartStop.access$2(StartStop.java:103)
01-15 19:00:28.363: ERROR/AndroidRuntime(4531):     at cloudsensor.csams.UserInterface.StartStop$2.onReceive(StartStop.java:98)
01-15 19:00:28.363: ERROR/AndroidRuntime(4531):     at android.app.ActivityThread$PackageInfo$ReceiverDispatcher$Args.run(ActivityThread.java:754)
01-15 19:00:28.363: ERROR/AndroidRuntime(4531):     ... 9 more

Qualche suggerimento?

Grazie

Offline psicomant

  • Utente junior
  • **
  • Post: 95
  • Respect: 0
    • Mostra profilo
  • Sistema operativo:
    OSX 10.6.5
Re:Problema nel far partire un Dialog dentro un'activity dopo ricezione intent
« Risposta #1 il: 16 Gennaio 2011, 17:56:31 CET »
0
...in realta' il codice e' corretto, perche' poi dopo qualche prova andava!  :-P

Non ho ben identificato il problema che si verificava, ma ora e' tutto ok!

Quindi topi autorisolto (bisognerebbe mettere anche questo tasto :P )