android 上的分支 io 出错 - 空对象引用上的 getCurrentActivity()

Error with branch io on android - getCurrentActivity() on a null object reference

我不断收到此错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.Activity io.branch.referral.Branch.getCurrentActivity()' on a null object reference

这是源代码:

MainApp.java

public class MainApp extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

}

@Override public void onStart() {
    super.onStart();
    try {
        Branch.sessionBuilder(this).withCallback(new Branch.BranchReferralInitListener() {
            @Override
            public void onInitFinished(JSONObject referringParams, BranchError error) {
                if (error == null) {

                    // option 3: navigate to page
                    Intent intent = new Intent(MainApp.this, Main2Activity.class);
                    startActivity(intent);
                    

                } else {
                    Log.i("BRANCH SDK", error.getMessage());
                }
            }
        }).withData(this.getIntent().getData()).init();

    }
    catch (Exception e) {
        e.printStackTrace();
    }

}
@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    try {
        setIntent(intent);
        // if activity is in foreground (or in backstack but partially visible) launching the same
        // activity will skip onStart, handle this case with reInitSession
        Branch.sessionBuilder(this).withCallback(branchReferralInitListener).reInit();
    }
    catch (Exception ignored) { }

}

private Branch.BranchReferralInitListener branchReferralInitListener = new Branch.BranchReferralInitListener() {
    @Override
    public void onInitFinished(JSONObject linkProperties, BranchError error) {
        // do stuff with deep link data (nav to page, display content, etc)
    }
};

}

Main2Activity.java

public class Main2Activity 扩展 AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

}

@Override public void onStart() {
    super.onStart();
    try{
        // Branch logging for debugging
        Branch.enableLogging();

        // Branch object initialization
        Branch.getAutoInstance(this);
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    android:usesCleartextTraffic="true">

    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/facebook_app_id" />

    <activity
        android:name=".MainApp">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <!-- Branch URI Scheme -->
        <intent-filter>
            <data android:scheme="hello" />
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
        </intent-filter>


    </activity>

    <!-- Branch init -->
    <meta-data android:name="io.branch.sdk.BranchKey" android:value="key_live_jeVWU2AYfrIjpJmslgNxZgjeAwcUzcqK" />
    <meta-data android:name="io.branch.sdk.BranchKey.test" android:value="key_test_hlxrWC5Zx16DkYmWu4AHiimdqugRYMr" />
    <meta-data android:name="io.branch.sdk.TestMode" android:value="false" />     <!-- Set to true to use Branch_Test_Key (useful when simulating installs and/or switching between debug and production flavors) -->


    <activity android:name=".MainActivity" />
    <activity android:name=".Main2Activity" >
    <!-- Branch App Links (optional) -->
    <intent-filter android:autoVerify="true">
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="https" android:host="2rerz.app.link" />
        <!-- example-alternate domain is required for App Links when the Journeys/Web SDK and Deepviews are used inside your website.  -->
        <data android:scheme="https" android:host="2rerz-alternate.app.link" />
    </intent-filter>
    </activity>

</application>

出现这种情况是因为您尚未实施 CustomApplication class。 请执行相同的操作并将应用程序 class 添加到您的 Manifest.xml 文件。