将 branch.io sdk 与 android app 集成,得到 classcastexception

integerating branch.io sdk with android app , getting classcasteException

我遵循了 Branch.io

上的所有说明

https://start.branch.io/#/ << 已执行此 link 中的所有步骤。 (我应该使用哪个键.. test_branch_key 或 live_branch_key ??两者有什么区别)

这是我的MainActivity.java文件

package absdevelopers.com.finalbranchtest;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import org.json.JSONObject;

import io.branch.referral.Branch;
import io.branch.referral.BranchError;

public class MainActivity extends AppCompatActivity {

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

    Branch branch = Branch.getInstance();
    branch.initSession(new Branch.BranchReferralInitListener() {
        @Override
        public void onInitFinished(JSONObject referringParams, BranchError error) {
            if (error == null) {
                // params are the deep linked params associated with the link that the user clicked before showing up
                Log.i("BranchConfigTest", "deep link data: " + referringParams.toString());
            }
        }
    }, this.getIntent().getData(), this);
}

@Override
public void onNewIntent(Intent intent) {
    this.setIntent(intent);
}

}

这是我的activity_main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="absdevelopers.com.finalbranchtest.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</RelativeLayout>

这是我的 AndroidManifest.xml 文件:

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

<uses-permission android:name="android.permission.INTERNET"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <data android:scheme="finalbranchtest" android:host="open"/>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE"/>
        </intent-filter>
    </activity>
    <meta-data android:name="io.branch.sdk.BranchKey" android:value="key_live_icb6ztmzyA8s5fhGYkqpNipjrEiiUc7F" />
</application>

LOGCAT

03-17 14:24:38.407 30232-30232/? I/art: Late-enabling -Xcheck:jni
03-17 14:24:38.434 30232-30242/? E/art: Failed sending reply to debugger:      Broken pipe
03-17 14:24:38.434 30232-30242/? I/art: Debugger is no longer active
03-17 14:24:38.539 30232-30232/? E/BranchSDK: Branch instance is not created  yet. Make sure you have initialised Branch. [Consider Calling  getInstance(Context ctx) if you still have issue.]
03-17 14:24:38.539 30232-30232/? D/AndroidRuntime: Shutting down VM
03-17 14:24:38.540 30232-30232/? E/AndroidRuntime: FATAL EXCEPTION: main
                                               Process:  absdevelopers.com.finalbranchtest, PID: 30232
                                               java.lang.RuntimeException: Unable to start activity    ComponentInfo{absdevelopers.com.finalbranchtest/absdevelopers.com.finalbranchtes t.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual  method 'boolean  io.branch.referral.Branch.initSession(io.branch.referral.Branch$BranchReferralIn itListener, android.net.Uri, android.app.Activity)' on a null object reference
                                                    at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
                                                   at   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
                                                   at android.app.ActivityThread.access0(ActivityThread.java:144)
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                   at android.os.Looper.loop(Looper.java:135)
                                                   at android.app.ActivityThread.main(ActivityThread.java:5221)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                   at java.lang.reflect.Method.invoke(Method.java:372)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
                                                Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean io.branch.referral.Branch.initSession(io.branch.referral.Branch$BranchReferralInitListener, android.net.Uri, android.app.Activity)' on a null object reference
                                                   at absdevelopers.com.finalbranchtest.MainActivity.onStart(MainActivity.java:25)
                                                   at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1220)
                                                   at android.app.Activity.performStart(Activity.java:5949)
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
                                                   at android.app.ActivityThread.access0(ActivityThread.java:144) 
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                   at android.os.Looper.loop(Looper.java:135) 
                                                   at android.app.ActivityThread.main(ActivityThread.java:5221) 
                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                   at java.lang.reflect.Method.invoke(Method.java:372) 
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
03-17 14:29:38.617 30232-30232/? I/Process: Sending signal. PID: 30232 SIG: 9

以下是有关 LiveTest 键之间区别的信息:https://dev.branch.io/getting-started/integration-testing/guide/ios/#the-test-sandbox-environment

总而言之,您可以将这些视为 Branch 系统中的独立应用程序,为方便起见,可以从同一仪表板简单地访问这些应用程序。没有数据交叉,两者都提供相同的配置选项,并且您使用不同的 Branch 密钥来访问每个选项。链接遵循在其下创建的 Branch 密钥的配置设置。

我得到了问题的解决方案..

改变

Branch branch = Branch.getInstance();

Branch branch = Branch.getInstance(getApplicationContext());

在 .java 文件中