使用android应用程序整合branch.io sdk,获取classcasteException

时间:2016-03-17 18:49:20

标签: android classcastexception branch.io

我按照Branch.io上的所有说明进行操作

https://start.branch.io/#/<<按照此链接中的所有步骤操作。 (我应该使用哪个键.. 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.access$800(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.access$800(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

2 个答案:

答案 0 :(得分:5)

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

变化

Branch branch = Branch.getInstance();

Branch branch = Branch.getInstance(getApplicationContext());
<。>在.java文件中

答案 1 :(得分:1)

以下是实时测试键之间差异的信息:https://dev.branch.io/getting-started/integration-testing/guide/ios/#the-test-sandbox-environment

总而言之,您可以将这些视为分支系统中的单独应用程序,只是为了方便起见,这些应用程序可以从同一个仪表板中获得。没有数据交叉,两者都提供相同的配置选项,并且您使用不同的分支键来访问每个分支。链接遵循创建它们的Branch键的配置设置。