美好的一天,我在尝试访问包含布局的RelativeLayout视图组时遇到NullPointerexception.在下面的代码中,它被命名为rootlayout.我有这个
date_layout.xml:
并在main_layout.xml中:
"
我的代码是:
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_layout); View view = findViewById(R.id.datelayout_id); rootlayout = (RelativeLayout)view.findViewById(R.id.my_layoutroot_id); rootlayout.setOnclickListener(this); // am getting the NullPointerException here
任何想法我可能做错了什么?非常感谢!
您正在main_layout.xml中分配新的ID:
rootlayout = (RelativeLayout)view.findViewById(R.id.datelayout_id);
应该修复NPE.