“尝试在空对象引用上调用虚拟方法'void android.widget.TextView.setText(java.lang.CharSequence)'”

时间:2019-04-27 07:04:45

标签: android-fragments

无法加载应用程序,并且显示空指针错误。错误类似于“ java.lang.NullPointerException:尝试在null上调用虚拟方法'void android.widget.TextView.setText(java.lang.CharSequence)'”对象引用”

我已经在另一个活动中编写了相同的代码,在那边它可以工作,但是在这里不起作用。我想在textview点击上显示日期选择器。

这是我的Java类代码:

public class AdminProfileActivity extends Fragment {

DatePickerDialog picker;

private String date;
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootview = inflater.inflate(R.layout.activity_custom_calendar, container, false);
    final Calendar myCalendar = Calendar.getInstance();
    final TextView txtDate=(TextView) rootview.findViewById(R.id.txtBirtday);
    final SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    date = dateFormat.format(Calendar.getInstance().getTime());
    txtDate.setText(date);
    txtDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Calendar cldr = Calendar.getInstance();
            int day = cldr.get(Calendar.DAY_OF_MONTH);
            int month = cldr.get(Calendar.MONTH);
            int year = cldr.get(Calendar.YEAR);
            // date picker dialog
            picker = new DatePickerDialog(getActivity(),new DatePickerDialog.OnDateSetListener() {
                        @Override
                        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                            txtDate.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);
                        }
                    }, year, month, day);
            picker.getDatePicker().setSpinnersShown(true);
            picker.getDatePicker().setCalendarViewShown(false);
            picker.show();
        }
    });


    return rootview;
}
}

这是.XML文件代码:

 <LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AdminProfileActivity"
android:orientation="vertical"
android:padding="15dp"
android:layout_margin="10dp">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="#fff">

<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/txtInputName"
    app:errorEnabled="true"
    android:textColorHint="#0E0D0D">
    <android.support.design.widget.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtName"
        android:hint="Enter Your Name"
        android:inputType="text"/>
</android.support.design.widget.TextInputLayout>


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Select Birthdate"
        android:textColor="#0E0D0D"
        android:layout_marginRight="5dp"
        android:layout_marginBottom="5dp"
        android:textSize="22dp"
        android:id="@+id/txtBirtday"
        android:focusable="false"
        />

<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/gender"
    android:layout_below="@+id/mrname_layout"
    android:orientation="horizontal">
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/rbtnMale"
        android:checked="true"
        android:text="Male"
        android:textSize="20dp"
        />
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/rbtnFemale"
        android:text="Female"
        android:textSize="20dp"/>
</RadioGroup>
<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/txtInputAddress"
    app:errorEnabled="true"
    android:textColorHint="#0E0D0D">
    <android.support.design.widget.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtAddress"
        android:hint="Enter Your Address"
        android:inputType="textMultiLine" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtInputCity"
app:errorEnabled="true"
android:textColorHint="#0E0D0D">
<android.support.design.widget.TextInputEditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/txtCity"
    android:hint="City"
    android:inputType="text" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/txtInputState"
    app:errorEnabled="true"
    android:textColorHint="#0E0D0D">
    <android.support.design.widget.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtState"
        android:hint="State"
        android:inputType="text" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/txtInputCountry"
    app:errorEnabled="true"
    android:textColorHint="#0E0D0D">
    <android.support.design.widget.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtCountry"
        android:hint="Country"
        android:inputType="text" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/txtInputPhone"
    app:errorEnabled="true"
    android:textColorHint="#0E0D0D">
    <android.support.design.widget.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtPhone"
        android:hint="Mobile Number"
        android:inputType="textPhonetic" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/txtInputEmail"
    app:errorEnabled="true"
    android:textColorHint="#0E0D0D">
    <android.support.design.widget.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtEmail"
        android:hint="Email Address"
        android:inputType="textEmailAddress" />
</android.support.design.widget.TextInputLayout>
<android.support.v7.widget.AppCompatButton
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Save Profile"
    android:background="#1D77D6"
    android:id="@+id/btnSave"
    android:textColor="#fff"
    android:layout_gravity="bottom"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    android:textAllCaps="false"
    android:textSize="20dp"/>
</LinearLayout>

0 个答案:

没有答案