2016-10-21 53 views
0

我正在爲我的學校製作登錄屏幕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)  
+0

是否錯誤「的java.lang NullPointerException異常:試圖調用虛擬方法無效android.widget .Button.setOnClickListener(android.view.View $ OnClickListener)''揭示了發生的事情? – max630

回答

1

你的堆棧跟蹤表明問題是什麼:

Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference 

兩個的東西。首先,確保你正確地處理你的按鈕。

Login = (Button) findViewById(R.id.button_login);大概應該是這樣的:

login = (Button) findViewById(R.id.button_login);

其次,確保與ID button_login按鈕佈局activity_login內部存在。

一旦你確認你的按鈕是非空的,那麼你可以設置你的onClickListener

希望有幫助!

1

檢查你的代碼,你必須使用「登錄」變量和您的分配監聽器「登錄」:

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); 
         } 
        } 
       } 
      }) 
    ; 
+0

感謝您的幫助,我已經做出了改變,但現在我得到2個錯誤: – Andy

+0

找不到符號類登錄 – Andy

+0

再次編輯您的問題,並註釋出現錯誤的行 – siddhesh