在 Android 上出现分支 io 错误 – 在空对象引用上调用 getCurrentActivity()。

huangapple go评论130阅读模式
英文:

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

问题

我一直在收到这个错误:

java.lang.NullPointerException: 尝试调用虚拟方法
'android.app.Activity io.branch.referral.Branch.getCurrentActivity()'
但空对象引用

以下是源代码:

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) {

                        // 选项3:跳转到页面
                        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);
            // 如果活动在前台(或在返回堆栈中但部分可见),启动相同的活动将跳过onStart,使用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) {
            // 处理深链接数据(导航到页面,显示内容等)
        }
    };
}

Main2Activity.java

public class Main2Activity extends AppCompatActivity {

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

    @Override public void onStart() {
        super.onStart();
        try {
            // 用于调试的 Branch 日志记录
            Branch.enableLogging();

            // Branch 对象初始化
            Branch.getAutoInstance(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.parkwaydrive.packagename">

    <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 初始化 -->
        <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" />
        <!-- 设置为 true 以使用 Branch_Test_Key(在模拟安装和/或在调试和生产版本之间切换时很有用) -->

        <activity android:name=".MainActivity" />
        <activity android:name=".Main2Activity" >
            <!-- Branch App Links(可选) -->
            <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" />
                <!-- 示例备用域名在 Journeys/Web SDK 和 Deepviews 在网站内部使用时需要使用。 -->
                <data android:scheme="https" android:host="2rerz-alternate.app.link" />
            </intent-filter>
        </activity>
    </application>
</manifest>
英文:

I keep getting this error:

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

this is the source code:

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(&quot;BRANCH SDK&quot;, 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 extends 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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.parkwaydrive.packagename">

&lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; /&gt;
&lt;uses-permission android:name=&quot;android.permission.ACCESS_NETWORK_STATE&quot; /&gt;
&lt;application
android:allowBackup=&quot;true&quot;
android:icon=&quot;@mipmap/ic_launcher&quot;
android:label=&quot;@string/app_name&quot;
android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
android:supportsRtl=&quot;true&quot;
android:theme=&quot;@style/AppTheme&quot;
android:usesCleartextTraffic=&quot;true&quot;&gt;
&lt;meta-data
android:name=&quot;com.facebook.sdk.ApplicationId&quot;
android:value=&quot;@string/facebook_app_id&quot; /&gt;
&lt;activity
android:name=&quot;.MainApp&quot;&gt;
&lt;intent-filter&gt;
&lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
&lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
&lt;/intent-filter&gt;
&lt;!-- Branch URI Scheme --&gt;
&lt;intent-filter&gt;
&lt;data android:scheme=&quot;hello&quot; /&gt;
&lt;action android:name=&quot;android.intent.action.VIEW&quot; /&gt;
&lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;
&lt;category android:name=&quot;android.intent.category.BROWSABLE&quot; /&gt;
&lt;/intent-filter&gt;
&lt;/activity&gt;
&lt;!-- Branch init --&gt;
&lt;meta-data android:name=&quot;io.branch.sdk.BranchKey&quot; android:value=&quot;key_live_jeVWU2AYfrIjpJmslgNxZgjeAwcUzcqK&quot; /&gt;
&lt;meta-data android:name=&quot;io.branch.sdk.BranchKey.test&quot; android:value=&quot;key_test_hlxrWC5Zx16DkYmWu4AHiimdqugRYMr&quot; /&gt;
&lt;meta-data android:name=&quot;io.branch.sdk.TestMode&quot; android:value=&quot;false&quot; /&gt;     &lt;!-- Set to true to use Branch_Test_Key (useful when simulating installs and/or switching between debug and production flavors) --&gt;
&lt;activity android:name=&quot;.MainActivity&quot; /&gt;
&lt;activity android:name=&quot;.Main2Activity&quot; &gt;
&lt;!-- Branch App Links (optional) --&gt;
&lt;intent-filter android:autoVerify=&quot;true&quot;&gt;
&lt;action android:name=&quot;android.intent.action.VIEW&quot; /&gt;
&lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;
&lt;category android:name=&quot;android.intent.category.BROWSABLE&quot; /&gt;
&lt;data android:scheme=&quot;https&quot; android:host=&quot;2rerz.app.link&quot; /&gt;
&lt;!-- example-alternate domain is required for App Links when the Journeys/Web SDK and Deepviews are used inside your website.  --&gt;
&lt;data android:scheme=&quot;https&quot; android:host=&quot;2rerz-alternate.app.link&quot; /&gt;
&lt;/intent-filter&gt;
&lt;/activity&gt;
&lt;/application&gt;

</manifest>

答案1

得分: 0

这是因为您尚未实现 CustomApplication 类。请实现该类并将 Application 类添加到您的 Manifest.xml 文件中。

英文:

This is occurring since you have not implemented a CustomApplication class.
Please implement the same and add the Application class to your Manifest.xml file.

huangapple
  • 本文由 发表于 2020年10月13日 22:03:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/64336754.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定