gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 14:41:36 28 4
gpt4 key购买 nike

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

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

然后我改变了:
Branch.getAutoInstance(this);

到:
Branch.getInstance();

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)

你能帮我启动和运行基本的吗?

以下是我的 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 在这里:

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

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

  • Application onCreate() Activity onCreate() 之间的混合。实际上这两种方法都不需要基本实现
  • 缺少 Application 类(我们不小心从教程中完全删除了这一步 - 抱歉)。

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

    AndroidManifest.xml

    您在这里有三个选择:

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

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

    编辑:根据 以下的评论更新片段
    <?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 上下文 6792300 选项时)。
    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);
    }
    }

    带来不便敬请谅解!

    关于branch.io - Android : Branch. io 教程 : Branch. getAutoInstance(this);,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37482405/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com