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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23546303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:47:01  来源:igfitidea点击:

android.app.Application cannot be cast to android.app.Activity

javaandroidandroid-activityclasscastexception

提问by DomeWTF

I'm trying to change a LinearLayoutfrom another class, but when i run this code:

我正在尝试LinearLayout从另一个类更改 a ,但是当我运行此代码时:

public class IRC extends PircBot {

ArrayList<String> channels;
ArrayList<Integer> userCount;
ArrayList<String> topics;

LinearLayout channelLayout;
Context context;

public IRC(Context ctx) {
    this.setName("xxxx");
    channels = new ArrayList<String>();
    userCount = new ArrayList<Integer>();
    topics = new ArrayList<String>();

    context = ctx;

    channelLayout = (LinearLayout) ((Activity) context).findViewById(R.id.channels);
}

i get a ClassCastException

我得到一个 ClassCastException

context is the Main activity that extends Activitypassed with a getApplicationContext();

context 是Activity通过 getApplicationContext() 传递的 Main 活动;

LOGCAT

逻辑猫

05-08 17:53:55.102    3736-3799/g.d.allinonechat E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-5357
java.lang.ClassCastException: android.app.Application cannot be cast to android.app.Activity
        at g.d.xxx.IRC.<init>(IRC.java:34)
        at g.d.xxx.MainActivity.run(MainActivity.java:49)
        at java.lang.Thread.run(Thread.java:856)

采纳答案by codeMagic

You are passing the Application Contextnot the Activity Contextwith

您正在传递应用程序Context而不是Activity Context

getApplicationContext();

Wherever you are passing it pass thisor ActivityName.thisinstead.

无论你在哪里通过它通过thisActivityName.this相反。

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

由于您正在尝试将Context您传递的(应用程序不是您认为的活动)转换为Activitywith

(Activity)

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

您会收到此异常,因为您无法将 Application 强制转换为,Activity因为 Application 不是Activity.

回答by Gerrard

In my case, when I'm in an activity that extends from AppCompatActivity, it did not work(Activity) getApplicationContext (), I just putthisin its place.

就我而言,当我参与从 扩展的活动时AppCompatActivity,它不起作用(Activity) getApplicationContext (),我只是将this其放在了它的位置。

回答by Nicolai Weitkemper

For me, removing android:name=".MainActivity"from <application …did the trick.

对我来说,除去android:name=".MainActivity"<application …没有的伎俩。

回答by Aakash Sharma

You are getting this error because the parameter required is Activity and you are passing it the Application. So, either you cast application to the Activity like: (Activity)getApplicationContext();Or you can just type the Activity like: MyActivity.this

您收到此错误是因为所需的参数是 Activity 并且您将其传递给应用程序。因此,您可以将应用程序转换为 Activity ,例如:(Activity)getApplicationContext();或者您可以键入 Activity ,例如:MyActivity.this