-2

I have getting error while calling intent from Fragment. Following are the log error of the same. Below are the pieces of code from the same activities. The application automatically gets off as i click on the Button for Login. Please help me out. Thank you in advance.

Log Report

Process: com.example.lenovo.skanda, PID: 7978
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
        at com.example.lenovo.skanda.AdminFragment$1.onClick(AdminFragment.java:43)

HomeFragment.java

  public class AdminFragment extends Fragment implements View.OnClickListener{

    View viewroot;
    View v1;
    private EditText Name;
    private EditText Password;
    static Button Login;
    public static Button myLog;
    private int Counter = 5;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        viewroot = inflater.inflate(R.layout.fragment_admin,container,false);

        Name = (EditText) getActivity().findViewById(R.id.editText);
        Password = (EditText) getActivity().findViewById(R.id.editText2);
        Login = (Button) getActivity().findViewById(R.id.button2);
        myLog = (Button) viewroot.findViewById(R.id.button2);
        myLog.setOnClickListener(this);


        myLog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                validate(Name.getText().toString(), Password.getText().toString());
            }
        });
        return viewroot;
    }



    private void validate (String userName, String userPassword){
        if((userName.equals("Admin")) &&  (userPassword.equals("123"))){
            Intent intent = new Intent(AdminFragment.this.getActivity(), Login.class);
            startActivity(intent); //Login is the second activity
        }
        else {

            Counter --;
            Toast.makeText(getActivity(), "No. of Attempts Left for Login" + String.valueOf(Counter), Toast.LENGTH_SHORT).show();

            if (Counter == 0){
                Login.setEnabled(false);
                Toast.makeText(getActivity(), "For Enable the Login Please Contact..!!", Toast.LENGTH_SHORT).show();

            }
        }
    }

    @Override
    public void onClick(View v) {
        validate(Name.getText().toString(),Password.getText().toString());
    }
}

fragment_admin.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@android:color/background_dark">

    <com.airbnb.lottie.LottieAnimationView
        android:id="@+id/animation_view"
        android:layout_width="185dp"
        android:layout_height="155dp"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center"
        app:lottie_autoPlay="true"
        app:lottie_colorFilter="#FFBC00"
        app:lottie_fileName="user.json"
        app:lottie_loop="true" />


    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="17dp"
        android:text="admin login"
        android:textAllCaps="true"
        android:textColor="#FFBA24"
        android:textSize="25dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/editText"

        android:layout_alignStart="@+id/editText2"
        android:layout_marginBottom="-247dp"
        android:text="Enter your username"
        android:textAllCaps="true"
        android:textColor="#B7BE5D"
        android:textSize="15dp" />


    <EditText
        android:id="@+id/editText2"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="183dp"
        android:ems="10"
        android:fontFamily="monospace"
        android:hint="Password"
        android:inputType="textPassword"
        android:textColorHint="#FFFFFF" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/editText2"

        android:layout_alignStart="@+id/editText2"
        android:text="Enter your password"
        android:textAllCaps="true"
        android:textColor="#B7BE5D"
        android:textSize="15dp" />


    <EditText
        android:id="@+id/editText"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="247dp"
        android:ems="10"
        android:fontFamily="monospace"
        android:hint="Username"
        android:inputType="textPersonName"
        android:textColor="#FFFFFF"
        android:textColorHint="#FFFFFF" />

    <EditText
        android:id="@+id/Editinfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="62dp"
        android:text="No. of attempts left : "
        android:textColor="#FFF"
        android:textSize="15dp"
        android:textStyle="italic" />

    <Button
        android:id="@+id/button2"
        android:layout_width="210dp"
        android:layout_height="45dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="111dp"
        android:background="@drawable/rounded_button"
        android:text="login"
        android:textAllCaps="true"
        android:textColor="#000"
        android:visibility="visible" />

    <Button
        android:id="@+id/button"
        style="@style/Widget.AppCompat.Button.Borderless.Colored"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="#19193E"
        android:text="LOG IN PROBLEM? CONTACT US."
        android:textColor="#FFF"
        android:onClick="onClick"
        android:visibility="visible" />
</RelativeLayout>
itz Prateek
  • 101
  • 1
  • 13
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Vucko Oct 26 '18 at 14:19

3 Answers3

1

You can't use getActivity().findViewById() like that.

Inside onCreateView(), you haven't yet returned the Fragment's View, meaning it isn't attached to the Activity. getActivity().findViewById() returns null because the Activity currently has no View with that ID. It only will after viewroot is returned, which is after you're invoking it.

Instead, you can do one of two things:

  1. Replace getActivity() with viewroot:

    Name = (EditText) viewroot.findViewById(R.id.editText);
    Password = (EditText) viewroot.findViewById(R.id.editText2);
    Login = (Button) viewroot.findViewById(R.id.button2);
    
  2. Move your code to onViewCreated() and keep using getActivity() (you should be using the passed view variable, however).

In general, you don't want to use getActivity().findViewById(). If you need to find a View in that Fragment, use getView().findViewById(). Neither of these methods will work until onCreateView() returns.


I also notice that both Login and myLog are the same button. You should assign to one variable and call on it when needed. There's no point in a second local variable.

You should also try following Java's syntax guidelines:

  • Class names use TitleCase
  • Variable and function names use camelCase
TheWanderer
  • 16,775
  • 6
  • 49
  • 63
0

it should be like this

viewroot = inflater.inflate(R.layout.fragment_admin,container,false);
Name = (EditText) viewroot.findViewById(R.id.editText);
        Password = (EditText) viewroot.findViewById(R.id.editText2);
        Login = (Button) viewroot.findViewById(R.id.button2);
        myLog = (Button) viewroot.findViewById(R.id.button2);
AhmetAcikalin
  • 328
  • 2
  • 7
0

You need to use viewroot instead of getActivity() in onCreateView().

Use the following code:

Name = (EditText) viewroot.findViewById(R.id.editText);
Password = (EditText) viewroot.findViewById(R.id.editText2);
Login = (Button) viewroot.findViewById(R.id.button2);