Android:无法在fragment中的datepicker中设置日期

时间:2016-01-05 10:29:52

标签: android android-fragments nullpointerexception android-datepicker

我正试图在textview上点击datepicker的日期。我得到以下错误请帮我解决这个问题。

01-05 15:57:23.214 23076-23076/com.example.technotract.karofly2 E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.example.technotract.karofly2, PID: 23076
      java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
          at com.example.technotract.karofly2.edit_profile_project_Fragment.setTextViewText(edit_profile_project_Fragment.java:41)
          at com.example.technotract.karofly2.afterlogin$1.onDateSet(afterlogin.java:88)
          at android.app.DatePickerDialog.onClick(DatePickerDialog.java:134)
          at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:162)
          at android.os.Handler.dispatchMessage(Handler.java:102)
          at android.os.Looper.loop(Looper.java:135)
          at android.app.ActivityThread.main(ActivityThread.java:5343)
          at java.lang.reflect.Method.invoke(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:372)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

这是我的代码。

public class edit_profile_project_Fragment extends Fragment {
View rootview;


private  int year_P,month_P,day_P;
TextView txtfromdate;
TextView txttodate;
static final  int DIALOG_ID=0;
private SimpleDateFormat dateFormatter;
//private TextView txtfromdate;
static String value = "Hello";
int id = 0;

public void setTextViewText(String value){
    Log.d("kissu", value);
    TextView txtfromdate = (TextView) this.getView().findViewById(this.id);
    txtfromdate.setText(value);
    // this.value = value;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootview = inflater.inflate(R.layout.fragment_edit_profile_it_project, container, false);
    this.id = R.id.txtfromdate;
    //initializing textfield
    txtfromdate = (TextView) rootview.findViewById(R.id.txtfromdate);

    txtfromdate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getActivity().showDialog(DIALOG_ID);
            Log.d("nnn666", value);
            txtfromdate.setText("pravin");
            Log.d("nnn777", value);
        }

    });

    txttodate = (TextView) rootview.findViewById(R.id.txttodate);

    txttodate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
         /*   ((afterlogin) getActivity()).showDialogOnClick(txttodate);*/
        }
    });

    return rootview;
}

}

这是我的fragment_edit_profile_it_project.xml代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:background="@drawable/bg_profile1"
    android:id="@+id/fragment_edit_profile_it_skills"
    android:padding="5dp">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingTop="16dp"
        android:paddingBottom="16dp"
        android:background="#452a2a33"
        android:id="@+id/relativeLayout"
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Project Caption"
            android:id="@+id/textView6" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:inputType="textPersonName"
            android:ems="10"
            android:id="@+id/txtcaption"
            android:autoText="false"
            android:background="#e8e8e8"
            android:layout_below="@+id/textView6"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Duration"
            android:id="@+id/textView9"
            android:layout_below="@+id/txtcaption"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="20dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="From"
            android:id="@+id/textView11"
            android:layout_below="@+id/textView9"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="10dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:inputType="textPersonName"
            android:ems="10"
            android:id="@+id/txtfromdate"

            android:background="#e8e8e8"
            android:layout_below="@+id/textView11"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:drawableRight="@drawable/calendarblack"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:paddingRight="20dp"
            android:editable="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="To"
            android:id="@+id/textView16"
            android:layout_below="@+id/txtfromdate"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="20dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:inputType="textPersonName"
            android:ems="10"
            android:id="@+id/txttodate"
            android:autoText="false"
            android:background="#e8e8e8"
            android:drawableRight="@drawable/calendarblack"
            android:layout_below="@+id/textView16"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:paddingRight="20dp"
            android:editable="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Description"
            android:id="@+id/textView17"
            android:layout_below="@+id/txttodate"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="20dp" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="90dp"
            android:inputType="textLongMessage|textMultiLine"
            android:ems="10"
            android:id="@+id/txtdesc"
            android:layout_below="@+id/textView17"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:background="#e8e8e8"
            android:enabled="false"
            android:editable="false" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Submit"
            android:id="@+id/btn_submit_project"
            android:background="#1f1f1f"
            android:textColor="#8e8e8e"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

    </RelativeLayout>
</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

表示txtfromdate为空,您应该手动指定视图ID,

TextView txtfromdate = (TextView) this.getView().findViewById(this.id);

您设置了this.id=0,并且您没有ID为0的TextView,所以请检查您的视图,也许您想要这个:

TextView txtfromdate = (TextView) this.getView().findViewById(R.id.txtfromdate);

答案 1 :(得分:0)

试试这个:

public class edit_profile_project_Fragment extends Fragment {

private  int year_P,month_P,day_P;
TextView txtfromdate;
TextView txttodate;
static final  int DIALOG_ID=0;
private SimpleDateFormat dateFormatter;
private TextView txtfromdate;
static String value = "Hello";

public void setTextViewText(String value){
    Log.d("kissu", value);
    txtfromdate.setText(value);
    // this.value = value;
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootview = inflater.inflate(R.layout.fragment_edit_profile_it_project, container, false);
    //initializing textfield
    txtfromdate = (TextView) rootview.findViewById(R.id.txtfromdate);

    txtfromdate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getActivity().showDialog(DIALOG_ID);
            Log.d("nnn666", value);
            txtfromdate.setText("pravin");
            Log.d("nnn777", value);
        }

    });

    txttodate = (TextView) rootview.findViewById(R.id.txttodate);

    txttodate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
         /*   ((afterlogin) getActivity()).showDialogOnClick(txttodate);*/
        }
    });

    return rootview;
}