There is an Android FATAL EXCEPTION error, so please check it.

Asked 2 years ago, Updated 2 years ago, 68 views

package com.example.unbo.wifilist;

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.gun0912.tedpermission.PermissionListener;
import com.gun0912.tedpermission.TedPermission;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "WIFIList";

    private WifiManager wifiManager;
    private List<ScanResult> scanDatas; // ScanResult List
    private ListView listView;

    private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            if (action.equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
                scanDatas = wifiManager.getScanResults();

                //SSID comparison
                for (int i = 0; i <= scanDatas.size(); i++) {
                    String str1 = scanDatas.get(i).SSID;

                    for (int j = 0; j <= scanDatas.size(); j++) {
                        String str2 = scanDatas.get(j).SSID;
                        Log.d("Darin", "  " + str1.equals(str2));
                        //Log.d("Darin", "  " + scanDatas.get(j).level);
                        Log.d("Darin", "  " + str2);
                    }
                    String str3 = scanDatas.get(i).SSID;
                    Log.d("Darin", " SSID = " + str3);

                }

                // Adapter View (List View)
                listView = (ListView) findViewById(R.id.listView);
                // Adapters
                ArrayAdapter adapter = new WifiAdapter(getApplicationContext(), R.layout.item_layout, scanDatas);
                listView.setAdapter(adapter);

                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        Toast.makeText(getApplicationContext(), scanDatas.get(i).SSID, Toast.LENGTH_SHORT).show();
                    }
                });
                // Update listview
                adapter.notifyDataSetChanged();

            } } else if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
                sendBroadcast(new Intent("wifi.ON_NETWORK_STATE_CHANGED"));
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        showPermissionDialog();

        wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        if (!wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(true);
        }
        listView = (ListView) findViewById(R.id.listView);
    }

    private void showPermissionDialog() {
        PermissionListener permissionlistener = new PermissionListener() {
            @Override
            public void onPermissionGranted() {
                Toast.makeText (MainActivity.this, "Authorization", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onPermissionDenied(ArrayList<String> deniedPermissions) {
                Toast.makeText (MainActivity.this, "Denied Permissions.toString(), Toast.LENGTH_SHORT).show();
                finish();
            }
        };

        TedPermission.with(this)
                .setPermissionListener(permissionlistener)
                .setRationaleMessage ("Requires location service privileges")
                .setPermissions(Manifest.permission.READ_CONTACTS)
                .setPermissions(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION)
                .check();
    }

    @Override
    public void onResume() {
        super.onResume();
        IntentFilter intentFilter = new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
        intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        registerReceiver(receiver, intentFilter);
        wifiManager.startScan();

        listView.setFocusable(true);
    }

    @Override
    public void onPause() {
        super.onPause();
        unregisterReceiver(receiver);
    }
}

The error part is String str2 = scanDatas.Get(j).SSID; part.

From here on out, it's an error log. wifilist E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.eunbo.wifilist, PID: 23663 java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.wifi.SCAN_RESULTS flg=0x4000010 (has extras) } in com.example.eunbo.wifilist.MainActivity$1@8950aa7 at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:891) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.IndexOutOfBoundsException: Invalid index 15, size is 15 at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) at java.util.ArrayList.get(ArrayList.java:308) at com.example.eunbo.wifilist.MainActivity$1.onReceive(MainActivity.java:45) at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:881) at android.os.Handler.handleCallback(Handler.java:739)  at android.os.Handler.dispatchMessage(Handler.java:95)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:5417)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

android fatal error

2022-09-21 20:16

1 Answers

for (int i = 0; i <= scanDatas.in size(); i++)

<= Rohani

Caused by: java.lang.IndexOutOfBoundsException: Invalid index 15, size is 15 at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) at java.util.ArrayList.get(ArrayList.java:308)

The error comes out referring to the last index of scanDatas


2022-09-21 20:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.