0

在 Google 的 Kotlin 介绍课程之后,我创建了一个掷骰子应用程序。我现在正在实施 Firebase Analytics 来跟踪每个掷出的骰子。

但是,我不知道为什么我的事件没有被记录,它们没有出现在我的 Logcat 选项卡中......当我初始化我的应用程序时,我可以看到各种 Firebase 日志,但是当我点击我拥有的按钮时一个事件,Firebase 不会记录它。我检查了我的代码,我认为问题不是来自这里。

有人帮我吗?

我与您分享我的代码以及我在终端和目录标签中的所有消息。

package com.example.rolldiceapp

import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.google.firebase.analytics.FirebaseAnalytics
import com.google.firebase.analytics.FirebaseAnalytics.Param.*
import com.google.firebase.analytics.ktx.logEvent


/*** This activity allows the user to roll a dice and view the result on the screen.***/

class MainActivity : AppCompatActivity() {

    private lateinit var firebaseAnalytics: FirebaseAnalytics //Declare the FirebaseAnalytics object at the top of the activity

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main) //Set layout with the Activity
        firebaseAnalytics = FirebaseAnalytics.getInstance(this) //Initialize Firebase Analytics in the OnCreate method

        val rollButton: Button = findViewById(R.id.roll_button)
        rollButton.setOnClickListener {
            rollDice()
            trackClicks()
        }

    }

    private fun rollDice() {

        // Create new Dice object with 6 sides and roll it
        val dice = Dice(6)
        val diceRoll = dice.rollDice()

        // Update the screen with the dice roll number
        val resultTextView: TextView = findViewById(R.id.roll_textView)
        resultTextView.text = diceRoll.toString()

        // Update the screen with the dice roll image
        val diceImage: ImageView = findViewById(R.id.roll_imageView)
        diceImage.setImageResource(R.drawable.dice_2)
        when (diceRoll) {
            1 -> diceImage.setImageResource(R.drawable.dice_1)
            2 -> diceImage.setImageResource(R.drawable.dice_2)
            3 -> diceImage.setImageResource(R.drawable.dice_3)
            4 -> diceImage.setImageResource(R.drawable.dice_4)
            5 -> diceImage.setImageResource(R.drawable.dice_5)
            6 -> diceImage.setImageResource(R.drawable.dice_6)
        }

        //Update the screen with result message
        val luckyNumber = 4
        val resultMessage: TextView = findViewById(R.id.resultRollText)
        if (diceRoll == luckyNumber) {
            resultMessage.text = ("You win! You rolled $diceRoll and it is the lucky number!").toString()
        } else {
            resultMessage.text = ("Sorry you rolled a $diceRoll and you need a $luckyNumber. Try again!").toString()
        }

    }

    private fun trackClicks() {
        firebaseAnalytics.logEvent("Click_Dice_track_2") {
            param(SCREEN_NAME, "Dice_Homepage") // send predefined parameters
            param(SCORE, value = "test")
            param(SOURCE, "Local_Machine")
        }
    }

}

    class Dice(val numSides: Int) {
        fun rollDice(): Int {
            return (1..numSides).random()
        }
    }

当我使用 Firebase 初始化我的应用程序时,我将与您分享终端和 Logcat 选项卡中的消息。

在此处输入图像描述

在此处输入图像描述

4

1 回答 1

2

您可以尝试启用调试模式 要在 Android 设备上启用分析调试模式,请在终端执行以下命令(不要忘记添加包名称):

adb shell setprop debug.firebase.analytics.app com.example.rolldiceapp

如果您运行的模拟器不止一个,则必须使用 adb -s SERIAL(SERIAL 代表模拟器的唯一 ID)。如果连接了一个设备和一个仿真器,您可以使用快捷方式: adb -d ... 用于设备, adb -e ... 用于仿真器。

从启用调试模式执行步骤后,请确保调试设备或模拟器和 PC 上的日期和时间正确。如果更正日期和时间事件后仍未在 DebugView 上显示,请清除应用存储。然后重新启动应用程序并重试。

还要确保您在设备/模拟器中安装了最新的 Google Play 服务,否则无法保证一切正常。如果您转到模拟器上的设置,则有更新按钮。不幸的是,它要求您通过您的 Google 帐户登录。

你也可以研究这个问题。似乎你的问题类似于这个问题。所以检查所有答案,有非常有用的

于 2021-01-21T19:55:09.423 回答