简体   繁体   English

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

[英]Android - App crashing with “java.lang.ClassCastException”

I am using a tutorial provided by Google to implement Analytics in my app but something I possibly did wrong that causes the app to crash with java.lang.ClassCastException 我正在使用Google提供教程来在我的应用程序中实现Google Analytics(分析) ,但是我可能做错了某些事情,导致该应用程序因java.lang.ClassCastException而崩溃

This is what Google provided: 这是Google提供的:

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

This is the changes I made because I am using a Fragment 这是我所做的更改,因为我正在使用片段

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

UPDATE : The error happens at this line: 更新:此行发生错误:

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

This is my LogCat 这是我的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)

UPDATE 2 : This is the Manifest file 更新2:这是清单文件

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

Just change this :- 只需更改:-

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

to this :- 对此:

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

It's one of those obvious errors that happen with the best of us! 这是我们所有人中最明显的错误之一!

In addition to implementing the extends Application class, you need to tell Android system which class to use as an Application class. 除了实现extends Application类之外,您还需要告诉Android系统将哪个类用作Application类。 To do so, set the name of your application class in manifest: 为此,请在清单中设置应用程序类的名称:

<application
        android:name="your.package.AnalyticsApplication"

I think you're trying to use a Context object (getApplicationContext()) as an application object. 我认为您正在尝试使用Context对象(getApplicationContext())作为应用程序对象。

Try this: EDIT 试试这个: 编辑

AnalyticsApplication application = (AnalyticsApplication) getActivity().getApplication();

From your fragment you can call the activity that contains the fragment. 您可以从片段中调用包含片段的活动。 When you get the activity, you can use the getApplication() function as in the example. 获得活动后,可以使用示例中的getApplication()函数。

I hope it helps. 希望对您有所帮助。

EDIT 2 编辑2

As seen in this answer: https://stackoverflow.com/a/35905601/4336354 from CommonsWare user. 如该答案所示:CommonsWare用户的https://stackoverflow.com/a/35905601/4336354

Your AndroidManifest.xml file does not contain your custom Application subclass, so Android is using the default one (android.app.Application). 您的AndroidManifest.xml文件不包含自定义的Application子类,因此Android使用的是默认子类(android.app.Application)。

Add an android:name="auc.games2.Analytics.AnalyticsApplication" attribute to your element. 向您的元素添加android:name =“ auc.games2.Analytics.AnalyticsApplication”属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM