无法将班级强制转换为活动

时间:2019-05-31 16:07:04

标签: java android

我正在尝试用非活动类SplashScreenActivity的上下文来完成我的SessionManager,但是我不断收到此错误:

com.package.Utils.AppController cannot be cast to android.app.Activity

我试图用在网上找到的代码((Activity)_context).finish()

完成活动

这是我的代码

SessionManager.class

public class SessionManager {
 ...
 public void checkLogin() {

    if (!this.isLoggedIn()) {

        Intent intent = new Intent(_context, WelcomeActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        _context.startActivity(intent);
        ((Activity)_context).finish();
    } else {

        Intent intent = new Intent(_context, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        _context.startActivity(intent);
        ((Activity)_context).finish();
    }
}
}

AppController.class

public class AppController extends Application {

private static final String TAG = AppController.class.getSimpleName();

private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;

private static AppController mInstance;

@Override
public void onCreate() {
    super.onCreate();
    mInstance = this;
}

public static synchronized AppController getInstance() {
    return mInstance;
}

public RequestQueue getRequestQueue() {
    if (mRequestQueue == null) {
        mRequestQueue = Volley.newRequestQueue(getApplicationContext());
    }
    return mRequestQueue;
}

public ImageLoader getImageLoader() {
    getRequestQueue();
    if (mImageLoader == null) {
        mImageLoader = new ImageLoader(this.mRequestQueue,
                new LruBitmapCache());
    }
    return this.mImageLoader;
}

public <T> void addToRequestQueue(Request<T> req, String tag) {
    req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
    getRequestQueue().add(req);
}

public <T> void addToRequestQueue(Request<T> req) {
    req.setTag(TAG);
    getRequestQueue().add(req);
}

public void cancelPendingRequests(Object tag) {
    if (mRequestQueue != null) {
        mRequestQueue.cancelAll(tag);
    }
}

}

在我的清单上我有这个

<application
           android:name=".Utils.AppController"
...
</application>

0 个答案:

没有答案