Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
487 views
in Technique[技术] by (71.8m points)

java - Can't cast to Application with getApplication method

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

 App app = (App) getApplication();

Class App:

import android.app.Application;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.example.Radio_KPI.R;
import com.example.Radio_KPI.Utils.Const;

public class App extends Application {

    private MediaPlayer player;
    private NotificationManager manager;
    private String curSong;
    private Context Main_con;

    public Context getMain_con() {
        return Main_con;
    }

    public void setMain_con(Context main_con) {
        Main_con = main_con;
    }

    public String getCurSong() {
        return curSong;
    }

    public void setCurSong(String curSong) {
        this.curSong = curSong;
    }

    public NotificationManager getManager() {
        return manager;
    }

    public void setManager(NotificationManager manager) {
        this.manager = manager;
    }

    public MediaPlayer getPlayer() {
        return player;
    }

    public void setPlayer(MediaPlayer mPlayer) {
        this.player = mPlayer;
    }

}

Error:

java.lang.ClassCastException: android.app.Application cannot be cast to com.example.Radio_KPI.Application.AppContext
        at com.example.Radio_KPI.Activities.Main.onCreate(Main.java:62)
        at android.app.Activity.performCreate(Activity.java:4531)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)

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: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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
<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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...