setVisibility(int)在android中引发空点异常
当我试图在我的setVisibility(int)在android中引发空点异常,android,android-layout,android-studio,android-fragments,android-relativelayout,Android,Android Layout,Android Studio,Android Fragments,Android Relativelayout,当我试图在我的片段中隐藏RelativeLayout时,我得到一个异常,该异常RelativeLayout.setVisibility抛出空点异常。 这是我的片段代码: public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.l
片段中隐藏RelativeLayout
时,我得到一个异常
,该异常RelativeLayout.setVisibility
抛出空点异常
。
这是我的片段
代码:
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;
}
及
Android Studio似乎无法处理太多嵌套布局。因此,请删除父RelativeLayout,并创建一个新的Relative作为父布局的子级,并将所有内容放入新的Relative布局中。这就解决了问题
或
product\u description\u zoom\u image
版面没有id为的版面layout\u 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();
}
请尝试此代码
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>
code of product\u description\u zoom\u image.xml?是否与您在问题中添加的内容相同?您确定您使用的碎片布局正确吗?可能您的布局参考不正确,请再次检查。move layout.setVisibility(View.GONE);这一行到onviewcreated Method可能与OP的代码有什么不同?唯一的区别是您删除了一些只由Lint使用的注释,因此在运行时它不会以任何方式使用。请检查您的布局文件是否包含id为的相同布局layout\u fix
您在说什么?你的答案是错误的,你添加的一行描述应该是对OP的问题的评论,而不是答案,因为你基本上只是猜测这可能是问题。检查我更新了我的答案,它将修复你的问题@DarwindIt这不是我的问题,我只是对问题和答案进行评论。
<?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>