android 错误记录:
Attempt to invoke virtual method ‘void android.view.View.setVisibility(int)’ on a null object reference site:blog.csdn.net

场景:
布局中 最大的父布局设置的是不可见
代码中

View  a = view.findViewById(R.id.ll_vv);//R.id.ll_vv这个是最大的父布局
        a.setVisibility(View.VISIBLE);//报这个错误

解决方法

public class ContentMainTwo extends Fragment {
    private TextView tv_title_specific;
    private TextView tv_content_specific;
    private View view;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.content_frag_two,container,false);
        view = v;
        return v;
    }

    public void refresh(String title,String content){
        /*View a = view.findViewById(R.id.ll_vv);
        a.setVisibility(View.VISIBLE);*/
        view.setVisibility(View.VISIBLE);//方法1,不要View a
        tv_title_specific = view.findViewById(R.id.tv_title_specific);
        tv_content_specific = view.findViewById(R.id.tv_content_specific);

        tv_title_specific.setText(title);
        tv_content_specific.setText(content);
    }
}

方法2:让R.id.ll_vv这个布局变成不是最大的布局(外面加个Relativelayout之类的,设置此时ll_vv为可见或者不可见),就可以用View a啥啥啥的

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐