gpt4 book ai didi

java - 不是封闭类错误 Android Studio

转载 作者:IT老高 更新时间:2023-10-28 20:32:34 29 4
gpt4 key购买 nike

我是 android 开发的新手,对 Java 没有深入的了解。我被一个问题困扰了很长时间。我正在尝试在单击按钮时打开一个新 Activity 。但我收到一个错误 error: not an enclosure class: Katra_home

这是 MainActivity.java 的代码

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn=(Button)findViewById(R.id.bhawan1);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(Katra_home.this, Katra_home.class);
Katra_home.this.startActivity(myIntent);
}
});

这是 Katra_home.java 的代码

public class Katra_home extends BaseActivity {

protected static final float MAX_TEXT_SCALE_DELTA = 0.3f;

private ViewPager mPager;
private NavigationAdapter mPagerAdapter;
private SlidingTabLayout mSlidingTabLayout;
private int mFlexibleSpaceHeight;
private int mTabHeight;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.katra_home);

ActionBar ab = getSupportActionBar();
if (ab != null) {
ab.setDisplayHomeAsUpEnabled(true);
ab.setHomeButtonEnabled(true);
}

虽然我在 stackoverflow 上看到了很多答案,但我无法理解它们,因为我是 android 开发的新手。所以我想问一下我需要在我的代码中进行哪些更改才能使其正常工作。

最佳答案

应该是

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

您必须使用现有的 Activity 上下文来启动新 Activity ,新 Activity 尚未创建,您不能使用其上下文或对其调用方法。

not an enclosure class 由于您使用了 this 关键字而引发错误。 this 是对当前对象的引用——正在调用其方法或构造函数的对象。使用 this 您只能从实例方法或构造函数中引用当前对象的任何成员。

Katra_home.this 是无效的构造

关于java - 不是封闭类错误 Android Studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31104476/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com