Notificationmanager causes java.util.Arraylist error cannot be cast to java.lang.Charsequence

Asked

Viewed 143 times

4

I’m making an Android application and I’m having a question while generating a notification.

Here I will be providing an excerpt of the code:

public void gerarNotificacao(View view) { 
  NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

  SimpleDateFormat datas = new SimpleDateFormat("dd/mm/yyyy");

  mDiasdaSem = String.valueOf(datas.format(new Date())); 

  eventoDao = new eventosDao(this);
  eventoDao.open();

  String filtro = mDiasdaSem;
  List<Eventos> eventos = eventoDao.listarEventos(filtro);

  NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
  builder.setTicker("Ticker Texto");
  builder.setTicker( (CharSequence) eventos);
  builder.setSmallIcon(R.drawable.ic_launcher);

  Notification n =   builder.build();

  n.vibrate = new long[]{250, 300, 150, 600};
  nm.notify(R.drawable.ic_launcher, n);

  try
  {
     Uri som = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
     Ringtone toque = RingtoneManager.getRingtone(this, som);
     toque.play();
  }
  catch(Exception e)
  {         
  }
}

The following error occurs:

java.util.Arraylist cannot be cast to java.lang.Charsequence

How to solve?

  • 1

    @Otavio the error occurs because you are not passing the correct type to the method setTicker(). You can’t make a cast of a ArrayList<> for a CharSequence. Post your Event class code as well.

  • Well, I don’t see any ArrayList<>!

1 answer

0

Try to put it that way:

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Notification nt = null;

    nt = new Notification(R.drawable.ic_launcher, "Status Replicação", System.currentTimeMillis());
    nt.flags = Notification.FLAG_AUTO_CANCEL;

    PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(getApplicationContext(), Activity_Principal.class), 0);
    nt.setLatestEventInfo(this, "Status Replicação", "Replicação feita com sucesso, total: "+totalReplicado, pi);

    nt.vibrate = new long[]{250, 300, 150, 600};
    notificationManager.notify((int)Math.round(Math.random()), nt);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.