本文介绍了为什么会出现错误"MyActivity不是封闭的类?"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个libgdx程序,该程序以以下类开头:

I had a libgdx program which starts with the following class:

public class MyActivity extends AndroidApplication implements IActivityRequestHandler

我需要一个Activity类来使用Display检测屏幕尺寸(我不能在AndroidApplication类中做到这一点).

I needed to have an Activity class to detect screen size using Display (I can't do that in an AndroidApplication class).

因此,我将以下类添加为启动器Activity:

So I added the following class as my launcher Activity:

public class MyActivity1 extends Activity

因此,在我的新班级MyActivity1中,我尝试运行我的旧班级MyActivity:

So in my new class MyActivity1 I try to run my old class MyActivity:

Intent myIntent = new Intent(MyActivity.this, MyActivity.class);
startActivity(myIntent);

但是出现以下编译错误: MyActivity不是封闭类

But I got the following compilation error: MyActivity is not an enclosing class

清单如下

<activity android:name=".MyActivity1"
    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=".MyActivity"/>

为什么会出现此错误?

推荐答案

尝试一下

  Intent myIntent = new Intent(MyActivity1.this, MyActivity.class);
  startActivity(myIntent);

新的Intent需要当前活动的上下文(第一个参数)和要初始化的类(第二个参数).

The new Intent requires the current activity's context (first param) and the class which you want initializate (second param).

这篇关于为什么会出现错误"MyActivity不是封闭的类?"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 23:59