当我试图隐藏RelativeLayout
在我的Fragment
我得到一个Exception
RelativeLayout.setVisibility
抛出零点Exception
。这是我的Fragment
代码:
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;
}
和
<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"/>
试试这个代码..
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代码
<?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>
看起来Android Studio无法处理太多嵌套布局。因此,请删除父RelativeLayout,并创建一个新的Relative作为父布局的子项,并将所有内容放在新的Relative布局中。这解决了它。
要么
product_description_zoom_image
布局没有id layout_fix
的任何布局
和你的评论中的问题
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();
}