Why am I getting the error “MyActivity is not an e

2019-06-20 20:36发布

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

public class MyActivity extends AndroidApplication implements IActivityRequestHandler

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

So I added the following class as my launcher Activity:

public class MyActivity1 extends Activity

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

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

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

Manifest is as follows

<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"/>

Why am I getting this error?

1条回答
冷血范
2楼-- · 2019-06-20 21:13

Try with this

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

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

查看更多
登录 后发表回答