0

I am trying to hide the Realtive Layout in the Fragment using setVisibility(VIEW.GONE) and after some action showing it.

I had refer the following link.
How to Hide the layout
what is null pointer exception(Didn't understood anything here that's why asking)

I am getting the NullPointerException

Please check my code and correct me

fragment_track_order.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/formLayout"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".TrackOrder">

    <LinearLayout
        android:id="@+id/registrationForm"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="32dp"
        android:orientation="vertical">

        <EditText/>

        <Button />

    </LinearLayout>


    <RelativeLayout
        android:id="@+id/order_summary_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/registrationForm">

        <TextView />

        <android.support.v7.widget.GridLayout
            android:id="@+id/order_details_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/trackOrderPage"
            android:layout_marginStart="10dp"
            android:paddingLeft="0dp"
            android:paddingTop="10dp">

            <TextView />

            <TextView />

            <TextView />

            <TextView />

            <TextView />

            <TextView />

            <TextView />

            <TextView />

            <TextView />

            <TextView />

            <TextView />

            <TextView />


        </android.support.v7.widget.GridLayout>


    </RelativeLayout>


</LinearLayout>

TrackOrder.java

/**
 * A simple {@link Fragment} subclass.
 */
public class TrackOrder extends Fragment {

    @BindView(R.id.track_id)
    EditText trackID;
    @BindView(R.id.trackButton)
    Button trackBtn;
    @BindView(R.id.order_summary_layout)
    RelativeLayout OrderSummaryLayout;
    @BindView(R.id.fname)
    TextView firstname;
    @BindView(R.id.lname)
    TextView lastname;
    @BindView(R.id.order_status)
    TextView OrderStatus;
    @BindView(R.id.photo_desc)
    TextView Photodesc;
    private DatabaseReference mDatabase;
    private FirebaseDatabase mCustomerDatabase;
    private String tID;
    private View view;

    public TrackOrder() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_track_order, container, false);
        setLayoutInvisible();
        ButterKnife.bind(this,view);


        trackBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isValid()){
                    //Perform the fetch operation
                    Toast.makeText(getActivity(), ""+tID, Toast.LENGTH_SHORT).show();
                    setLayoutVisible();
                }
                else{
                    Toast.makeText(getActivity(), "Field can't be empty", Toast.LENGTH_SHORT).show();
                }
            }
        });
        return view ;
    }

    private boolean isValid() {
        tID = trackID.getText().toString();
        if(!TextUtils.isEmpty(tID)){
            return true;
        }
        return false;
    }

    public void setLayoutInvisible() {
            if (OrderSummaryLayout.getVisibility() == View.VISIBLE) {
                OrderSummaryLayout.setVisibility(View.GONE);
            }

    }
    public void setLayoutVisible() {
        if (OrderSummaryLayout.getVisibility() == View.GONE) {
            OrderSummaryLayout.setVisibility(View.VISIBLE);
        }
    }

}

Stack Trace

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.lenovo.jdstudio, PID: 19063
                  java.lang.NullPointerException
                      at com.example.lenovo.jdstudio.TrackOrder.setLayoutInvisible(TrackOrder.java:85)
                      at com.example.lenovo.jdstudio.TrackOrder.onCreateView(TrackOrder.java:56)
                      at android.support.v4.app.Fragment.performCreateView(Fragment.java:2346)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1428)
                      at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1759)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1827)
                      at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
                      at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2596)
                      at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2383)
                      at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2338)
                      at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2245)
                      at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:703)
                      at android.os.Handler.handleCallback(Handler.java:808)
                      at android.os.Handler.dispatchMessage(Handler.java:103)
                      at android.os.Looper.loop(Looper.java:193)
                      at android.app.ActivityThread.main(ActivityThread.java:5333)
                      at java.lang.reflect.Method.invokeNative(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:515)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
                      at dalvik.system.NativeStart.main(Native Method)

Thanks in advance.

Aakash
  • 101
  • 2
  • 11
  • You need to bind your `View`s before you try to set the visibility on one. That is, switch the `setLayoutInvisible();` and `ButterKnife.bind(this,view);` lines in `onCreateView()`. – Mike M. Apr 17 '18 at 03:45
  • 1
    Thanks @Mike M. you saved my day sir – Aakash Apr 17 '18 at 03:50
  • as you marked the question dupllicate i have visited that question but didn't understand btw thanks for helping me again – Aakash Apr 17 '18 at 03:51
  • No problem. That duplicate is appropriate, because you're trying to call a method on a null reference, as you haven't assigned it a value yet. In this case, ButterKnife is handling the assignment, but the Exception is still happening for the same reason. If you really want, I'm sure I could find a duplicate for the same thing with ButterKnife, but I didn't figure it was needed, since I explained the fix in comments. – Mike M. Apr 17 '18 at 03:55
  • I am new to Android and making a project for my Last year studies. I m in learning mode that's why i haven't understood that answers.And thanks for pointing my mistakes and correcting me. – Aakash Apr 17 '18 at 04:00
  • You're welcome. I do usually try to leave a comment with a specific explanation whenever I mark a duplicate. Glad it helped. Cheers! – Mike M. Apr 17 '18 at 04:02
  • Mark it as a answer and i will correct it for sure bcoz this is not less than answer – Aakash Apr 17 '18 at 04:09

0 Answers0