3

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

4 回答 4

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>
于 2013-09-12T09:19:00.660 回答
1

用这个..

在 Manifest.xml 中

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

在活动中

App app = (App)getApplicationContext();
于 2013-09-12T09:22:00.017 回答
1

更新这个

<application
        android:name=".App"
        android:icon="@drawable/ijoomer_luncher_icon"
        android:label="@string/app_name"
 >
于 2013-09-12T09:19:49.210 回答
0

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

<application
        android:name=".App" 
        android:icon="@drawable/launcher_logo"
        android:label="@string/app_name" ... >
于 2013-09-12T09:20:27.380 回答