0
我一直在設計我的應用程序的登錄和註冊屏幕。更改我的主要活動中的操作欄設置後,我似乎無法登錄到應用程序。登錄和註冊屏幕加載完美。但後來它說:「不幸的是,____已經停止了」這是我得到的錯誤,我還包含了我的LoginActivity.java,因爲它說錯誤在哪裏。但我不知道要引用哪個其他文件,所以如果您需要另一個文件,請告訴我,我會更新它。謝謝!獲取致命異常:主錯誤
1493-1493/com.example.aalok.chirp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.aalok.chirp, PID: 1493
java.lang.NullPointerException
at com.example.aalok.chirp.LoginActivity$2.onClick(LoginActivity.java:58)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
package com.example.aalok.chirp;
import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.parse.LogInCallback;
import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseUser;
import com.parse.SignUpCallback;
public class LoginActivity extends Activity {
protected EditText mUsername;
protected EditText mPassword;
protected Button mLoginButton;
protected TextView mSignUpTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_login);
ActionBar actionBar = getActionBar();
actionBar.hide();
//Set up SignUp text
mSignUpTextView = (TextView) findViewById(R.id.signUpText);
mSignUpTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(LoginActivity.this, SignUpActivity.class);
startActivity(intent);
}
});
mUsername = (EditText) findViewById(R.id.usernameField);
mPassword = (EditText) findViewById(R.id.passwordField);
//Set up Login button
mLoginButton = (Button) findViewById(R.id.loginButton);
mLoginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String username = mUsername.getText().toString();
String password = mPassword.getText().toString();
username = username.trim();
password = password.trim();
if (username.isEmpty() || password.isEmpty()) {
AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
builder.setMessage(R.string.login_error_message)
.setTitle(R.string.login_error_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
} else {
//LOGIN
setProgressBarIndeterminateVisibility(true);
ParseUser.logInInBackground(username, password, new LogInCallback() {
@Override
public void done(ParseUser user, ParseException e) {
setProgressBarIndeterminateVisibility(false);
if (e == null) {
//SUCCESS!
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
builder.setMessage(e.getMessage())
.setTitle(R.string.login_error_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
}
});
}
}
});
}
}
記錄'mPassword'是否爲空。另外,檢查佈局中是否存在'R.id.passwordField'。 – stealthjong 2014-10-07 14:13:49
非常感謝!當我重構 – 2014-10-08 15:00:49
樣式沒問題時,我以某種方式刪除了passwordField id。那麼我把它變成一個答案。 – stealthjong 2014-10-09 06:12:24