0

I am trying to get date from datepicker on textview click. I am getting following error please help me to solve this.

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)

Here is my code.

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;
}

}

This is my fragment_edit_profile_it_project.xml code

<?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>
Clay
  • 4,458
  • 3
  • 29
  • 45

2 Answers2

0

means txtfromdate is null,you should specify view id manally,

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

you set this.id=0,and you dont't have a TextView with id 0,so please check your view,maybe you want this:

TextView txtfromdate = (TextView) this.getView().findViewById(R.id.txtfromdate);
starkshang
  • 7,050
  • 6
  • 34
  • 50
0

Try this:

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;
}
Blunderer
  • 846
  • 7
  • 15