android.app.Application不能转换为com.example..AnalyticsApplication [英] android.app.Application cannot be cast to com.example..AnalyticsApplication

查看:648
本文介绍了android.app.Application不能转换为com.example..AnalyticsApplication的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid和加入谷歌分析我的应用程序之一的过程。
运行我的应用程序时,我收到以下错误。

  android.app.Application不能转换为com.xxx.xxx.xxx.AnalyticsApplication

这是我的主要活动。

 包com.example.dell.testanalytics;进口android.content.Intent;
进口android.support.v4.app.FragmentPagerAdapter;
进口android.support.v4.view.ViewPager;
进口android.support.v7.app.AppCompatActivity;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.Menu;
进口android.view.MenuItem;进口android.app.Application;进口com.google.android.gms.analytics.GoogleAnalytics;
进口com.google.android.gms.analytics.Logger;
进口com.google.android.gms.analytics.HitBuilders;
进口com.google.android.gms.analytics.Tracker;
公共类MainActivity扩展AppCompatActivity {
    私有静态最后弦乐TAG =MainActivity;
    私人跟踪mTracker;     @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        AnalyticsApplication应用=(AnalyticsApplication)getApplication();
        mTracker = application.getDefaultTracker();
    }    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){        。getMenuInflater()膨胀(R.menu.menu_main,菜单);
        返回true;
    }    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){// [START screen_view_hit]
        字符串名称=的getString(R.string.test);
        Log.i(TAG,设置网名:+姓名);
        mTracker.setScreenName(图像〜+姓名);
        mTracker.send(新HitBuilders.ScreenViewBuilder()建立());
        // [END screen_view_hit]
        返回false;
    }
}

错误指向我称之为分析应用方法的地方。

这是我的清单文件

 <?XML版本=1.0编码=UTF-8&GT?;

 <使用许可权的android:NAME =android.permission.INTERNET对/>
<使用许可权的android:NAME =android.permission.ACCESS_NETWORK_STATE/><应用
    机器人:allowBackup =真
    机器人:图标=@的mipmap / ic_launcher
    机器人:标签=@字符串/ APP_NAME
    机器人:主题=@风格/ AppTheme>
    <活动
        机器人:名字=。MainActivity
        机器人:标签=@字符串/ APP_NAME>
        &所述;意图滤光器>
            <作用机器人:名字=android.intent.action.MAIN/>            <类机器人:名字=android.intent.category.LAUNCHER/>
        &所述; /意图滤光器>
    < /活性GT;
< /用途>

我无法弄清楚什么是错的,因为我没有得到在code任何错误或红线。

将是巨大的,如果我能得到你的帮助。


解决方案

如果您希望与应用程序的某个子类来代替单应用实例,你需要有的android:名称的属性<应用> 您的清单指向该子类的元素。

I am new to android and in a process of adding google analytics to one of my application. I am getting the following error when running my application.

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

This is my main 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;
    }


}

The error is pointing the place I call the analytics application method.

This is my manifest file

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

I cannot figure out what is wrong since i am not getting any errors or red lines in the code.

Would be great if i could get your assistance.

解决方案

If you wish to replace the singleton Application instance with some subclass of Application, you need to have the android:name attribute of the <application> element of your manifest point to that subclass.

这篇关于android.app.Application不能转换为com.example..AnalyticsApplication的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆