Not an enclosing class error Android Studio

后端 未结 6 1186
孤街浪徒
孤街浪徒 2020-12-08 07:50

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. B

相关标签:
6条回答
  • 2020-12-08 07:56

    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 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 is invalid construct

    0 讨论(0)
  • 2020-12-08 07:56
    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();
                }
            }
        });
    
    0 讨论(0)
  • 2020-12-08 07:59

    replace code in onClick() method with this:

    Intent myIntent = new Intent(this, Katra_home.class);
    startActivity(myIntent);
    
    0 讨论(0)
  • 2020-12-08 08:05
    Intent myIntent = new Intent(MainActivity.this, Katra_home.class);
    startActivity(myIntent);
    

    This Should the perfect one :)

    0 讨论(0)
  • 2020-12-08 08:09

    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....

    0 讨论(0)
  • 2020-12-08 08:19
    startActivity(new Intent(this, Katra_home.class));
    

    try this one it will be work

    0 讨论(0)
提交回复
热议问题