android - 'android.view.View android.widget.TextView.findViewById(int)' 在空对象引用上
问题描述
当我在 android studio 的片段中调用 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(...)
. 您必须在父视图中找到您之前已经膨胀的子视图,而不是在其内部
推荐阅读
- c++ - 为什么这个makefile不构建依赖关系,而只是在使用变量时
- r - 单击饼图部分,打开一个新网页 R Shiny Highcharter
- java - Java Spring Boot 构造函数方法依赖注入是如何工作的
- sas - 在SAS中用null替换字符串
- c# - 尝试获取 MS Graph API 的令牌时缺少用户的 ImmutableID(Windows 身份验证)
- node.js - Mongo db 从 aws 同步到本地
- python - git commit 无法使用“python was not found ...”警告
- loops - 如何使用循环来创建滞后变量?
- javascript - 如何在 react js 应用程序中插入 hubspot 表单?
- c++ - C/C++ recvmsg() 导致错误“检测到未对齐的 tcache 块”但 recv() 成功