-2

When I am trying to hide RelativeLayout in my Fragment I get an Exception that RelativeLayout.setVisibility throws null point Exception. Here is my Fragment code:

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.product_description_zoom_image,container,false);
    layout = view.findViewById(R.id.layout_fix);
    layout.setVisibility(View.GONE);
    return view;
}

and

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:id="@+id/layout_fix">
    <Button
        android:layout_width="190dp"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:background="@drawable/border"
        android:id="@+id/btnFavourite"
        android:text="@string/add_to_favourite"
        android:drawableLeft="@drawable/ic_favorite_border_black_24dp"/>
Onik
  • 19,396
  • 14
  • 68
  • 91
ansar abbas
  • 11
  • 1
  • 8

2 Answers2

0

Its seems like Android Studio cannot handle too many nested layouts. So please remove the parent RelativeLayout,and create a new Relative as child of parent layout and put everything in the new Relative layout. That solved it.

or

product_description_zoom_image layout has not any layout with id layout_fix

and your question in comment

Put this code in fragment in which you want to hide toolbar...

 @Override
public void onResume() {
    super.onResume();
    ((AppCompatActivity)getActivity()).getSupportActionBar().hide();
}
@Override
public void onStop() {
    super.onStop();
    ((AppCompatActivity)getActivity()).getSupportActionBar().show();
}
Sandeep Parish
  • 1,888
  • 2
  • 9
  • 20
0

Try this code..

public class HomeFragment extends Fragment {
RelativeLayout layout;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View view=inflater.inflate(R.layout.home_fragment,container,false);
    initView(view);
    return view;

}

private void initView(View view) {
    layout=view.findViewById(R.id.layout_fix);
    layout.setVisibility(View.GONE);
}

}

xml code like..

<?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"
android:id="@+id/layout_fix">

<Button
    android:layout_width="190dp"
    android:layout_height="50dp"
    android:text="Hellow"/>
</RelativeLayout>