【问题标题】:Not an enclosing class error Android Studio不是封闭类错误 Android Studio
【发布时间】:2015-09-15 06:07:15
【问题描述】:

我是 android 开发的新手,对 Java 没有深入的了解。我被一个问题困扰了很长时间。我正在尝试在单击按钮时打开一个新活动。但我收到一个错误 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 开发的新手。所以我想问一下我需要在我的代码中进行哪些更改才能使其正常工作。

【问题讨论】:

    标签: java android android-intent android-activity android-studio


    【解决方案1】:

    应该是

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

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

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

    Katra_home.this 是无效构造

    【讨论】:

      【解决方案2】:
      Intent myIntent = new Intent(MainActivity.this, Katra_home.class);
      startActivity(myIntent);
      

      这应该是完美的一个:)

      【讨论】:

        【解决方案3】:

        您正在调用不存在活动的上下文...所以只需将 onClick(View v) 中的代码替换为

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

        它肯定会起作用....

        【讨论】:

          【解决方案4】:
          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();
                      }
                  }
              });
          

          【讨论】:

          • 请添加解释为什么/如何此代码将解决问题。 ty
          【解决方案5】:

          将 onClick() 方法中的代码替换为:

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

          【讨论】:

            【解决方案6】:
            startActivity(new Intent(this, Katra_home.class));
            

            试试这个就行了

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2016-01-15
              • 2020-05-31
              • 1970-01-01
              • 1970-01-01
              • 2016-04-04
              • 1970-01-01
              相关资源
              最近更新 更多