【问题标题】:RelativeLayout.setVisibility(int) throws null point exception in androidRelativeLayout.setVisibility(int) 在android中抛出空点异常
【发布时间】:2018-08-09 12:26:02
【问题描述】:

当我试图在我的Fragment 中隐藏RelativeLayout 时,我得到一个ExceptionRelativeLayout.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"/>

【问题讨论】:

  • product_description_zoom_image.xml 的代码?是否与您在问题中添加的内容相同?
  • 你确定你使用了正确的片段布局
  • 可能您的布局参考不正确,请重新检查。
  • 移动 layout.setVisibility(View.GONE);这一行到 onviewcreated 方法

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


【解决方案1】:

试试这个代码..

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>

【讨论】:

    【解决方案2】:

    Android Studio 似乎无法处理太多的嵌套布局。所以请移除父级RelativeLayout,并创建一个新的Relative 作为父级布局的子级,并将所有内容放入新的Relative 布局中。这样就解决了。

    product_description_zoom_image layout 没有任何 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();
    }
    

    【讨论】:

    • 这与 OP 的代码有何不同?唯一的区别是您删除了一些仅由 Lint 使用的注释,因此它不会在运行时以任何方式使用。
    • 检查您的布局文件是否包含与 id layout_fix 相同的布局
    • 你在说什么?您的答案是错误的,您添加的单行描述应该是对 OP 问题的评论,而不是答案,因为您基本上只是在猜测这可能是问题所在。
    • 检查我更新了我的答案,它将解决你的问题@Darwind
    • 这不是我的问题,我只是评论问题和答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-31
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-02
    • 2012-09-09
    相关资源
    最近更新 更多