diff --git a/examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/ConnectionExampleFragment.java b/examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/ConnectionExampleFragment.java index fc3ba6cd1da90a..f711096a22f79f 100644 --- a/examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/ConnectionExampleFragment.java +++ b/examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/ConnectionExampleFragment.java @@ -29,6 +29,7 @@ import android.widget.TextView; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentActivity; import com.R; import com.matter.casting.core.CastingPlayer; import com.matter.casting.support.CommissionerDeclaration; @@ -208,36 +209,41 @@ public void handle(CommissionerDeclaration cd) { Log.i(TAG, "CastingPlayer CommissionerDeclaration message received: "); cd.logDetail(); - getActivity() - .runOnUiThread( - () -> { - connectionFragmentStatusTextView.setText( - "CommissionerDeclaration message received from Casting Player: \n\n"); - if (cd.getCommissionerPasscode()) { + FragmentActivity activity = getActivity(); + // Prevent possible NullPointerException. This callback could be called when + // this Fragment is not attached to its host activity or when the fragment's + // lifecycle is not in a valid state for interacting with the activity. + if (activity != null && !activity.isFinishing()) { + activity.runOnUiThread( + () -> { + connectionFragmentStatusTextView.setText( + "CommissionerDeclaration message received from Casting Player: \n\n"); + if (cd.getCommissionerPasscode()) { - displayPasscodeInputDialog(getActivity()); + displayPasscodeInputDialog(activity); + connectionFragmentStatusTextView.setText( + "CommissionerDeclaration message received from Casting Player: A passcode is now displayed for the user by the Casting Player. \n\n"); + } + if (cd.getCancelPasscode()) { + if (useCommissionerGeneratedPasscode) { connectionFragmentStatusTextView.setText( - "CommissionerDeclaration message received from Casting Player: A passcode is now displayed for the user by the Casting Player. \n\n"); - } - if (cd.getCancelPasscode()) { - if (useCommissionerGeneratedPasscode) { - connectionFragmentStatusTextView.setText( - "CastingPlayer/Commissioner-Generated passcode connection attempt cancelled by the CastingPlayer/Commissioner user. \n\nRoute back to exit. \n\n"); - } else { - connectionFragmentStatusTextView.setText( - "Connection attempt cancelled by the CastingPlayer/Commissioner user. \n\nRoute back to exit. \n\n"); - } - if (passcodeDialog != null && passcodeDialog.isShowing()) { - passcodeDialog.dismiss(); - } + "CastingPlayer/Commissioner-Generated passcode connection attempt cancelled by the CastingPlayer/Commissioner user. \n\nRoute back to exit. \n\n"); + } else { + connectionFragmentStatusTextView.setText( + "Connection attempt cancelled by the CastingPlayer/Commissioner user. \n\nRoute back to exit. \n\n"); } - if (cd.getErrorCode() != CommissionerDeclaration.CdError.noError) { - commissionerDeclarationErrorTextView.setText( - "CommissionerDeclaration error from CastingPlayer: " - + cd.getErrorCode().getDescription()); + if (passcodeDialog != null && passcodeDialog.isShowing()) { + passcodeDialog.dismiss(); } - }); + } + if (cd.getErrorCode() != CommissionerDeclaration.CdError.noError) { + commissionerDeclarationErrorTextView.setText( + "CommissionerDeclaration error from CastingPlayer: " + + cd.getErrorCode().getDescription()); + } + }); + } } };