Android:Branch.io教程:Branch.getAutoInstance(this);

Chr*_* G. 6 branch.io

我想让Branch.io在Android上运行,但我遇到了:

myapplication.MainActivity cannot be cast to android.app.Application
Run Code Online (Sandbox Code Playgroud)

然后我改变了:

Branch.getAutoInstance(this);
Run Code Online (Sandbox Code Playgroud)

至:

Branch.getInstance();
Run Code Online (Sandbox Code Playgroud)


onCreate活动中.

然后我得到:

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 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
Run Code Online (Sandbox Code Playgroud)

你能帮助我完成基本的运行吗?

以下是我的AndroidManifest.xml :(注意:我的应用程序代码中添加了branch_key)

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

    <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">

        <meta-data android:name="io.branch.sdk.BranchKey" android:value="key_live_xxxxxxxxxxxxxxx" />

        <activity android:name=".MainActivity">
            <intent-filter>
                <data android:scheme="yourapp" android:host="open" />
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

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

        <receiver android:name="io.branch.referral.InstallListener" android:exported="true">
            <intent-filter>
                <action android:name="com.android.vending.INSTALL_REFERRER" />
            </intent-filter>
        </receiver>

    </application>

</manifest>
Run Code Online (Sandbox Code Playgroud)


我的主要活动:

package com.example.chg.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.util.Log;

import org.json.JSONObject;

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

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Branch.getAutoInstance(this);
        Branch.getInstance();
        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 -> was re-directed to this app
                    // params will be empty if no data found
                    // ... insert custom logic here ...
                } else {
                    Log.i("MyApp", error.getMessage());
                }
            }
        }, this.getIntent().getData(), this);
    }

    @Override
    public void onNewIntent(Intent intent) {
        this.setIntent(intent);
    }
}
Run Code Online (Sandbox Code Playgroud)

Ale*_*uer 7

Alex和Branch.io在这里:

我们最近对我们的教程进行了一些更改,看起来我们错过了一些东西.感谢您发布此消息 - 我们将在今天晚些时候推出更新,以便更加清晰.

在这种特殊情况下,有两个问题:

  1. Application onCreate()Activity onCreate()方法之间的混合,基本实现实际上都不需要.
  2. 一个缺少的Application类(我们完全从我们的教程中意外删除了这一步 - 我的道歉).

要启动并运行,请按如下方式更新文件:

AndroidManifest.xml中

这里有三个选项:

1.使用Branch应用程序类(最简单)

如果您还没有自定义应用程序类,这是最简单的方法.加入android:name="io.branch.referral.BranchApp"你的Application班级:

编辑:代码段UPDATED每条评论如下

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

    <meta-data android:name="io.branch.sdk.BranchKey" android:value="xxx" />

    <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"
        android:name="io.branch.referral.BranchApp">

        <!--Enable test mode to see debugging data
         (https://dev.branch.io/getting-started/integration-testing/guide/android/#use-debug-mode-to-simulate-fresh-installs)-->
        <meta-data android:name="io.branch.sdk.TestMode" android:value="true" />
        <meta-data android:name="io.branch.sdk.BranchKey" android:value="key_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" />

        <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="theapp" 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>

        <receiver android:name="io.branch.referral.InstallListener" android:exported="true">
            <intent-filter>
                <action android:name="com.android.vending.INSTALL_REFERRER" />
            </intent-filter>
        </receiver>

    </application>

</manifest>
Run Code Online (Sandbox Code Playgroud)

2.使用BranchApp类扩展Application 类

如果您已经有自定义Application类,这是最简单的方法.您的AndroidManifext.xml文件将如下所示:

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:name="com.your.app.CustomApplicationClass" >
Run Code Online (Sandbox Code Playgroud)

您的自定义Application类(CustomApplicationClass在上面的示例中)将如下所示:

public final class CustomApplicationClass extends YourApp {
    @Override
    public void onCreate() {
        super.onCreate();
    }
}
Run Code Online (Sandbox Code Playgroud)

3.直接集成到自定义应用程序类中

高级实现的最自定义方法.您的AndroidManifext.xml文件设置与上面相同:

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:name="com.your.app.CustomApplicationClass" >
Run Code Online (Sandbox Code Playgroud)

然后按如下方式配置Application类:

public final class CustomApplicationClass {
    @Override
    public void onCreate() {
        super.onCreate();
        Branch.getAutoInstance(this);
    }
}
Run Code Online (Sandbox Code Playgroud)

活动定义

删除onCreate()电话.它们在这里不需要,实际上是您的错误消息的原因(当SDK期望上面的选项3 的应用程序上下文时,Branch.getAutoInstance(this)正在传递Activity上下文).this

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

public class MainActivity extends Activity {

    @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 -> was re-directed to this app
                    // params will be empty if no data found
                    // ... insert custom logic here ...
                } else {
                    Log.i("MyApp", error.getMessage());
                }
            }
        }, this.getIntent().getData(), this);
    }

    @Override
    public void onNewIntent(Intent intent) {
        this.setIntent(intent);
    }
}
Run Code Online (Sandbox Code Playgroud)

抱歉给你带来不便!