Can't cast to Application with getApplication method

后端 未结 4 1945
天命终不由人
天命终不由人 2020-12-19 20:16

I have class App which contain context of my Application. But when I compiled I got an error in other class in this line:



        
相关标签:
4条回答
  • 2020-12-19 20:52

    use this..

    in Manifest.xml

    <application
    android:label="@string/app_name"
    android:icon="@drawable/one"
    android:name=".App" >
    

    in Activity

    App app = (App)getApplicationContext();
    
    0 讨论(0)
  • 2020-12-19 21:06
    <application
        android:label="@string/app_name"
        android:icon="@drawable/one"
        android:name=".App" >
    

    You need to specify in your manifest to use your custom Application.

    Here is the complete AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
              package="com.example.Radio_KPI"
              android:versionCode="1"
              android:versionName="1.0">
    
        <uses-sdk android:minSdkVersion="8"/>
        <uses-sdk android:targetSdkVersion="14"/>
    
        <uses-permission android:name="android.permission.INTERNET"/>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    
        <application
        android:name=".App"
        android:label="@string/app_name"
        android:icon="@drawable/one">
            <activity android:name=".Activities.Main"
                      android:screenOrientation="portrait"
                      android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
                      android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN"/>
                    <category android:name="android.intent.category.LAUNCHER"/>
                </intent-filter>
            </activity>
            <activity android:name=".Activities.About"
                      android:screenOrientation="portrait"
                      android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
                      android:label="@string/app_name">
            </activity>
            <activity android:name=".Activities.SavedSongs"
                      android:screenOrientation="portrait"
                      android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
                      android:label="@string/savedSongs">
            </activity>
            <service
                    android:name=".Services.MyService">
            </service>
        </application>
    </manifest>
    
    0 讨论(0)
  • 2020-12-19 21:10

    Update this

    <application
            android:name=".App"
            android:icon="@drawable/ijoomer_luncher_icon"
            android:label="@string/app_name"
     >
    
    0 讨论(0)
  • 2020-12-19 21:11

    Do you have your new application object (App) registered in the manifest application tag? See the following application tag snippet for solution.

    <application
            android:name=".App" 
            android:icon="@drawable/launcher_logo"
            android:label="@string/app_name" ... >
    
    0 讨论(0)
提交回复
热议问题