-4

I am developing very simple sign up application. I am also trying to validate the password(i.e. length is less than 8)..but the I am getting error "unfortunately,your app has stopped working" after running the app. please help me out. I am using seterror method to display the error

java file: package com.example.dell.practice;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
public void onbuttonclick(View v)
{
    EditText et=(EditText) findViewById(R.id.atpassword);

    if(et.getText().toString().trim().length()<8)
        et.setError("invalid password");
    else if(v.getId()==R.id.bsignup)
    {
        Intent i=new Intent(MainActivity.this,aftersignup.class);
        startActivity(i);
    }
}
}

and xml code is:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="344dp"
    android:layout_height="495dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:orientation="vertical"
    tools:layout_editor_absoluteY="8dp"
    tools:layout_editor_absoluteX="8dp">



    <TextView
        android:id="@+id/tvusername"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Username"
        android:textSize="16sp"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp" />

    <EditText
        android:id="@+id/etname"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"/>
    <TextView
        android:id="@+id/tvpassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="password"
        android:textSize="16sp"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp" />

    <EditText
        android:id="@+id/etpassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/blogin"
        android:layout_width="75dp"
        android:layout_height="65dp"
        android:layout_gravity="center"
        android:textStyle="bold"
        android:text="LOGIN" />

    <Button
        android:id="@+id/bsignup"
        android:layout_width="100dp"
        android:layout_height="65dp"
        android:layout_gravity="center"
        android:onClick="onbuttonclick"
        android:textStyle="bold"
        android:text="SIGN UP" />
</LinearLayout>

</android.support.constraint.ConstraintLayout>
zeekhuge
  • 1,594
  • 1
  • 13
  • 23

4 Answers4

1

As you are having etpassword in the xml, you need to refer with the same name in the java file also.
Change the below line,

EditText et=(EditText) findViewById(R.id.atpassword);

to

EditText et=(EditText) findViewById(R.id.etpassword);


This should resolve your error of app crashing.

Avid
  • 171
  • 16
0

Just remove if else condition and replace it with else condition only

 if(et.getText().toString().trim().length()<8){
    et.setError("invalid password");
}
else 
{
    Intent i=new Intent(MainActivity.this,aftersignup.class);
    startActivity(i);
}

Also initialize your edittext in onCreate and made a class level variable for it

Although you might need to work on how to write effective code as beginner :)

MRX
  • 1,400
  • 3
  • 12
  • 32
0
            import android.content.Intent;
            import android.support.v7.app.AppCompatActivity 
            import android.os.Bundle;
            import android.view.View;
            import android.widget.EditText;

            public class MainActivity extends AppCompatActivity  implements View.OnClickListener;{

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


                EditText et=(EditText) findViewById(R.id.etpassword);
                Button btn=(Button)findViewById(R.id.blogin);
                Button btn_signup=(Button)findViewById(R.id.bsignup)
                btn_signup.setOnClickListener(this);
                btn.setOnClickListener(this);

            }
             @Override
                public void onClick(View v) {
                    switch(v.getId()){
                         case R.id.blogin:
                            if(et.getText().toString().trim().length()<8){
                    et.setError("invalid password");
                }else{
        //do Login procedure
        }


             break;
            case R.id.bsignup:
             Intent i=new Intent(MainActivity.this,aftersignup.class);
                    startActivity(i);
            break;
         }
      }

}
Shashwat Gupta
  • 876
  • 9
  • 22
  • 2
    Please take some time to 1) Type a well formatted answer. 2) Make code that compiles (implementing an import does not compile). 3) write code that works (You don't set a click listener on the button) – 0xDEADC0DE Aug 30 '17 at 10:55
0

Do this

    btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                       if(et.getText().toString().trim().length()<8){
                        et.setError("invalid password");
                       }else{}
                   }
   });
Vijay Chaudhary
  • 228
  • 2
  • 12