twilio / voice-quickstart-android

Quickstart app for the Voice Android SDK
https://www.twilio.com/docs/api/voice-sdk/android/getting-started
MIT License
184 stars 140 forks source link

Error in XIAOMI device and works in SAMSUNG device Caused by: java.lang.NullPointerException #541

Closed Waseemnajibsufyan closed 10 months ago

Waseemnajibsufyan commented 1 year ago

Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at com.twilio.voice.quickstart.VoiceActivity.onCreate(VoiceActivity.java:133)

can help me how to correct the error in this code:

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_voice); bacreaccesstoken = findViewById(R.id.reaccesstoken); saveeaccesstoken = findViewById(R.id.reaccesstoken2); send_text = findViewById(R.id.editTextTextPersonName3); receiver_msg = findViewById(R.id.textView2); try { receiver_msg.setText("Hello world"); } catch(Exception e){ Toast.makeText(this,e.getMessage(), Toast.LENGTH_SHORT).show(); }

    bacreaccesstoken.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String str = send_text.getText().toString();
            receiver_msg.setText(str);
            accessToken=str;
            send_text.setVisibility(View.GONE);
            bacreaccesstoken.setVisibility(View.GONE);

}});

saveeaccesstoken.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {

            bacreaccesstoken.setVisibility(View.VISIBLE);
            send_text.setVisibility(View.VISIBLE);
            send_text.setText("");
        }});

    // These flags ensure that the activity can be launched when the screen is locked.
    Window window = getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
            | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
            | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    coordinatorLayout = findViewById(R.id.coordinator_layout);
    callActionFab = findViewById(R.id.call_action_fab);
    hangupActionFab = findViewById(R.id.hangup_action_fab);
    holdActionFab = findViewById(R.id.hold_action_fab);
    muteActionFab = findViewById(R.id.mute_action_fab);
    chronometer = findViewById(R.id.chronometer);

    callActionFab.setOnClickListener(callActionFabClickListener());
    hangupActionFab.setOnClickListener(hangupActionFabClickListener());
    holdActionFab.setOnClickListener(holdActionFabClickListener());
    muteActionFab.setOnClickListener(muteActionFabClickListener());

    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    /*
     * Setup the broadcast receiver to be notified of FCM Token updates
     * or incoming call invite in this Activity.
     */
    voiceBroadcastReceiver = new VoiceBroadcastReceiver();
    registerReceiver();

    /*
     * Setup the UI
     */
    resetUI();

    /*
     * Displays a call dialog if the intent contains a call invite
     */
    handleIncomingCallIntent(getIntent());

    /*
     * Ensure required permissions are enabled
     */
    if (Build.VERSION.SDK_INT > VERSION_CODES.R) {
        if (!hasPermissions(this, Manifest.permission.RECORD_AUDIO,
                Manifest.permission.BLUETOOTH_CONNECT)) {
            requestPermissionForMicrophoneAndBluetooth();
        } else {
            registerForCallInvites();
        }
    } else {
        if (!hasPermissions(this, Manifest.permission.RECORD_AUDIO)) {
            requestPermissionForMicrophone();
        } else {
            registerForCallInvites();
        }
    }

    /*
     * Setup audio device management and set the volume control stream
     */
    audioSwitch = new AudioSwitch(getApplicationContext());
    savedVolumeControlStream = getVolumeControlStream();
    setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);
}
@Override
protected void onResume() {

    super.onResume();
    registerReceiver();
    startAudioSwitch();
    // Fetching the stored data
    // from the SharedPreference
    SharedPreferences sh = getSharedPreferences("MySharedPref", MODE_MULTI_PROCESS);

    accessToken = sh.getString("message_key", "");
    if (accessToken==""){
        accessToken="ادخل الكود";
        receiver_msg.setText(accessToken);
        // Setting the fetched data
        // in the EditTexts
        send_text.setText(accessToken);
        send_text.setVisibility(View.GONE);
        bacreaccesstoken.setVisibility(View.GONE);
    }else {

        receiver_msg.setText(accessToken);
        // Setting the fetched data
        // in the EditTexts
        send_text.setText(accessToken);
        send_text.setVisibility(View.GONE);
        bacreaccesstoken.setVisibility(View.GONE);
    }

}
@Override
protected void onPause() {
    super.onPause();
    unregisterReceiver();
    // Creating a shared pref object
    // with a file name "MySharedPref"
    // in private mode
    SharedPreferences sharedPreferences = getSharedPreferences("MySharedPref", MODE_MULTI_PROCESS);
    SharedPreferences.Editor myEdit = sharedPreferences.edit();

    // write all the data entered by the user in SharedPreference and apply
    myEdit.putString("message_key", send_text.getText().toString());
    myEdit.apply();
}
@Override
Waseemnajibsufyan commented 1 year ago

I need to fetch access token from the Url in my code I have got the access token as a Url , but i am not able to fetch it into my code after expire . Could anybody help me giving the code

cybex-dev commented 1 year ago

Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at com.twilio.voice.quickstart.VoiceActivity.onCreate(VoiceActivity.java:133)

Make sure you assign bacreaccesstoken before using the object itself. i.e. you cannot call a function setOnClickListener on a null object bacreaccesstoken.

Note: This is not a Twilio voice error, I'd advise going through some Android tutorials.

afalls-twilio commented 10 months ago

@cybex-dev Thank you helping the community out! and yes, this is not an SDK issue, closing.