简体   繁体   English

在清单中设置的 Android 自定义应用程序类不适用于 ClassCastException

[英]Android custom Application class with set up at manifest don't work with ClassCastException

I don't know what is problem, because in my other projects it is work.我不知道什么是问题,因为在我的其他项目中它是工作。 I'm use custom application class and register it in my manifest file:我正在使用自定义应用程序类并将其注册到我的清单文件中:

package ru.jooogle.sunshine.admin_pc.sunshine_reborn;

import android.app.Application;
import android.content.Context;
import ru.jooogle.sunshine.admin_pc.sunshine_reborn.model.network.NetworkService;

    public class SunshineApplication extends Application {
        private NetworkService networkService;

        public static SunshineApplication get(Context context) {
            return (SunshineApplication) context.getApplicationContext();
        }

        public NetworkService getNetworkService() {
            if (networkService == null) {
                networkService = new NetworkService();
            }

            return networkService;
        }
    }

And manifest:并体现:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ru.jooogle.sunshine.admin_pc.sunshine_reborn">

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

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

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

</manifest>

In Activity onCreate i do:在 Activity onCreate 我做:

SunshineApplication sunshineApplication = SunshineApplication.get(this);

and i get ClassCastException:我得到 ClassCastException:

java.lang.ClassCastException: android.app.Application cannot be cast to ru.jooogle.sunshine.admin_pc.sunshine_reborn.SunshineApplication

I don't know what is it, help, please.我不知道是什么,请帮忙。

I'm found solution.我找到了解决方案。 In this project i use experimental feature buildCach in gradle.properties :在这个项目中,我在 gradle.properties 中使用了实验功能 buildCach :

android.enableBuildCache=true

if comment this line and rebuild project, problem gone如果注释此行并重建项目,问题就消失了

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

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