Java 不是封闭类错误 Android Studio
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31104476/
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
Not an enclosing class error Android Studio
提问by Ahmed Raza
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.
我是 android 开发的新手,对 Java 没有深入的了解。我被一个问题困住了很长时间。我正在尝试在单击按钮时打开一个新活动。但我收到一个错误:不是封闭类: Katra_home。
Here is the code for MainActivity.java
这是 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);
}
});
And this is the code for Katra_home.java
这是 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);
}
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.
虽然我在 stackoverflow 上看到了很多答案,但我无法理解它们,因为我是 android 开发的新手。所以我想问一下我需要在我的代码中进行哪些更改才能使其工作。
采纳答案by Dalija Prasnikar
It should be
它应该是
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.
您必须使用现有活动上下文来启动新活动,新活动尚未创建,您不能使用其上下文或对其调用方法。
not an enclosing classerror 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.
由于您使用了this
关键字,因此不会引发封闭类错误。this
是对当前对象的引用——正在调用其方法或构造函数的对象。与this
只可以参照当前对象的任何成员从一个实例方法或构造内。
Katra_home.this
is invalid construct
Katra_home.this
是无效构造
回答by Max77
replace code in onClick() method with this:
用以下代码替换 onClick() 方法中的代码:
Intent myIntent = new Intent(this, Katra_home.class);
startActivity(myIntent);
回答by Abhilash
Intent myIntent = new Intent(MainActivity.this, Katra_home.class);
startActivity(myIntent);
This Should the perfect one :)
这应该是完美的:)
回答by venu46
you are calling the context of not existing activity...so just replace your code in onClick(View v) as Intent intent=new Intent(this,Katra_home.class); startActivity(intent); it will definitely works....
您正在调用不存在的活动的上下文...所以只需将 onClick(View v) 中的代码替换为 Intent intent=new Intent(this,Katra_home.class); 开始活动(意图);它肯定会起作用......
回答by Avinash
String user_email = email.getText().toString().trim();
firebaseAuth
.createUserWithEmailAndPassword(user_email,user_password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()) {
Toast.makeText(RegistraionActivity.this, "Registration sucessful", Toast.LENGTH_SHORT).show();
startActivities(new Intent(RegistraionActivity.this,MainActivity.class));
}else{
Toast.makeText(RegistraionActivity.this, "Registration failed", Toast.LENGTH_SHORT).show();
}
}
});
回答by Ganesh MB
startActivity(new Intent(this, Katra_home.class));
try this one it will be work
试试这个它会起作用