无法使用getApplication方法强制转换为Application

时间:2013-09-12 09:14:00

标签: java android android-context

我的课程App包含我Application的上下文。 但是当我编译时,我在这一行的其他课程中得到了error

 App app = (App) getApplication();

班级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;
    }

}

错误:

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>

4 个答案:

答案 0 :(得分:6)

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

您需要在清单中指定使用自定义应用程序。

以下是完整的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>

答案 1 :(得分:1)

使用此.. ..

在Manifest.xml中

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

在活动中

App app = (App)getApplicationContext();

答案 2 :(得分:0)

更新此

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

答案 3 :(得分:0)

您是否在清单应用程序标签中注册了新的应用程序对象(App)?请参阅以下应用程序标记代码段以获取解决方案。

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