首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓: Branch.io教程:Branch.getAutoInstance(这个);

安卓: Branch.io教程:Branch.getAutoInstance(这个);
EN

Stack Overflow用户
提问于 2016-05-27 11:32:30
回答 1查看 4.8K关注 0票数 7

我试图让Branch.io在Android上工作,但我遇到了以下情况:

代码语言:javascript
复制
myapplication.MainActivity cannot be cast to android.app.Application

然后我改变了:

代码语言:javascript
复制
Branch.getAutoInstance(this);

至:

代码语言:javascript
复制
Branch.getInstance();

onCreate中的活动。

然后我得到:

代码语言:javascript
复制
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是在我的应用程序代码中添加的)

代码语言:javascript
复制
<?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>

我的主要活动:

代码语言:javascript
复制
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);
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-27 21:23:00

Branch.io的亚历克斯在这里:

我们最近对我们的教程做了一些修改,看起来我们遗漏了一些东西。我很感谢你的帖子-我们将在今天晚些时候推动一个更新,以获得更多的清晰度。

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

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

若要启动和运行,请按以下方式更新您的文件:

AndroidManifest.xml

你在这里有三个选择:

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

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

编辑:根据下面的注释更新代码段

代码语言:javascript
复制
<?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类扩展应用程序类

如果您已经有一个自定义应用程序类,这是最简单的方法。您的AndroidManifext.xml文件将如下所示:

代码语言:javascript
复制
<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" >

您的自定义应用程序类(在上面的示例中为CustomApplicationClass)如下所示:

代码语言:javascript
复制
public final class CustomApplicationClass extends YourApp {
    @Override
    public void onCreate() {
        super.onCreate();
    }
}

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

最自定义的方法,用于高级实现。您可以让您的AndroidManifext.xml文件设置与上面的相同:

代码语言:javascript
复制
<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" >

然后按照以下方式配置应用程序类:

代码语言:javascript
复制
public final class CustomApplicationClass {
    @Override
    public void onCreate() {
        super.onCreate();
        Branch.getAutoInstance(this);
    }
}

活动定义

删除onCreate()调用。这里不需要它们,实际上是错误消息的原因(Branch.getAutoInstance(this)Activity上下文作为this传递,而SDK期望从上面的选项3获得应用程序上下文)。

代码语言:javascript
复制
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);
    }
}

很抱歉给您带来不便!

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37482405

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档