React Native Android应用Facebook登录/取消时崩溃求助
Root Cause Analysis
Looking closely at your ADB crash logs, the issue isn't related to your Facebook Login code or react-native-fbsdk directly. The crash is triggered by a null pointer exception in the UpiPaymentModule when it tries to handle the activity result from Facebook Login:
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'void com.facebook.react.bridge.Callback.invoke(java.lang.Object[])' on a null object reference at com.upi.payment.UpiPaymentModule.onActivityResult(UpiPaymentModule.java:102)
When any activity (like Facebook Login) finishes and returns a result, all React Native native modules that override onActivityResult receive this result. The UpiPaymentModule is attempting to call a callback that's already null, leading to the app crash.
Solution
Step 1: Fix the UpiPaymentModule Native Code
- Navigate to your Android project's native code directory and find
UpiPaymentModule.java(typically located atandroid/app/src/main/java/com/upi/payment/or a similar path depending on how the module was installed). - Update the
onActivityResultmethod to add a null check for the callback before invoking it:
Note: If the module uses a different variable name for the callback (e.g.,@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { // Add this null check to prevent the crash if (mCallback == null) { super.onActivityResult(requestCode, resultCode, data); return; } // Keep your existing logic here // ... }callbackinstead ofmCallback), adjust the check to match.
Step 2: Clean and Rebuild Your Project
After making the code change, clean and rebuild your Android project to ensure the fix takes effect:
cd android ./gradlew clean react-native run-android
Alternative: Update the UPI Payment Package
If you're using a third-party npm package for UPI payments, check if there's an updated version of the package that addresses this null pointer issue. If an update is available, install it with:
npm install [upi-payment-package-name]@latest --save
Additional Notes
Your Facebook Login code looks correct—since the crash is coming from a different module, fixing the UpiPaymentModule should resolve the issue without needing to modify your React Native login logic.
内容的提问来源于stack exchange,提问作者Chetan

