gpt4 book ai didi

java - 无法使用 getApplication 方法转换为 Application

转载 作者:太空宇宙 更新时间:2023-11-03 12:19:55 25 4
gpt4 key购买 nike

我有类 App,其中包含我的 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)

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>

最佳答案

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

您需要在 list 中指定才能使用您的自定义应用程序。

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

关于java - 无法使用 getApplication 方法转换为 Application,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18760150/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com