我正在爲我的學校製作登錄屏幕Android Studio中的一個級別項目,但每當我嘗試運行代碼時,模擬器都會運行,然後當應用程序嘗試打開,它崩潰。無法在Android Studio中獲取登錄屏幕
,我使用的代碼是:
public class Login extends AppCompatActivity {
private static EditText username;
private static EditText password;
private static TextView attempts;
private static Button login;
int attempt_counter = 5;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
LoginButton();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
public void LoginButton() {
Button Login;
{
username = (EditText) findViewById(R.id.editText_user);
password = (EditText) findViewById(R.id.editText_password);
attempts = (TextView) findViewById(R.id.textView_attempt_Count);
Login = (Button) findViewById(R.id.button_login);
attempts.setText(Integer.toString(attempt_counter));
login.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
if (username.getText().toString().equals("user1") &&
password.getText().toString().equals("password")) {
Toast.makeText(Login.this, "Username and Password are correct",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent("com.example.andrew.rewardsandsanctions.User");
startActivity(intent);
} else {
Toast.makeText(Login.this, "Username and Password are not correct",
Toast.LENGTH_SHORT).show();
attempt_counter--;
attempts.setText(Integer.toString(attempt_counter));
if (attempt_counter == 0) {
login.setEnabled(false);
}
}
}
})
;
}
}
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
public Action getIndexApiAction() {
Thing object = new Thing.Builder()
.setName("Login Page") // TODO: Define a title for the content shown.
// TODO: Make sure this auto-generated URL is correct.
.setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
.build();
return new Action.Builder(Action.TYPE_VIEW)
.setObject(object)
.setActionStatus(Action.STATUS_TYPE_COMPLETED)
.build();
}
@Override
public void onStart() {
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
AppIndex.AppIndexApi.start(client, getIndexApiAction());
}
@Override
public void onStop() {
super.onStop();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
AppIndex.AppIndexApi.end(client, getIndexApiAction());
client.disconnect();
}
}
所出現的錯誤是:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.andrew.rewardsandsanctions, PID: 2925
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.andrew.rewardsandsanctions/com.example.andrew.rewardsandsanctions.Login}: 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.andrew.rewardsandsanctions.Login.LoginButton(Login.java:54)
at com.example.andrew.rewardsandsanctions.Login.onCreate(Login.java:37)
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)
是否錯誤「的java.lang NullPointerException異常:試圖調用虛擬方法無效android.widget .Button.setOnClickListener(android.view.View $ OnClickListener)''揭示了發生的事情? – max630