2011-11-02 3 views
16

Sto tentando di utilizzare le notifiche personalizzate nella mia applicazione Android utilizzando il campione di Google da here (sezione Creating a Custom Notification Layout). Dato che sto usando questo codice esatto, ho ricevuto un numero RuntimeException ogni volta che viene inviata una notifica.Notifica personalizzata: java.lang.RuntimeException: lunghezza array errata

mio stack trace:

FATAL EXCEPTION: main 
java.lang.RuntimeException: bad array lengths 
    at android.os.Parcel.readIntArray(Parcel.java:677) 
    at android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:369) 
    at android.app.NotificationManager.notify(NotificationManager.java:110) 
    at android.app.NotificationManager.notify(NotificationManager.java:90) 
    at com.****.service.UpdateFeedService.notifyUpdateProgress(UpdateFeedService.java:266) 
    at com.****.service.task.PodcastUpdaterTask.onProgressUpdate(PodcastUpdaterTask.java:63) 
    at com.****.service.task.PodcastUpdaterTask.onProgressUpdate(PodcastUpdaterTask.java:1) 
    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:432) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:144) 
    at android.app.ActivityThread.main(ActivityThread.java:4937) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:521) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
    at dalvik.system.NativeStart.main(Native Method) 

Il mio codice:

private final Intent updateInProgressIntent = new Intent(this.context, PodcastListActivity.class); 
private RemoteViews updateInProgressContentView = null; 
private PendingIntent updateInProgressPendingIntent = null; 
private Notification updateInProgressNotification = null; 
... 
    @Override 
    public void onCreate() { 
     super.onCreate();  
... 
     this.updateInProgressPendingIntent = PendingIntent.getActivity(this, UPDATE_INPROGRESS_NOTIFICATION_ID, 
     this.updateInProgressIntent, PendingIntent.FLAG_UPDATE_CURRENT);  
     this.updateInProgressContentView = new RemoteViews(getPackageName(), R.layout.custom_notification); 
... 
    } 

    public void notifyUpdateProgress(final int index, final int size, final Podcast podcast) { 

     this.updateInProgressContentView.setImageViewBitmap(
       R.id.image, ActivityHelper.getBitmap(context, podcast.getThumbnailAsset())); 
     this.updateInProgressContentView.setTextViewText(R.id.title, "some msg"); 
     this.updateInProgressContentView.setTextViewText(R.id.text, podcast.getName()); 
     this.updateInProgressNotification.contentView = this.updateInProgressContentView;  
     this.updateInProgressNotification.contentIntent = this.updateInProgressPendingIntent;    
     this.notificationManager.notify(UPDATE_INPROGRESS_NOTIFICATION_ID, this.updateInProgressNotification); 

     ... 
     }  

Se si sostituisce la notifica personalizzato con uno standard (con setLatestEventInfo()) non ho problemi.

risposta

5

Ok, finalmente trovo la soluzione. Non è possibile riutilizzare lo stesso oggetto RemoteView come ho fatto io! Stavo creando RemoteView nel metodo onCreate(), quindi stavo impostando i suoi attributi in notifyUpdateProgress(). Se creo l'oggetto in notifyUpdateProgress() appena prima di usarlo, non ho più eccezioni.

+1

A volte si verifica un arresto anomalo anche con un nuovo RemoteView, quindi penso che sia correlato alle bitmap come menzionato da @Brigham – tokudu

+0

@ La risposta di Brigham è corretta. Potresti aver perso oggetti 'Bitmap' attorno al tuo' RemoteView'. –

18

Se si riscontra questo problema, esaminare la dimensione della bitmap che si sta utilizzando per l'immagine di notifica.

Se è troppo grande, è possibile che venga visualizzato questo errore, nonché un OutOfMemoryError sul lato del sistema Android.

+0

m non utilizza alcuna immagine a parte l'icona dell'app che è stata impostata nel layout – DjHacktorReborn