android - inflater.inflate 中的 NPE
问题描述
我遇到了意外的 NullPointerExpception 问题
View view = inflater.inflate(R.layout.fragment_weight, container, false);
我想知道为什么那不起作用。我看了一个教程,一切都在我的代码中并且运行良好。这是我的整个代码:
public class AddWeightFragment extends Fragment {
Button button2;
TextView textView6;
MaterialCalendarView calendarView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_weight, container, false);
button2 = view.findViewById(R.id.button2);
textView6 = view.findViewById(R.id.textView6);
calendarView = view.findViewById(R.id.calendarView);
calendarView.setDateSelected(CalendarDay.today(), true);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
textView6.setText(calendarView.getSelectedDate().toString());
Toast.makeText(getContext(), calendarView.getSelectedDate().toString(), Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
日志猫:
android.view.InflateException: Binary XML file line #9: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
片段重量.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<view
android:id="@+id/calendarView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="147dp"
android:layout_marginLeft="147dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="209dp"
android:layout_marginRight="209dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:mcv_tileHeight="40dp"
app:mcv_tileWidth="50dp" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="209dp"
android:text="Button" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="-209dp"
android:layout_toEndOf="@+id/calendarView"
android:text="TextView" />
</RelativeLayout>
解决方案
推荐阅读
- android - 关于改进 LinkedIn 社交登录模块的问题
- python - 如何配置 tensorflow legacy/train.py model.cpk 输出间隔
- sql - 在 Access 上的 SQL 操作中按名称返回最后日期
- amazon-web-services - 通过 AWS ELB 的 MongooseIM SSL 连接
- php - 如何在 PHPExcel 中读取合并和具有背景颜色的单元格
- java - 在运行时在 log4j.properties 文件中传递 appender.file 值
- django - 为选择字段添加额外的选项标签 - Django
- xml - XSLT 1.0 - 取消文件创建
- php - 如何打印响应soap请求的单个xml值?
- sql - SQL server Rex 获取通配符中的字符串