2017-09-27 108 views
2

我試圖設置小圖標作爲網絡速度,但App強制每次關閉。我不知道爲什麼會發生。這是我的通知方法。我在設置data_icon時遇到錯誤。任何幫助將不勝感激。Android通知生成器setSmallIcon()

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(); 
    } 
} 

下面是我收到的錯誤: -

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) 
+0

什麼是你的目標API? – chornge

+0

嗨,閔API是16 –

回答

1

的porblem是setSmallIcon(data_icon)。您使用s作爲可繪製資源名稱獲取getResources().getIdentifier()中的data_icon。 s最初是空的,然後你在if ... else中爲它賦值。有可能沒有條件得到滿足,並且你的代碼永遠不會進入if ... else,並且你的變量s在那之後仍然是空的。然後getResources().getIdentifier()方法將返回0,然後setSmallIcon(data_icon)將引發錯誤Invalid notification (no valid small icon)

能否請確保您的變量s不爲空,如果...否則,如果以後不爲空,然後其他data_icon具有有效RESOURCE_ID然後按0

+0

是的我也認爲,這是問題。你錯過了最後一個''else'',並且有一個默認 - 如果沒有命中的值賦給''s''。或者爲's''分配一個有效的默認值而不是''String s = null;''use''String s =「name_of_your_default_icon」;'' – Grisgram

+0

我正在獲取s 0b的值。它不是null。 –

+0

但是你有任何可繪製的名稱0b ...和data_icon的值是什麼? –