2015-11-06 66 views
2

我在AndroidStudio 1.4.1中设置了一个通知,并且遇到了setStyle函数的错误。Android NotificationCompat.BigTextStyle错误

这是代码:

NotificationCompat.Builder builder = 
       new NotificationCompat.Builder(this); 
     builder 
       .setSmallIcon(R.drawable.ic_assignment) 
       .setContentTitle("Easteregg found!"); 
     String easter = "You found our Easteregg! Well done"; 
     //custom colour 
     int color = getResources().getColor(R.color.primaryColor); 
     builder.setContentText(easter); 
     builder.setColor(color); 
     builder.setStyle(new NotificationCompat.BigTextStyle(easter)); 
     Notification notification = builder.build(); 
     NotificationManagerCompat.from(this).notify(0, notification); 
     Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); 
     // Vibration pattern 
     long[] pattern = {0, 100, 250, 320}; 
     v.vibrate(pattern, -1); 

当调用builder.setStyle(新NotificationCompat.BigTextStyle(复活节));我收到“BigTextStyle无法应用于java.lang.string”的通知

由于我使用Google提供的样本https://www.youtube.com/watch?v=-iog_fmm6mE,所以我非常困惑。我也尝试了一些基本的bug修复,但没有解决问题,所以我问你。

如果有人能帮助我,我会非常感谢。

在此先感谢。

P.S .:如果我遗漏了一些东西,请通知我有关它,我会尽我所能,并为您提供所有需要的信息。 *(无双关语意图)

回答

3

该视频中的代码片段不正确。当您遇到API未按预期执行的问题时,我强烈建议您使用going to the API documentation first

在这种情况下,问题是没有构造函数BigTextStyle需要一个字符串。相反,您需要先创建一个BigTextStyle,然后调用其他三种可用方法中的一个(或多个)来设置内容。

像这样的东西可能是你在找什么:

builder.setStyle(new NotificationCompat.BigTextStyle().bigText(easter)); 
+0

是的,这工作,非常感谢你的回答,并与API文档的提示! 在这里,有一朵野花作为礼物。 http://wildflower.resn.co.nz/?CodeTillYouDrop+Tanisx –

+0

哇,从来没有得到过其中之一,谢谢! –