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.