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

Android: Branch.io tutorial: Branch.getAutoInstance(this);

我正在努力让 Branch.io 继续 Android,但我 运行 喜欢:

myapplication.MainActivity cannot be cast to android.app.Application

然后我改了:

Branch.getAutoInstance(this);

收件人:

Branch.getInstance();


onCreate的Activity.

然后我得到:

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)

你能帮我打基础吗运行?

以下是我的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>


我的主要Activity:

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);
    }
}

Alex 与 Branch.io 在这里:

我们最近对教程做了一些更改,看起来我们遗漏了一些东西。感谢您发布有关此内容的信息 - 我们将在今天晚些时候推送更新以更加清晰。

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

  1. Application onCreate()Activity onCreate() 方法之间的混淆,两者都不是基本实现实际上需要这些。
  2. 缺少应用程序 class(我们不小心从教程中完全删除了这一步 - 抱歉)。

要起床 运行,按如下方式更新您的文件:

AndroidManifest.xml

这里有三个选项:

1。使用 Branch 应用程序 class(最简单)

如果您还没有自定义应用程序 class,这是最简单的方法。将 android:name="io.branch.referral.BranchApp" 添加到您的 Application class:

编辑:根据以下评论更新片段

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

2。使用 BranchApp class

扩展您的应用程序 class

如果您已经有自定义应用程序class,这是最简单的方法。您的 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" >

您的自定义应用程序 class(上例中的 CustomApplicationClass)将如下所示:

public final class CustomApplicationClass extends YourApp {
    @Override
    public void onCreate() {
        super.onCreate();
    }
}

3。直接集成到您的自定义应用程序中 class

最自定义的方法,用于高级实现。您的 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" >

然后按如下方式配置您的应用程序 class:

public final class CustomApplicationClass {
    @Override
    public void onCreate() {
        super.onCreate();
        Branch.getAutoInstance(this);
    }
}

Activity定义

删除 onCreate() 调用。此处不需要它们,它们实际上是错误消息的原因(Branch.getAutoInstance(this)Activity 上下文作为 this 传递,当 SDK期待来自上面选项 3 的 Application 上下文)。

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);
    }
}

抱歉给您带来不便!