java.lang.ClassCastException : Application1 cannot be cast to Application2

标签 java android

我收到以下错误:

java.lang.ClassCastException: com.example.cillin.map.AuthenticationApplication cannot be cast to com.example.cillin.map.NBHAuthenticationApplication

这是标记错误的行:

NBHAuthenticationApplication myNBHApp = (NBHAuthenticationApplication) getApplication();

这是该行所在的类:

import android.app.Activity;
import android.os.Bundle;

public class NBHBaseActivity extends Activity
{
    protected NBHAuthService mNBHAuthService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        NBHAuthenticationApplication myNBHApp = (NBHAuthenticationApplication) getApplication();
        myNBHApp.setCurrentNBHActivity(this);
        mNBHAuthService = myNBHApp.getNBHAuthService();
    }
}

这是 NBHAuthenticationApplication 类:

public class NBHAuthenticationApplication extends Application
{
    private NBHAuthService mNBHAuthService;
    private Activity mCurrentNBHActivity;

    public NBHAuthenticationApplication() {}

    public NBHAuthService getNBHAuthService() {
        if (mNBHAuthService == null) {
            mNBHAuthService = new NBHAuthService(this);
        }
        return mNBHAuthService;
    }

    public void setCurrentNBHActivity(Activity NBHactivity) {
        mCurrentNBHActivity = NBHactivity;
    }

    public Activity getCurrentActivity() {
        return mCurrentNBHActivity;
    }
}

关于为什么会发生此错误有什么想法吗?我已按照类似问题中的建议将该文件添加到我的 list 文件中并出现此错误,但仍然没有运气..

list :

<application
        android:largeHeap="true"
        android:name="AuthenticationApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >


        <!--
             The API key for Google Maps-based APIs is defined as a string resource.
             (See the file "res/values/google_maps_api.xml").
             Note that the API key is linked to the encryption key used to sign the APK.
             You need a different API key for each encryption key, including the release key that is used to
             sign the APK for publishing.
             You can define the keys for the debug and release targets in src/debug/ and src/release/. 
        -->
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key" />
        <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

        <activity
            android:name=".MapsActivity"
            android:label="@string/title_activity_maps" >

        </activity>
        <activity
            android:name=".MainMenu"
            android:label="@string/MainMenu" >

        </activity>
        <activity
            android:name=".CrimeInput"
            android:label="@string/MainMenu" >
        </activity>
        <activity
            android:name=".CoverPage"
            android:label="@string/MainMenu" >
        </activity>
        <activity
            android:name=".LoggedInActivity"
            android:label="LoggedIn" >
        </activity>
        <activity
            android:name=".RegisterAccountActivity"
            android:label="Register" >
        </activity>
        <activity
            android:name=".BaseActivity"
            android:label="Base" >
        </activity>
        <activity
            android:name=".AuthenticationActivity"
            android:label="Authentication" >
        </activity>
        <activity
            android:name=".NBHBaseActivity"
            android:label="Base" >
        </activity>
        <activity
            android:name=".CustomLoginActivity"
            android:label="Login" >
        </activity>
        <activity
            android:name=".Newsfeed"
            android:label="Login"
            android:windowSoftInputMode="adjustResize">
        </activity>
        <activity
        android:name=".NewsfeedInput"
        android:label="@string/MainMenu" >
    </activity>
        <activity
        android:name=".InfoWindowList"
        android:label="@string/MainMenu" >
    </activity>
        <activity
            android:name=".CrimeStats"
            android:label="@string/MainMenu" >
        </activity>
        <activity
        android:name=".GardaRegister"
        android:label="@string/MainMenu" >
    </activity>
        <activity
            android:name=".GardaLoginActivity"
            android:label="@string/MainMenu" >
        </activity>
        <activity
            android:name=".NeighborhoodRegister"
            android:label="@string/MainMenu" >
        </activity>
        <activity
            android:name=".NeighborhoodLogin"
            android:label="@string/MainMenu" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </>

</manifest>

最佳答案

替换:

android:name="AuthenticationApplication"

与:

android:name="NBHAuthenticationApplication"

关于java.lang.ClassCastException : Application1 cannot be cast to Application2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35963171/

相关文章:

android - 我可以关闭模​​拟器的省电功能吗?

android - Google 客户端 Api 未在 Activity 恢复时连接

java - 如何使用widget.switch更改editText值Android

java - 制作Android应用程序的欢迎页面

java - 如何在 Selenium WebDriver 的属性文件中使用多行属性?

android - 在Android中,有没有办法在新的RecyclerView中实现快速滚动?

android - 在 ListView 中获取绝对 View 位置

java - 检查editText是否为空

java - 无法反序列化 - Jackson LocalDate/Time - JUnit

java - 数组或对象的 Json 响应解析器