Reputation: 337
I am trying to set the small icon as Network speed, But the App force closes every time. I don't know why it happens. Here is my Notification Method. I am Getting the error when I am trying to set data_icon. Any Help will be Appreciated.
public void showNotification(long receiveData) {
List<String> connStatus = NetworkUtil.getConnectivityInfo(getApplicationContext());
notificationManager = (NotificationManager) getSystemService("notification");
Boolean notification_state = Boolean.valueOf(PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean("notification_state", true));
String wifi_mobile_details = getWifiMobileData();
String s = null;
if (receiveData < PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID) {
s = "b" + (((int) (receiveData / PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID)) * 10);
} else if (receiveData >= PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID && receiveData < 1048576) {
s = "k" + (((int) receiveData) / 1024);
} else if (receiveData >= 1048576 && receiveData < 10485760) {
s = "m" + ((int) (((double) receiveData) / 104857.6d));
} else if (receiveData >= 10485760 && receiveData <= 20971520) {
s = "mm" + (((int) receiveData) / 1048576);
} else if (receiveData > 20971520) {
s = "mmm20";
}
data_icon = getResources().getIdentifier(s, "drawable", getPackageName());
String network_name ;
if (( connStatus.get(0)).equals("wifi_enabled")) {
network_name = ( connStatus.get(1)) + " " + ( connStatus.get(2));
} else if (( connStatus.get(0)).equals("mobile_enabled")) {
network_name = connStatus.get(1);
} else {
network_name = "";
}
DecimalFormat df = new DecimalFormat("#.##");
String speed ;
if (receiveData < PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID) {
speed = "Speed " + ((int) receiveData) + " B/s" + " " + network_name;
} else if (receiveData < 1048576) {
speed = "Speed " + (((int) receiveData) / 1024) + " KB/s" + " " + network_name;
} else {
speed = "Speed " + df.format(((double) receiveData) / 1048576.0d) + " MB/s" + " " + network_name;
}
notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(data_icon)
.setContentTitle(speed)
.setContentText(wifi_mobile_details)
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0))
.setAutoCancel(true);
try{
if (notification_state.booleanValue()) {
notificationManager.notify(this.nid, notificationBuilder.build());
} else {
notificationManager.cancel(this.nid);
}
}catch (Exception e){
e.printStackTrace();
}
}
Here is the Error what i am getting:-
java.lang.IllegalArgumentException: Invalid notification (no valid small icon): Notification(pri=0 contentView=com.techx.intenetspeedmeter/0x10900a1 vibrate=null sound=null defaults=0x0 flags=0x10 color=0x00000000 vis=PRIVATE)
Upvotes: 1
Views: 1963
Reputation: 1057
The porblem is with setSmallIcon(data_icon)
. You are getting data_icon from getResources().getIdentifier()
using s as drawable resource name. s is initially null and then you are assigning value to it in if...else. It may be possible that no conditions are satisfied and your code never enter in the if...else and your variable s is still null after that. And then getResources().getIdentifier()
method will return 0 and then setSmallIcon(data_icon)
will throw an error Invalid notification (no valid small icon)
.
Can you please make sure that your variable s is not null after if...else and if it is not null then data_icon has valid resource_id other then 0.
Upvotes: 2