Kevin Ng
Kevin Ng

Reputation: 41

React native "Attempt to invoke virtual method 'android.app.Activity.showdShowRequestPermissionRationale' on a null object reference"

I'm trying to incorporate Twilio voice using the react-native-twilio-programmable-voice package. My app loads on ios, but when running on android I get this error message

Attempt to invoke virtual method 'boolean android.app.Activity.shouldShowRequestPermissionRationale' on a null object reference screenshot here

I've included <uses-permission android:name="android.permission.RECORD_AUDIO" /> in AndroidManifest.xml

and none of the TwilioVoice related functions are called until 4 or 5 screens into the app.

Been scratching my head for a few days now, any help is greatly appreciated.

Code snippet of my Twilio helper class:

import TwilioVoice from 'react-native-twilio-programmable-voice';
import {Platform} from 'react-native';

import config from '../config/Config';

export default class Voip{

  constructor(props) {
      this.state = {

  };
}

  async setupDeviceWithToken(accessToken){
    console.log('V32: setup device', accessToken);
    TwilioVoice.addEventListener('deviceReady', () => this.deviceReadyHandler());
    TwilioVoice.addEventListener('deviceNotReady', () => this.deviceNotReadyHandler());
    TwilioVoice.addEventListener('connectionDidConnect', () => this.connectionDidConnectHandler());
    TwilioVoice.addEventListener('connectionDidDisconnect', () => this.connectionDidDisconnectHandler());

if(Platform.OS === 'ios')
{
  TwilioVoice.addEventListener('callRejected', this.callRejected());
} else if (Platform.OS === 'android')
{
  TwilioVoice.addEventListener('deviceDidReceiveIncoming', this.deviceDidReceiveIncomingHandler());
}
var success;
try {
      success = await TwilioVoice.initWithToken(accessToken);
      console.log('V36: ', success);
      //return success;
  }
catch(err){
      console.log('V40: ' ,err);
      return err;
  }

  // if(Platform.OS === 'ios')
  // {
      try {
          TwilioVoice.configureCallKit({
              appName:       'VoipApp'                  // Required param
          })
          console.log('V50: ios success');
          //return 'success';
      }
      catch (err) {
          console.log('V54: ',err);
          return err;
      }
  // }
  return success;
}

Upvotes: 4

Views: 10994

Answers (4)

I have the same type of issue but slightly different Attempt to invoke interface method 'expo.modules.interfaces.barcodescanner.BarCodeScannerInterface expo.modules.interfaces.barcodescanner.BarCodeScannerProviderInterface.createBarCodeDetectorWithContext(android.content.Context)' on a null object reference

ℹ️ After downgrading expo-camera from 12.1.0 to 12.0.3, the issue does not seem to show anymore.

Upvotes: 0

John Abraham
John Abraham

Reputation: 21

I solve this problem by starting a new react native project because it save me all the stress of looking for unknown solutions

Upvotes: -2

Aftab Baig
Aftab Baig

Reputation: 291

I had the same issue and after examining the code in detail, I figured out that the permission request is been made in TwilioVoiceModule's constructor and at that time, ActivityCompat might not have been initialized probably (BTW, I am an iOS developer and haven't worked much on Android). So I created a @ReactMethod that asks for permission and called that method in ReactNative after I get to the call screen.

Something like this:

    @ReactMethod
    public void askForMicrophones() {
      if (!checkPermissionForMicrophone()) {
        requestPermissionForMicrophone();
      }
    }

Upvotes: 0

jheyer159
jheyer159

Reputation: 19

I've been using the same library to handle the integration with React Native. I feel your pain with attempting this integration because there are a lot of hiccups along the way. I ran into this problem when letting the Android side handle permissions.

I discovered that permissions can instead be handled through React Native on Android. I commented out the permission calls on Android and switched over to the React Native implementation, and that worked for me.

There have been many updates since this question was posted so the user must take care in what versions are being used between Twilio Voice for iOS (Pods), Android, and the React Native wrapper. I ended up forking the library to control what is merged because every aspect is under active development.

async function requestMicrophonePermission() {
  try {
    const granted = await PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.RECORD_AUDIO,
      {
        'title': `${name} Microphone Permission`,
        'message': `${name} needs access to your microphone 
                   'so you can talk.`
      }
    )
    if (granted === PermissionsAndroid.RESULTS.GRANTED) {
      console.log("Microphone permission granted")
    } else {
      console.log("Microphone permission denied")
    }
  } catch (err) {
    console.warn(err)
  }
}

Best of luck!

Upvotes: 0

Related Questions