2017-03-27 6 views
0

私は、コードをビルドするときに、アプリケーションが非常にうまく動作しますが、後で試行カウンタに3の値を割り当てることはありませんが、私はYouTubeから簡単なログインアプリを作ることを学んでいました。コードでは、レイアウトのみが表示され、ログインが機能しません。手伝ってくれませんか?他のファイルが必要な場合はコメントに書き込んでください。後でアップロードします。ありがとう。単純なログインアプリを実行する問題

package com.shubham.splashscreenemulation; 

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; 
import android.widget.Toast; 


public class UserLogin extends AppCompatActivity { 

private static EditText username; 
private static EditText password; 
private static TextView attempts_left; 
private static Button login_btn; 
int attempt_counter = 3; 


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

public void LoginButton(){ 

    username = (EditText)findViewById(R.id.editText_user); 
    password = (EditText)findViewById(R.id.editText_password); 
    attempts_left = (TextView)findViewById(R.id.textView_attempt_count); 
    login_btn = (Button)findViewById(R.id.button_login); 

    attempts_left.setText(Integer.toString(attempt_counter)); 

    login_btn.setOnClickListener(
      new View.OnClickListener(){ 

       @Override 
       public void onClick(View v){ 

        if(username.getText().toString().equals("Shubh") && password.getText().toString().equals("adidev")) 
        { 
         Toast.makeText(UserLogin.this, "Login credentials are correct!", Toast.LENGTH_SHORT).show(); 
         Intent intent = new Intent("com.shubham.splashscreenemulation.SplashScreen"); 
         startActivity(intent); 
        } 

        else 
        { 
         Toast.makeText(UserLogin.this, "Login credentials are not correct!", Toast.LENGTH_SHORT).show(); 
         attempt_counter--; 
         attempts_left.setText(Integer.toString(attempt_counter)); 
         if(attempt_counter == 0) 
         { 
          login_btn.setEnabled(false); 
         } 
        } 
       } 


      } 

    ); 
} 


} 

答えて

0

アクティビティでLoginButton()関数を呼び出さないでください。

ここでいくつかのヒント:関数名はloginButton(camelCase)のように小文字で始まり、クラス名はAppCompatActivityのような大文字で始まります。このヒントは、コードを読みやすくするためのものです。なぜその関数でViewsを静的と宣言していますか?

package com.shubham.splashscreenemulation; 

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; 
import android.widget.Toast; 


public class UserLogin extends AppCompatActivity { 

private static EditText username; 
private static EditText password; 
private static TextView attempts_left; 
private static Button login_btn; 
int attempt_counter = 3; 


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

} 

public void LoginButton(){ 

    username = (EditText)findViewById(R.id.editText_user); 
    password = (EditText)findViewById(R.id.editText_password); 
    attempts_left = (TextView)findViewById(R.id.textView_attempt_count); 
    login_btn = (Button)findViewById(R.id.button_login); 

    attempts_left.setText(Integer.toString(attempt_counter)); 

    login_btn.setOnClickListener(
      new View.OnClickListener(){ 

       @Override 
       public void onClick(View v){ 

        if(username.getText().toString().equals("Shubh") && password.getText().toString().equals("adidev")) 
        { 
         Toast.makeText(UserLogin.this, "Login credentials are correct!", Toast.LENGTH_SHORT).show(); 
         Intent intent = new Intent("com.shubham.splashscreenemulation.SplashScreen"); 
         startActivity(intent); 
        } 

        else 
        { 
         Toast.makeText(UserLogin.this, "Login credentials are not correct!", Toast.LENGTH_SHORT).show(); 
         attempt_counter--; 
         attempts_left.setText(Integer.toString(attempt_counter)); 
         if(attempt_counter == 0) 
         { 
          login_btn.setEnabled(false); 
         } 
        } 
       } 


      } 

    ); 
} 
+0

にあなたのsetTextを変更することができます

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_user_login); username = (EditText)findViewById(R.id.editText_user); password = (EditText)findViewById(R.id.editText_password); attempts_left = (TextView)findViewById(R.id.textView_attempt_count); login_btn = (Button)findViewById(R.id.button_login); attempts_left.setText(Integer.toString(attempt_counter)); login_btn.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ if(username.getText().toString().equals("Shubh") && password.getText().toString().equals("adidev")) { Toast.makeText(UserLogin.this, "Login credentials are correct!", Toast.LENGTH_SHORT).show(); Intent intent = new Intent("com.shubham.splashscreenemulation.SplashScreen"); startActivity(intent); } else { Toast.makeText(UserLogin.this, "Login credentials are not correct!", Toast.LENGTH_SHORT).show(); attempt_counter--; attempts_left.setText(Integer.toString(attempt_counter)); if(attempt_counter == 0) { login_btn.setEnabled(false); } } } }); } 

uは – ShubhGOYAL5

+0

を詳しく説明することができ、それはあなたの最初のAndroidプロジェクトのですか?もしそうなら、あなたはそれに入るのを手伝ってくれるいくつかのコース(たとえば、Udacityは無料で良い)をたどるべきです。 – sgrumo

+0

とにかくありがとうございます。 – ShubhGOYAL5

0

あなたはsetContentViewLoginButton()を追加したり、LoginButton方法を取り外し、内部のonCreateすべてを移動することができます。あなたも、このattempts_left.setText(attempt_counter+"");

+0

did not work ... – ShubhGOYAL5

+0

チェックagain .... .... –

+1

どうもありがとうございました。 – ShubhGOYAL5