У меня проблема с неожиданным исключением 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;
}
}
Logcat:
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
fragment_weight.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>
также проверьте этот вопрос
Отредактировал и смотрю по вашей ссылке :)
Нашел решение в этот ответ: переименовать view в View
почтовый индекс
layout\fragment_weight.xml