crash ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1)

Multi tool use
Multi tool use


crash ActivityCompat.requestPermissions(this,new String{Manifest.permission.ACCESS_FINE_LOCATION}, 1)



When I include this line I get error(crash)
ActivityCompat.requestPermissions(this,new String{Manifest.permission.ACCESS_FINE_LOCATION}, 1);



logcat


07-02 00:41:56.478 24745-24745/com.example.root.securityalert E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.root.securityalert, PID: 24745
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.root.securityalert/com.example.root.securityalert.DeviceScanActivity}: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2678)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2743)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1490)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6165)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
at com.example.root.securityalert.DeviceScanActivity.onRequestPermissionsResult(DeviceScanActivity.java:213)
at android.app.Activity.requestPermissions(Activity.java:4120)
at com.example.root.securityalert.DeviceScanActivity.onCreate(DeviceScanActivity.java:142)
at android.app.Activity.performCreate(Activity.java:6687)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2631)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2743) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1490) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6165) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778) 



code. It is making blue tooth adapter and manager.Then request permission. Then starts scan on onRequestPermissionsResult. Ihave attached requestPermissions for both ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION


public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);


mHandler = new Handler();

//mSend=new BluetoothSendRecv(cntxt);
mActvty= this.getParent();
visible = this.getIntent();

// Use this check to determine whether BLE is supported on the device. Then you can
// selectively disable BLE-related features.
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
finish();
}
mBluetoothManager =(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = mBluetoothManager.getAdapter();
// Checks if Bluetooth is supported on the device.
if (mBluetoothAdapter == null ) {
Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show();
finish();
return;
} // Initializes a Bluetooth adapter. For API level 18 and above, get a reference to
// BluetoothAdapter through BluetoothManager.

if( !mBluetoothAdapter.isEnabled())
{
Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetooth, 1);
}
if( !mBluetoothAdapter.isDiscovering()) {
Intent discoverableIntent =
new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);
}
// Construct the data source

ArrayList<ViewHolder> arrayOfUsers = new ArrayList<ViewHolder>();

// Create the adapter to convert the array to views

adapter = new UsersAdapter(this, arrayOfUsers);
cntxt=this.getApplicationContext();
ListView listView = (ListView) findViewById(R.id.mobile_list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
ViewHolder entry= (ViewHolder) parent.getAdapter().getItem(position);
mAddress = entry.deviceAddress;
Toast.makeText(cntxt, mAddress, Toast.LENGTH_SHORT).show();
Intent i = new Intent(cntxt, BluetoothLeService.class);
cntxt.startService(i);
bindService(i, mConnection, BIND_AUTO_CREATE);; //if checked, start service
//final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
//mBluetoothService.mBluetoothDeviceAddress=address;
//mBluetoothService.mBluetoothManager=mBluetoothManager;
//mBluetoothService.mBluetoothAdapter = mBluetoothAdapter;
//mBluetoothService.mBluetoothGatt.connect();
/*mBluetoothService.mBluetoothGatt = */
//mSend.mBluetoothGatt=device.connectGatt(mActvty/*cntxt*/, false, mSend.mGattCallback);
//mSend.mBluetoothDeviceAddress=address;
//mSend.mBluetoothManager=mBluetoothManager;
//mSend.mBluetoothAdapter = mBluetoothAdapter;
//mSend.mBluetoothGatt.connect();
//mBluetoothService.mBluetoothGatt=mBluetoothGatt;
//Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show();
}});

/*if (ContextCompat.checkSelfPermission(this,
android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {*/
ActivityCompat.requestPermissions(this, new String{Manifest.permission.ACCESS_FINE_LOCATION}, 123);
//}

requestPermissions(new String{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);
ViewHolder newUser2 = new ViewHolder("adtv2","vvg2");
adapter.add(newUser2);

}
ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
//Toast.makeText(Client.this, "Service is disconnected", 1000).show();
mBounded = false;
mBluetoothService = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//Toast.makeText(Client.this, "Service is connected", 1000).show();
mBounded = true;
BluetoothLeService.LocalBinder mLocalBinder = (BluetoothLeService.LocalBinder)service;
Toast.makeText(cntxt, "Mithun", Toast.LENGTH_SHORT).show();
mBluetoothService = mLocalBinder.getService();
if (!mBluetoothService.initialize()) {
//Log.e(TAG, "Unable to initialize Bluetooth");
finish();
}
// Automatically connects to the device upon successful start-up
// initialization.
mBluetoothService.connect(mAddress);
}
};
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions, @NonNull int grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_COARSE_LOCATION: {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission granted, yay! Start the Bluetooth device scan.
scanLeDevice(true);
} else {
// Alert the user that this application requires the location permission to perform the scan.
}
}
}
}





Did you add this line to your AndroidManifest.xml? <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> And maybe you can add more code to understand where and how you are using the request permission
– Fumarja
Jul 1 at 20:03



<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>





Your crash is on DeviceScanActivity.onRequestPermissionsResult, can you include the code that you have in that method?
– ianhanniballake
Jul 1 at 20:59


DeviceScanActivity.onRequestPermissionsResult




1 Answer
1



You access grantResults[0] in onRequestPermissionsResult without checking that grandResults.length != 0 It appears that in your error case grantResults is empty (thus the ArrayIndexOutOfBoundsException: length=0; index=0 error). Here's an example of how you could check for this case.


grantResults[0]


onRequestPermissionsResult


grandResults.length != 0


grantResults


ArrayIndexOutOfBoundsException: length=0; index=0


if (grantResults.length > 0 && // alternately check that length == 1
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission granted, yay! Start the Bluetooth device scan.
scanLeDevice(true);
} else {
// Alert the user that this application requires the location permission to perform the scan.
}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

YfQmByv1PgunZqgKLnmzD7KabbWwVV9mqnrs 3hbq1yGv etscfz CJ dHzOAvDtkyB fzZRQm,u w7E B yIcZjZ 8 OQnEeD zez18uF8
dzkuZySGA2l t,1DnBF s6p5ef,5zPogu6giMuiiaZ,huijnkpTnOCIXuXR5X zS2,CRLfhFPtGQzjkxY6 3QBdGkwES2 I,v2rhui1T1nU mE

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications