2016-02-02 81 views
-1

運行代碼時出現錯誤。我正在創建一個登錄頁面並註冊頁面。我無法確定哪些錯誤。關於登錄和註冊活動

public class MainActivity extends ActionBarActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_login); 


     Intent intent = new Intent(this, LoginActivity.class); 
     startActivity(intent); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

其中用戶輸入用戶名和密碼

public class LoginActivity extends AppCompatActivity { 
    private static final String TAG = "LoginActivity"; 
    private static final int REQUEST_SIGNUP = 0; 


    EditText _emailText; 
    EditText _passwordText; 
    Button _loginButton; 

    TextView _signupLink; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     //ButterKnife.bind(this); 

     _loginButton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       login(); 
      } 
     }); 

     _signupLink.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // Start the Signup activity 
       Intent intent = new Intent(getApplicationContext(), SignupActivity.class); 
       startActivityForResult(intent, REQUEST_SIGNUP); 
      } 
     }); 
    } 

    public void login() { 
     Log.d(TAG, "Login"); 

     if (!validate()) { 
      onLoginFailed(); 
      return; 
     } 

     _loginButton.setEnabled(false); 

     final ProgressDialog progressDialog = new ProgressDialog(LoginActivity.this, 
       R.style.AppTheme_Dark_Dialog); 
     progressDialog.setIndeterminate(true); 
     progressDialog.setMessage("Authenticating..."); 
     progressDialog.show(); 

     String email = _emailText.getText().toString(); 
     String password = _passwordText.getText().toString(); 

     // TODO: Implement your own authentication logic here. 

     new android.os.Handler().postDelayed(
       new Runnable() { 
        public void run() { 
         // On complete call either onLoginSuccess or onLoginFailed 
         onLoginSuccess(); 
         // onLoginFailed(); 
         progressDialog.dismiss(); 
        } 
       }, 3000); 
    } 


    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == REQUEST_SIGNUP) { 
      if (resultCode == RESULT_OK) { 

       // TODO: Implement successful signup logic here 
       // By default we just finish the Activity and log them in automatically 
       this.finish(); 
      } 
     } 
    } 

    @Override 
    public void onBackPressed() { 
     // Disable going back to the MainActivity 
     moveTaskToBack(true); 
    } 

    public void onLoginSuccess() { 
     _loginButton.setEnabled(true); 
     finish(); 
    } 

    public void onLoginFailed() { 
     Toast.makeText(getBaseContext(), "Login failed", Toast.LENGTH_LONG).show(); 

     _loginButton.setEnabled(true); 
    } 

    public boolean validate() { 
     boolean valid = true; 

     String email = _emailText.getText().toString(); 
     String password = _passwordText.getText().toString(); 

     if (email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) { 
      _emailText.setError("enter a valid email address"); 
      valid = false; 
     } else { 
      _emailText.setError(null); 
     } 

     if (password.isEmpty() || password.length() < 4 || password.length() > 10) { 
      _passwordText.setError("between 4 and 10 alphanumeric characters"); 
      valid = false; 
     } else { 
      _passwordText.setError(null); 
     } 

     return valid; 
    } 
} 

在註冊活動

public class SignupActivity extends AppCompatActivity { 
    private static final String TAG = "SignupActivity"; 


    EditText _nameText; 
    EditText _emailText; 
    EditText _passwordText; 
    Button _signupButton; 
    TextView _loginLink; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.acitivity_signup); 
     //ButterKnife.bind(this); 

     _signupButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       signup(); 
      } 
     }); 

     _loginLink.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // Finish the registration screen and return to the Login activity 
       finish(); 
      } 
     }); 
    } 

    public void signup() { 
     Log.d(TAG, "Signup"); 

     if (!validate()) { 
      onSignupFailed(); 
      return; 
     } 

     _signupButton.setEnabled(false); 

     final ProgressDialog progressDialog = new ProgressDialog(SignupActivity.this, 
       R.style.AppTheme_Dark_Dialog); 
     progressDialog.setIndeterminate(true); 
     progressDialog.setMessage("Creating Account..."); 
     progressDialog.show(); 

     String name = _nameText.getText().toString(); 
     String email = _emailText.getText().toString(); 
     String password = _passwordText.getText().toString(); 

     // TODO: Implement your own signup logic here. 

     new android.os.Handler().postDelayed(
       new Runnable() { 
        public void run() { 
         // On complete call either onSignupSuccess or onSignupFailed 
         // depending on success 
         onSignupSuccess(); 
         // onSignupFailed(); 
         progressDialog.dismiss(); 
        } 
       }, 3000); 
    } 


    public void onSignupSuccess() { 
     _signupButton.setEnabled(true); 
     setResult(RESULT_OK, null); 
     finish(); 
    } 

    public void onSignupFailed() { 
     Toast.makeText(getBaseContext(), "Login failed", Toast.LENGTH_LONG).show(); 

     _signupButton.setEnabled(true); 
    } 

    public boolean validate() { 
     boolean valid = true; 

     String name = _nameText.getText().toString(); 
     String email = _emailText.getText().toString(); 
     String password = _passwordText.getText().toString(); 

     if (name.isEmpty() || name.length() < 3) { 
      _nameText.setError("at least 3 characters"); 
      valid = false; 
     } else { 
      _nameText.setError(null); 
     } 

     if (email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) { 
      _emailText.setError("enter a valid email address"); 
      valid = false; 
     } else { 
      _emailText.setError(null); 
     } 

     if (password.isEmpty() || password.length() < 4 || password.length() > 10) { 
      _passwordText.setError("between 4 and 10 alphanumeric characters"); 
      valid = false; 
     } else { 
      _passwordText.setError(null); 
     } 

     return valid; 
    } 
} 

和日誌的登錄活動是

FATAL EXCEPTION: main 
                         Process: com.example.bhardwaj.logintrialtrans, PID: 21617 
                         java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bhardwaj.logintrialtrans/com.example.bhardwaj.logintrialtrans.LoginActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference 
                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 
                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                          at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                          at android.os.Handler.dispatchMessage(Handler.java:102) 
                          at android.os.Looper.loop(Looper.java:148) 
                          at android.app.ActivityThread.main(ActivityThread.java:5417) 
                          at java.lang.reflect.Method.invoke(Native Method) 
                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                         Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference 
                          at com.example.bhardwaj.logintrialtrans.LoginActivity.onCreate(LoginActivity.java:35) 
                          at android.app.Activity.performCreate(Activity.java:6237) 
                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  
                          at android.app.ActivityThread.-wrap11(ActivityThread.java)  
                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  
                          at android.os.Handler.dispatchMessage(Handler.java:102)  
                          at android.os.Looper.loop(Looper.java:148)  
                          at android.app.ActivityThread.main(ActivityThread.java:5417)  
                          at java.lang.reflect.Method.invoke(Native Method)  
                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

+0

可能重複[什麼是空指針異常,以及如何解決它?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how -do-i-fix-it) – Selvin

回答

2

您尚未初始化按鈕LoginActivity

onCreateLoginActivity

_signupLink = (Button) findViewById(R.id.signuplink); // use your id 
_loginButton = (Button) findViewById(R.id.loginbutton); // use your id 

做這個調用setContentView()

注後:我猜你正在使用錯誤的XML MainActivityLoginActivity

MainAcitivity你有setContentView(R.layout.activity_login);LoginActivity你有setContentView(R.layout.activity_main);。只是根據這些xmls的名稱說法

+0

感謝和抱歉,跛腳問題 – anupriya

+0

@anupriya:不要擔心。與我們大多數人發生。 – Rohit5k2

0

您需要使用findViewById(R.id.YOUR_BUTTON_ID)獲取對該按鈕的引用,然後再嘗試爲其設置onClickListener。目前,您只是在代碼中創建按鈕對象,但不會將其鏈接到佈局中的按鈕。