且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

android.app.Application 无法转换为 android.app.Activity

更新时间:2022-11-16 22:29:41

您传递的是 Application Context 而不是 Activity Context

You are passing the Application Context not the Activity Context with

getApplicationContext();

无论你在哪里传递它,都通过 thisActivityName.this 代替.

Wherever you are passing it pass this or ActivityName.this instead.

由于您试图将 Context 传递给 Activity

Since you are trying to cast the Context you pass (Application not Activity as you thought) to an Activity with

(Activity)

你得到这个异常是因为你不能将 Application 转换为 Activity 因为 Application 不是 Activity 的子类.

you get this exception because you can't cast the Application to Activity since Application is not a sub-class of Activity.