RelativeLayout.setVisibility(int)在Android中引发空点异常

时间:2018-08-09 12:26:03

标签: android android-layout android-studio android-fragments android-relativelayout

当我尝试将RelativeLayout隐藏在Fragment中时,我得到Exception抛出空点RelativeLayout.setVisibility的{​​{1}}。 这是我的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;
}

2 个答案:

答案 0 :(得分:0)

似乎Android Studio无法处理太多嵌套布局。因此,请删除父级RelativeLayout,并创建一个新的Relative作为父级布局的子级,然后将所有内容放入新的Relative布局中。解决了。​​

public static String upload(File file) throws IOException { Cloudinary cloudinary = new Cloudinary(CONFIG); Map<Object, Object> parameters = new HashMap<>(); parameters.put("public_id", "Bookstore/Authors/Images/vejder"); Map result = cloudinary.uploader().upload(file, parameters); return (String) result.get("url"); }  布局没有ID为product_description_zoom_image

的任何布局

以及您的评论问题

layout_fix

答案 1 :(得分:0)

尝试此代码。

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>