英文:
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("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 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
<?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 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>
</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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论