android.app.Application 无法转换为 com.example..AnalyticsApplication

标签 android google-analytics

我是 android 的新手,正在将谷歌分析添加到我的一个应用程序中。 运行我的应用程序时出现以下错误。

android.app.Application cannot be cast to com.xxx.xxx.xxx.AnalyticsApplication

这是我的主要 Activity 。

 package com.example.dell.testanalytics;

import android.content.Intent;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import android.app.Application;

import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Logger;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;


public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private Tracker mTracker;

     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        AnalyticsApplication application = (AnalyticsApplication) getApplication();
        mTracker = application.getDefaultTracker();


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

// [START screen_view_hit]
        String name = getString(R.string.test);
        Log.i(TAG, "Setting screen name: " + name);
        mTracker.setScreenName("Image~" + name);
        mTracker.send(new HitBuilders.ScreenViewBuilder().build());
        // [END screen_view_hit]
        return false;
    }


}

错误指向我调用分析应用程序方法的地方。

这是我的 list 文件

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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


</application>

我无法弄清楚哪里出了问题,因为我在代码中没有收到任何错误或红线。

如果我能得到你的帮助就太好了。

最佳答案

如果你想替换单例 Application带有 Application 子类的实例, 你需要有 the android:name attribute of the <application> element of your manifest point to that subclass .

关于android.app.Application 无法转换为 com.example..AnalyticsApplication,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32233507/

相关文章:

像 iOS 一样的 Android URL Intent 深度链接

android - 如何在 PopupWindow 上显示键盘?

google-analytics - Google内容实验的样本量

javascript - 如何访问数据层变量

google-analytics - 禁用广告系列谷歌分析

google-analytics - 尝试将网络媒体资源链接到 AdWords (Google Ads) 帐户时出现权限不足 (403)

java - 私有(private)方法上的 Android Fragment NullPointerException

java - 致命异常 : Main Android Eclipse

android - 将 GLES 纹理从 GL_TEXTURE_EXTERNAL_OES 绑定(bind)到 GL_TEXTURE_2D

internet-explorer - IE 11 在 Google Analytics 中作为 Mozilla 出现