充气机中的NPE。充气

时间:2018-10-04 21:48:43

标签: android nullpointerexception

我在

中遇到意外的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>

0 个答案:

没有答案