Java – cannot cast to application using getapplication method
•
Java
I have a class app that contains the context of my application
App app = (App) getApplication();
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)
<?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>
Solution
<application
<application android:label="@string/app_name" android:icon="@drawable/one" android:name=".App" >
<?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>
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码