片段错误" android.app.Application无法转换为"

时间:2015-07-09 08:35:50

标签: android google-analytics fragment

关注此问题: Using Google Analytics To Track Fragments 我有一个实现Navigation Drawer的应用,因此他们FragmentsGoogle Analytics不在这条线上工作:

final Tracker tracker = ((App) getActivity().getApplication()).getTracker();

Fragment

public class Accueil extends android.support.v4.app.Fragment {

@Override
public void onResume() {

    super.onResume();

    final Tracker tracker = ((App) getActivity().getApplication()).getTracker();
    if(tracker != null){

        tracker.setScreenName(getClass().getSimpleName());
        tracker.send(new HitBuilders.ScreenViewBuilder().build());
    }
}
    }

App

public class App extends Application {
private static final String TAG = "App";
public synchronized Tracker getTracker() {

    try {
        final GoogleAnalytics googleAnalytics = GoogleAnalytics.getInstance(this);
        return googleAnalytics.newTracker(R.xml.app_tracker);

    }catch(final Exception e){
        Log.e(TAG, "Failed to initialize Google Analytics V4");
    }

    return null;
}
}

MainActivity

Navigation Drawer
public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener {
...
}

logcat的:

07-09 08:23:06.688    1125-1125/(package) E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to resume activity {(package)/(package).MainActivity}: java.lang.ClassCastException: android.app.Application cannot be cast to (package).App
            at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2443)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2471)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1985)
            at android.app.ActivityThread.access$600(ActivityThread.java:122)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4340)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to (package).App
            at (package).Accueil.onResume(Accueil.java:53)
            at android.support.v4.app.Fragment.performResume(Fragment.java:1832)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:995)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1120)
            at android.support.v4.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:1939)
            at android.support.v4.app.FragmentActivity.onResumeFragments(FragmentActivity.java:447)
            at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:436)
            at android.support.v7.app.AppCompatActivity.onPostResume(AppCompatActivity.java:138)
            at android.app.Activity.performResume(Activity.java:4552)
            at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2433)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2471)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1985)
            at android.app.ActivityThread.access$600(ActivityThread.java:122)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4340)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
            at dalvik.system.NativeStart.main(Native Method)

将App类放入AndroidManifest时出错:

on:android:label =" @ string / app_name"错误=(包).App不能分配给' Android.app.activity':

<activity
    android:name=".App"
    android:label="@string/app_name"
    android:screenOrientation="portrait">
    <intent-filter>
        <action android:name="android.intent.action.APP" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

1 个答案:

答案 0 :(得分:0)

这里最小清单定义包,应用程序上下文和主要活动:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="my.app.package.name">

    <application
        android:name=".MyCustomApplicationContext"
        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>

</manifest>

重要的属性是:

  1. 清单标记(根节点)属性定义应用包,然后是以开头的所有类包的前缀。
  2. 应用程序标记属性 android:name 定义应用程序上下文类以重载SDK应用程序类
  3. 活动代码属性 android:name 活动类名称
  4. 关于评论如何使用多个应用程序类。 首先,这是不可能的Android应用程序只能有一个上下文。但是您可以将实现链接到单独的功能。 这是一个链式应用程序上下文的示例:

    Base Impl。自定义应用程序上下文介绍了一些有用的方法:

    public class UtilApp extends Application {
        ...
        some useful methods
        ...
    }
    

    通过扩展第一个Impl来链接应用程序上下文。上下文现在,这个包括应用程序和UtilApp中的所有有用方法。

    public class MyApp extends UtilApp {
        ...
        some useful methods
        ...
    }
    

    使用最高的impl。在链中作为你的上下文。

    <application
            android:name=".MyApp" ...>