'android.view.View android.widget.TextView.findViewById(int)'空对象引用



当我在安卓工作室中的一个片段中调用findViewById时,我收到了这个错误

"尝试在com.example.apptrail4.profile_frg.onCreateView((上的空对象引用上调用虚拟方法"android.view.view android.widget.TextView.findViewById(int(";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
F2F_fragment = inflater.inflate(R.layout.fragment_profile_frg, container, false);

String abc = "Hello sir";
nametxt = nametxt.findViewById(R.id.nameshow);
nametxt.setText(abc);

return F2F_fragment;
}

您应该使用F2F_fragment.findViewById(...)。你必须在父视图中找到子视图,你之前已经膨胀过,而不是在内部

最新更新