在Android Studio中编译时“不是封闭类”错误

时间:2017-09-27 19:12:34

标签: java android android-studio android-intent

我试图在按钮点击时打开一个新活动,但我收到了这两个错误:

  1. not an enclosing class:Main Activity
  2. Execution failed for task ':app:compileDebugJavaWithJavac'
  3.   

    编译失败;请参阅编译器错误输出以获取详细信   这是MainActivity.java的代码

    package com.example.admin.viewsandevents;
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            final EditText etUSN=(EditText) findViewById(R.id.etUSN);
            final EditText etPassword=(EditText) findViewById(R.id.etPassword);
            final Button bLogin=(Button) findViewById(R.id.bLogin);
            final TextView registerLink =(TextView) findViewById(R.id.tvRegisterhere);
    
            registerLink.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent registerIntent = new Intent(MainActivity.this,RegisterActivity.class);
                    MainActivity.this.startActivity(registerIntent);
                }
            });
    
        }
    }
    

    这是RegisterActivity.java的代码

    package com.example.admin.viewsandevents;
    
    import android.content.Intent;
    import android.support.v7.app.AlertDialog;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    
    import com.android.volley.RequestQueue;
    import com.android.volley.Response;
    import com.android.volley.toolbox.Volley;
    
    import org.json.JSONException;
    import org.json.JSONObject;
    
    public class RegisterActivity extends AppCompatActivity
    {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_register);
            final EditText etAge=(EditText) findViewById(R.id.etAge);
            final EditText etName=(EditText) findViewById(R.id.etName);
            final EditText etUSN=(EditText) findViewById(R.id.etUSN);
            final EditText etPassword=(EditText) findViewById(R.id.etPassword);
            final Button bRegister=(Button) findViewById(R.id.bRegister);
         bRegister.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View view) {
            final String name=etName.getText().toString();
            final String username=etUSN.getText().toString();
            final String password=etPassword.getText().toString();
            final int age=Integer.parseInt(etAge.getText().toString());
    
            Response.Listener<String> responseListener =new Response.Listener<String>(){
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject jsonResponse =new JSONObject(response);
                        boolean success =jsonResponse.getBoolean("success");
    
                        if(success){
                            Intent myintent =new Intent(RegisterActivity.this, MainActivity.class);
                            RegisterActivity.this.startActivity(myintent);
                        }
                        else{
                            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                            builder.setMessage("Register Failed")
                                    .setNegativeButton("Retry",null)
                                    .create()
                                    .show();
                        }
    
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            };
            RegisterRequest registerRequest =new RegisterRequest(name,username,age,password, responseListener );
            RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
            queue.add(registerRequest);
        }
    });
    
        }
    }
    

    所以请帮我解决这个问题

1 个答案:

答案 0 :(得分:1)

问题出在RegisterActivity.java。您正尝试从中创建警告对话框,但您使用MainActivity.this作为上下文:

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("Register Failed")
       .setNegativeButton("Retry",null)
       .create()
       .show();

将其更改为使用RegisterActivity.this,如:

AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
builder.setMessage("Register Failed")
       .setNegativeButton("Retry",null)
       .create()
       .show();