In the app im getting WIFI List<ScanResults>
data and trying to update the Chart but something is not going well , and the app throw exception java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
, i have tried to many code combination from MpAndroidChart but nothing solve the issue ... Why it is happening ?
Broadcast Receiver for WiFi ScanResults :
public class Receiver extends BroadcastReceiver {
@SuppressWarnings("ConstantConditions")
@Override
public void onReceive(Context context, Intent intent) {
new Thread(new Runnable() {
@Override
public void run() {
try {
List<ScanResult> results = mWifiManager.getScanResults();
ArrayList<String> lista_bssid_e_skanuar = new ArrayList<>();
ArrayList<String> lista_bssid_lista_skanuar = new ArrayList<>();
//boolean channel_width_supported = false;
for (ScanResult result1 : results) {
final String SSID = result1.SSID;
final String channel = String.valueOf(ieee80211_frequency_to_channel(result1.frequency));
final String frequency = String.valueOf(result1.frequency);
final String BSSID = result1.BSSID;
final String capabilities = result1.capabilities;
final String signal = String.valueOf(result1.level);
String security = "FREE";
int channel_width;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//channel_width_supported = true;
int freq1 = result1.centerFreq0;
int freq2 = result1.centerFreq1;
int channel_width_freq;
if (freq1 - freq2 < 0) {
channel_width_freq = freq2 - freq1;
} else {
channel_width_freq = freq1 - freq2;
}
if ((Integer.parseInt(frequency) - channel_width_freq) <= 0) {
channel_width = channel_width_freq - Integer.parseInt(frequency);
} else {
channel_width = Integer.parseInt(frequency) - channel_width_freq;
}
if (channel_width != 20 && channel_width != 40 && channel_width == 60 && channel_width != 80 && channel_width != 160) {
channel_width = 21;
}
} else {
//channel_width_supported = false;
channel_width = 21;
}
final int finalChannel_width = channel_width;
if (capabilities.contains("WPA")) {
security = "WPA";
}
if (capabilities.contains("WPA2")) {
security = "WPA2";
}
if (capabilities.contains("WEP")) {
security = "WEP";
}
final String finalSecurity = security;
if (!BSSID.equals("00:00:00:00:00:00")) {
lista_bssid_e_skanuar.add(SSID + "/" + channel + "/" + frequency + "/" + BSSID + "/" + capabilities + "/" + signal + "/" + finalSecurity + "/" + finalChannel_width);
lista_bssid_lista_skanuar.add(BSSID);
}
}
synchronized (_lock) {
for (int a = 0; a < lista_bssid_e_skanuar.size(); a++) {
String[] TE_DHENAT = lista_bssid_e_skanuar.get(a).split("/");
final String SSID_LISTA = TE_DHENAT[0];
final String CHANNEL_LISTA = TE_DHENAT[1];
final String BSSID_LISTA = TE_DHENAT[3];
final String SIGNAL_LISTA = TE_DHENAT[5];
final int kanali = Integer.parseInt(CHANNEL_LISTA);
int signal = Integer.parseInt(SIGNAL_LISTA) + 100;
if (signal > 70) {
signal = 70;
}
if (!lista_bssid_lista.contains(BSSID_LISTA)) {
lista_bssid_lista.add(BSSID_LISTA);
addEntry(kanali - 2, 0, a, SSID_LISTA);
addEntry(kanali, signal, a, SSID_LISTA);
addEntry(kanali + 2, 0, a, SSID_LISTA);
} else {
for (int i = 0; i < lista_bssid_lista.size(); i++) {
if (!lista_bssid_lista_skanuar.contains(lista_bssid_lista.get(i))) {
//MAYBE EXCEPTION HAPPENING HERE ??
final int finalSignal1 = signal;
final int finalI = i;
removeEntry(kanali, finalSignal1, finalI);
addEntry(kanali - 2, 0, finalI, SSID_LISTA);
addEntry(kanali, 0, finalI, SSID_LISTA);
addEntry(kanali + 2, 0, finalI, SSID_LISTA);
} else {
// OR HERE ????
final int finalSignal = signal;
final int finalA = a;
removeEntry(kanali, finalSignal, finalA);
addEntry(kanali - 2, 0, finalA, SSID_LISTA);
addEntry(kanali, finalSignal, finalA, SSID_LISTA);
addEntry(kanali + 2, 0, finalA, SSID_LISTA);
}
}
}
}
}
}catch (Exception i){
Thread.currentThread().interrupt();
}
}
}).start();
}
}
Add Entry Method :
private synchronized void addEntry(float kanali, float sinjali, int indexi, String emri) {
LineData data = mwifichart.getData();
ILineDataSet set = data.getDataSetByIndex(indexi);
if (set == null) {
set = createSet(emri);
data.addDataSet(set);
}
data.addEntry(new Entry(kanali, sinjali), indexi);
data.notifyDataChanged();
mwifichart.notifyDataSetChanged();
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
mwifichart.invalidate();
}
});
}
Remove Entry Method :
private synchronized void removeEntry(float x_value,float y_value, int indexi) {
try {
LineData data = mwifichart.getData();
if (data != null) {
ILineDataSet set = data.getDataSetByIndex(indexi);
if (set != null) {
data.removeEntry(x_value - 2, indexi);
data.removeEntry(x_value + 0, indexi);
data.removeEntry(x_value + 2, indexi);
data.notifyDataChanged();
mwifichart.notifyDataSetChanged();
}
}
}catch (Exception I){ TastyToast.makeText(getContext(),I.getMessage(),TastyToast.LENGTH_SHORT,TastyToast.ERROR);
}
}
I'm getting this Exception and the app crash instantly , tried to many code combination but nothing ...
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:260)
at java.util.ArrayList.get(ArrayList.java:313)
at com.github.mikephil.charting.data.DataSet.getEntryForIndex(DataSet.java:286)
at com.github.mikephil.charting.utils.Transformer.generateTransformedValuesLine(Transformer.java:184)
at com.github.mikephil.charting.renderer.LineChartRenderer.drawValues(LineChartRenderer.java:547)
at com.github.mikephil.charting.charts.BarLineChartBase.onDraw(BarLineChartBase.java:263)
UPDATE :
It seems like the exception is not happening if the code runs on UI THREAD , but not sure about this , just it don't throws exception anymore ???
Example
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
removeEntry(kanali, finalSignal, finalA);
addEntry(kanali - 2, 0, finalA, SSID_LISTA);
addEntry(kanali, finalSignal, finalA, SSID_LISTA);
addEntry(kanali + 2, 0, finalA, SSID_LISTA);
}
});
The issue is followed on the Github project here: https://github.com/PhilJay/MPAndroidChart/issues/2450
It seems to be happening only for line charts.
The possible solution seems to be to clear the chart if there are no values to display:
if (values.isEmpty()) {
chart.clear();
} else {
// set data
chart.setData(data);
}
I have the same problem and will deploy this fix soon, I will update if this fix is not working.
You should custom a simple ChartRenderer avoid drawing while chart is empty. Then,you add it to your chart.
chart.setRenderer(new CustomLineChartRenderer(chart,chart.getAnimator(),chart.getViewPortHandler()));
public class CustomLineChartRenderer extends LineChartRenderer {
public CustomLineChartRenderer(LineDataProvider chart, ChartAnimator animator, ViewPortHandler viewPortHandler) {
super(chart, animator, viewPortHandler);
}
@Override
protected boolean shouldDrawValues(IDataSet set) {
return super.shouldDrawValues(set) && set.getEntryCount() > 0;
}
}
I had the same problem but I just clear the chart before to put any data.
PieChart pieChart = view.findViewById(R.id.PieChart);
pieChart.clear();
In your add entry method mwifichart.getdata() is returning null. And you are trying to get value from that list. Please put a check on that statement. If(data !=null && data.size >=indexi) ILineDataSet data =data.getSetDataByIndex(indexi)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.