不是封闭的类错误Android Studio [英] Not an enclosing class error Android Studio
问题描述
我是Android开发的新手,并且没有深入的Java知识。我长期坚持一个问题。我试图按下按钮打开一个新活动。但是我收到错误错误:不是封闭类:Katra_home 。
I am new in android development and do not have an in depth knowledge of Java. I am stuck on a problem for a long time. I am trying to open a new activity on button click. But I am getting an error that error: not an enclosing class: Katra_home.
以下是MainActivity.java的代码
Here is the code for 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的代码
And this is the code for 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开发的新手。所以我想问一下,我需要在代码中做些什么更改才能使其正常工作。
Though I have seen many answers on stackoverflow but I could not understand them as I am new in android development. So I would like to ask what changes do I need to make in my code to make it work.
推荐答案
应该是
Intent myIntent = new Intent(this, Katra_home.class);
startActivity(myIntent);
您必须使用现有活动上下文来开始新活动,尚未创建新活动,而您不能使用它的上下文或调用方法。
You have to use existing activity context to start new activity, new activity is not created yet, and you cannot use its context or call methods upon it.
因为你使用而抛出了一个封闭的类
错误这个关键字。 此
是对当前对象的引用 - 正在调用其方法或构造函数的对象。使用 this
,您只能从实例方法或构造函数中引用当前对象的任何成员。
not an enclosing class error is thrown because of your usage of this
keyword. this
is a reference to the current object — the object whose method or constructor is being called. With this
you can only refer to any member of the current object from within an instance method or a constructor.
Katra_home.this
无效构造
这篇关于不是封闭的类错误Android Studio的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!