java - Android - 应用程序崩溃并显示 "java.lang.ClassCastException"

标签 java android google-analytics classcastexception

我正在使用tutorial provided by Google在我的应用程序中实现Analytics,但我可能做错了一些事情,导致应用程序崩溃并出现java.lang.ClassCastException

这是 Google 提供的:

// Obtain the shared Tracker instance.
AnalyticsApplication application = (AnalyticsApplication) getApplication();
mTracker = application.getDefaultTracker();

这是我所做的更改,因为我使用 fragment

// This is where I get the error
AnalyticsApplication application = (AnalyticsApplication) getContext().getApplicationContext();
mTracker = application.getDefaultTracker();

更新:错误发生在这一行:

AnalyticsApplication application = (AnalyticsApplication) getContext().getApplicationContext();

这是我的 LogCat

FATAL EXCEPTION: main
Process: com.incorp.labs.appname, PID: 14095
java.lang.ClassCastException: android.app.Application cannot be cast to com.incorp.labs.appname.Helper.AnalyticsTracker
  at com.incorp.labs.appname.OneFragment.onCreateView(OneFragment.java:126)

更新 2:这是 list 文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.incorp.labs.appname">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="com.android.vending.BILLING" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/newlogops"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/MyMaterialTheme">
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    <activity
        android:name=".Splash"
        android:screenOrientation="portrait" />
    <activity
        android:name=".MainActivity"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustPan" />
    <activity
        android:name=".OneFragment"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustPan" />
    <activity
        android:name=".TwoFragment"
        android:screenOrientation="portrait" />
    <activity
        android:name=".Feedback"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustPan" />
    <activity
        android:name=".FourFragment"
        android:screenOrientation="portrait" />
    <activity
        android:name=".SplashTimer"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
        android:theme="@android:style/Theme.Translucent" />

    <service android:name=".FirebaseMessagingService">
        <intent-filter>                     
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
             
        </intent-filter>
    </service>

    <activity android:name=".AboutActivity"></activity>
</application>

最佳答案

只需更改此:-

AnalyticsTracker application = (AnalyticsTracker) getContext().getApplicationContext();

对此:-

AnalyticsApplication application = (AnalyticsApplication) getContext().getApplicationContext();

这是我们最优秀的人都会犯的明显错误之一!

关于java - Android - 应用程序崩溃并显示 "java.lang.ClassCastException",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46236977/

相关文章:

java - 为什么我在测试中收到 NullPointerException?

java - Android:Dagger 2 构造函数注入(inject)不会调用构造函数并最终出现 NPE

java - 是否可以将 TestNG 插件与 Netbeans 6.8 一起使用?

Java 编译、包和相对路径

android - 没有 firebase 的 AdMob 广告

google-analytics - Google Analytics : Spreadsheet Add-on, 无效段 ga:region

java - 将数据导入 SQLite Android 应用程序数据库

android - 解释 Android 构建系统文档中描述的传递依赖策略

javascript - 使用 Google 跟踪代码管理器跟踪滑动事件

google-analytics - DAU/MAU Google 数据工作室