Cannot resolve PendingIntent.getActivity()

笑着哭i 提交于 2020-01-14 09:34:08

问题


I am trying to make a custom notification and cannot resolve this.

public void remNotifyClicked (View view){
    notification.setSmallIcon(R.drawable.ic_launcher);
    notification.setTicker("Ticker");
    notification.setContentTitle("Notification");
    notification.setContentText("Congratulation!");
    notification.setWhen(System.currentTimeMillis());


    Intent i = new Intent(this, SecondActivity.class);
    notification.setContentIntent( new PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT));

    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    nm.notify(uniqueId, notification.build());

}

The problem here is, "getActivity" is showing as error(red colored) and it says it cannot resolve the symbol(when hovered over it). Thanks.

PS: I use Android Studio.


回答1:


Replace

notification.setContentIntent( new PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT));

with

notification.setContentIntent(PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT));

getActivity() is a static method of the class PendingIntent and does not require an instance of PendingIntent in order to be invoked.

Try this. This will work.



来源:https://stackoverflow.com/questions/28658402/cannot-resolve-pendingintent-getactivity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!