Skip to content

Commit

Permalink
Fixing possible getActivity() NullPointerException in Android example…
Browse files Browse the repository at this point in the history
… app
  • Loading branch information
pgregorr-amazon committed Oct 10, 2024
1 parent 83b031e commit 9c196b2
Showing 1 changed file with 31 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
});
}
}
};

Expand Down

0 comments on commit 9c196b2

Please sign in to comment.