【问题标题】:Android: Branch.io tutorial: Branch.getAutoInstance(this);Android:Branch.io 教程:Branch.getAutoInstance(this);
【发布时间】:2016-05-27 11:32:30
【问题描述】:

我正在尝试让 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>


我的主要活动:

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

【问题讨论】:

    标签: branch.io


    【解决方案1】:

    Alex 和 Branch.io 在这里:

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

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

    1. Application onCreate()Activity onCreate() 方法之间的混合,基本实现实际上都不需要这两种方法。
    2. 缺少应用程序类(我们不小心从教程中完全删除了这一步——我深表歉意)。

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

    AndroidManifest.xml

    这里有三个选项:

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

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

    编辑:sn-p UPDATED per cmets 下面

    <?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 类扩展您的 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" >
    

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

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

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

    然后配置你的Application类如下:

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

    活动定义

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

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

    很抱歉给您带来不便!

    【讨论】:

    • 嗨,亚历克斯。很抱歉,但我无法让它工作。是否可以从教程(SDK集成指南)l创建一个项目并将其放在github上?
    • @ChrisG。看看Android testbed app!如上所述,它使用选项 2。
    • 嗨,亚历克斯。你看过这个吗?提前致谢。
    • 嗨@ChrisG.,我看了一下这个。据我所知,这个骨架应用程序并没有完全遵循当前的 SDK 集成指南说明,也没有实现我上面回答中描述的三种方法中的任何一种。通过使用 Option 1,我能够相当容易地编译,但看起来我无法将这些更改直接推送到该 repo,所以我更新了我的答案以包含我使用的代码.让我知道这是否有帮助!
    • 嗨,Alex,我已将您作为合作者添加到 github 项目中:github.com/JCzz/AppBranch_01/invitations
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-20
    • 2011-12-28
    相关资源
    最近更新 更多