Android 从datepicker返回后如何在edit textview上设置日期

Android 从datepicker返回后如何在edit textview上设置日期,android,android-toolbar,android-datepicker,Android,Android Toolbar,Android Datepicker,我在android项目中使用第三方datepicker库。指向此库的链接是 当我的片段中的一个按钮被点击时,我正在打开这个日期选择器。我已放置此datepicker库的所有依赖项,但当我尝试在接口的onDateSet方法中访问edittextview时,它会显示空指针异常,不允许我访问此edittextview 下面是我在按下按钮时打开datepicker的片段代码 package com.example.anonymous.ghar_ka_khana; import android.cont

我在android项目中使用第三方datepicker库。指向此库的链接是

当我的片段中的一个按钮被点击时,我正在打开这个日期选择器。我已放置此datepicker库的所有依赖项,但当我尝试在接口的onDateSet方法中访问edittextview时,它会显示空指针异常,不允许我访问此edittextview

下面是我在按下按钮时打开datepicker的片段代码

package com.example.anonymous.ghar_ka_khana;

import android.content.Context;
import java.util.Calendar;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.app.Fragment;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;   
import com.wdullaer.materialdatetimepicker.date.DatePickerDialog;
import com.wdullaer.materialdatetimepicker.time.TimePickerDialog;

public class Selling_item_detail extends Fragment implements DatePickerDialog.OnDateSetListener,TimePickerDialog.OnTimeSetListener {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";  
    EditText timetext;
    EditText datetext;
    int hours;
    View v;               
    private String mParam1;
    private String mParam2;   
    private OnFragmentInteractionListener mListener;    
    public Selling_item_detail() {
        // Required empty public constructor
    }

    public static Selling_item_detail newInstance(String param1, String param2) {
        Selling_item_detail fragment = new Selling_item_detail();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {            
        v =inflater.inflate(R.layout.fragment_selling_item_detail, container, false);
        /*timetext = (EditText)v.findViewById(R.id.time);
        datetext = (EditText)v.findViewById(R.id.date);*/           
        ImageButton calendarbutton =(ImageButton)v.findViewById(R.id.dateButton);
        calendarbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Calendar now = Calendar.getInstance();
                DatePickerDialog dpd = DatePickerDialog.newInstance(
                        new Selling_item_detail(),
                        now.get(Calendar.YEAR),
                        now.get(Calendar.MONTH),
                        now.get(Calendar.DAY_OF_MONTH)
                );
                dpd.show(getFragmentManager(), "Datepickerdialog");
            }
        });  
        ImageButton timebutton =(ImageButton)v.findViewById(R.id.timeButton);
        timebutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view)
            {
                Calendar now = Calendar.getInstance();
                TimePickerDialog dpd = TimePickerDialog.newInstance(
                        new Selling_item_detail(),
                        now.get(Calendar.HOUR_OF_DAY),
                        now.get(Calendar.MINUTE),
                        now.isLenient()
                );
                dpd.show(getFragmentManager(), "Datepickerdialog");
            }
        });

        return  v;
    }

    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()+ " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth)
    {          
        hours=year;
        timetext = (EditText)v.findViewById(R.id.time);
        datetext = (EditText)v.findViewById(R.id.date);
        Log.i("timecontext",timetext+"");
        Log.i("datecontext",datetext+"");
        Log.i("tareekh",dayOfMonth+"-"+monthOfYear+"-"+year);
    }

    @Override
    public void onTimeSet(TimePickerDialog view, int hourOfDay, int minute, int second)
    {
      //  timetext.setText(hourOfDay+":"+second+":"+minute);
        Log.i("waqt",hourOfDay+":"+second+":"+minute);
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}
package.com.example.anonymous.ghar_ka_khana;
导入android.content.Context;
导入java.util.Calendar;
导入android.net.Uri;
导入android.os.Build;
导入android.os.Bundle;
导入android.app.Fragment;
导入android.support.annotation.RequiresApi;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.EditText;
导入android.widget.ImageButton;
导入com.wdullaer.materialdatetimepicker.date.DatePickerDialog;
导入com.wdullaer.materialdatetimepicker.time.TimePickerDialog;
公共类Selling_item_detail扩展片段实现DatePickerDialog.OnDateSetListener、TimePickerDialog.OnTimeSetListener{
私有静态最终字符串ARG_PARAM1=“PARAM1”;
私有静态最终字符串ARG_PARAM2=“PARAM2”;
编辑文本时间文本;
编辑文本日期文本;
整小时;
观点五;
私有字符串mParam1;
私有字符串mParam2;
私有OnFragmentInteractionListener mListener;
公开销售商品详情(){
//必需的空公共构造函数
}
public static Selling_item_detail newInstance(字符串param1,字符串param2){
销售项目详细信息片段=新销售项目详细信息();
Bundle args=新Bundle();
args.putString(ARG_PARAM1,PARAM1);
args.putString(ARG_PARAM2,PARAM2);
fragment.setArguments(args);
返回片段;
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
如果(getArguments()!=null){
mParam1=getArguments().getString(ARG_PARAM1);
mParam2=getArguments().getString(ARG_PARAM2);
}
}
@RequiresApi(api=Build.VERSION\u code.N)
@凌驾
创建视图上的公共视图(布局充气机、视图组容器、捆绑包保存状态){
v=充气机。充气(R.布局.碎片\出售\项目\细节,容器,假);
/*timetext=(EditText)v.findviewbyd(R.id.time);
datetext=(EditText)v.findViewById(R.id.date);*/
ImageButton calendarbutton=(ImageButton)v.findViewById(R.id.dateButton);
calendarbutton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
Calendar now=Calendar.getInstance();
DatePickerDialog dpd=DatePickerDialog.newInstance(
新的销售项目详细信息(),
现在。获取(日历。年份),
现在。获取(日历。月份),
now.get(日历.月中日)
);
show(getFragmentManager(),“Datepickerdialog”);
}
});  
ImageButton timebutton=(ImageButton)v.findViewById(R.id.timebutton);
timebutton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图)
{
Calendar now=Calendar.getInstance();
TimePickerDialog dpd=TimePickerDialog.newInstance(
新的销售项目详细信息(),
现在。获取(日历。一天中的小时),
现在。获取(日历。分钟),
现在,islenent()
);
show(getFragmentManager(),“Datepickerdialog”);
}
});
返回v;
}
public void onButtonPressed(Uri){
if(mListener!=null){
onFragmentInteraction(uri);
}
}
@凌驾
公共void-onAttach(上下文){
super.onAttach(上下文);
if(OnFragmentInteractionListener的上下文实例){
mListener=(OnFragmentInteractionListener)上下文;
}否则{
抛出新的RuntimeException(context.toString()+“必须实现OnFragmentInteractionListener”);
}
}
@凌驾
公共无效连接(){
super.onDetach();
mListener=null;
}
@凌驾
公共无效onDateSet(DatePickerDialog视图,整数年,整数月,整数月)
{          
小时=年;
timetext=(EditText)v.findviewbyd(R.id.time);
datetext=(EditText)v.findViewById(R.id.date);
Log.i(“timecontext”,timetext+”);
Log.i(“datecontext”,datetext+”);
Log.i(“tareekh”,月日+“-”+月日+“-”+年);
}
@凌驾
时间集上的公共无效(时间选择器对话框视图、整数小时、整数分钟、整数秒)
{
//timetext.setText(hourOfDay+“:“+second+”:“+minute);
Log.i(“waqt,hourOfDay+”:“+second+”:“+minute”);
}
/**
*此接口必须由包含以下内容的活动实现
*片段,以允许通信此片段中的交互
*该活动以及其中可能包含的其他片段
*活动。
*
*有关更多信息,请参阅Android培训课程。
*/
FragmentInteractionListener上的公共接口{
//TODO:更新参数类型和名称
void onFragmentInteraction(Uri);
}
}
下面是这个片段的XML文件

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?attr/actionBarSize"
    xmlns:fab="http://schemas.android.com/apk/res-auto">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="24dp">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="220dp"
            >

            <com.mikepenz.materialdrawer.view.BezelImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/header"
                android:id="@+id/seelingfoodimage"/>

            <com.scalified.fab.ActionButton
                android:id="@+id/cambutton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|start"
                fab:button_color="#ffffff"
                fab:button_colorPressed="#FF0000"
                fab:image="@drawable/cam"
                fab:image_size="34dp" />

        </FrameLayout>

        <com.github.florent37.materialtextfield.MaterialTextField
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            app:mtf_openKeyboardOnFocus="true"
            app:mtf_labelColor="#666"
            app:mtf_image="@drawable/food"
            >

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/foodname"
                android:hint="Foodname"
                android:layout_marginTop="10dp"
                android:textColor="#333"
                android:textSize="15sp" />

        </com.github.florent37.materialtextfield.MaterialTextField>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="horizontal">

            <com.github.florent37.materialtextfield.MaterialTextField
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:mtf_labelColor="#666"
                app:mtf_image="@drawable/calendar"
                app:mtf_openKeyboardOnFocus="true"
                >

                <EditText
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/date"
                    android:hint="Selling Location"
                    android:textColor="#333"
                    android:textSize="15sp" />

            </com.github.florent37.materialtextfield.MaterialTextField>

            <ImageButton
                android:id="@+id/dateButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="40dp"
                android:layout_marginLeft="5dp"
                android:src="@drawable/calendar"/>

        </LinearLayout>

        <!--com.github.florent37.materialtextfield.MaterialTextField
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:mtf_labelColor="#666"
            app:mtf_image="@drawable/calendar"
            android:layout_marginTop="10dp"
            app:mtf_openKeyboardOnFocus="true"
            >

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/date"
                android:layout_marginTop="10dp"
                android:hint="Date"
                android:textColor="#333"
                android:textSize="15sp" />

        </com.github.florent37.materialtextfield.MaterialTextField-->


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="horizontal">

            <com.github.florent37.materialtextfield.MaterialTextField
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:mtf_labelColor="#666"
                app:mtf_image="@drawable/time"
                app:mtf_openKeyboardOnFocus="true"
                >

                <EditText
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/time"
                    android:hint="Selling Location"
                    android:textColor="#333"
                    android:textSize="15sp" />

            </com.github.florent37.materialtextfield.MaterialTextField>

            <ImageButton
                android:id="@+id/timeButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="40dp"
                android:layout_marginLeft="5dp"
                android:src="@drawable/time"/>

        </LinearLayout>

        <!--com.github.florent37.materialtextfield.MaterialTextField
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:mtf_labelColor="#666"
            android:layout_marginTop="10dp"
            app:mtf_image="@drawable/time"
            app:mtf_openKeyboardOnFocus="true"
            >

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/mail"
                android:hint="Time"
                android:layout_marginTop="10dp"
                android:textColor="#333"
                android:textSize="15sp" />

        </com.github.florent37.materialtextfield.MaterialTextField-->

        <com.github.florent37.materialtextfield.MaterialTextField
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:mtf_labelColor="#666"
            android:layout_marginTop="10dp"
            app:mtf_image="@drawable/plate"
            app:mtf_openKeyboardOnFocus="true"
            >

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/plate"
                android:hint="Number Of Plates"
                android:layout_marginTop="10dp"
                android:textColor="#333"
                android:textSize="15sp" />

        </com.github.florent37.materialtextfield.MaterialTextField>



        <com.github.florent37.materialtextfield.MaterialTextField
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:mtf_labelColor="#666"
            android:layout_marginTop="10dp"
            app:mtf_image="@drawable/money"
            app:mtf_openKeyboardOnFocus="true"
            >

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/money"
                android:hint="Price Per Plate"
                android:layout_marginTop="10dp"
                android:textColor="#333"
                android:textSize="15sp" />

        </com.github.florent37.materialtextfield.MaterialTextField>



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <com.github.florent37.materialtextfield.MaterialTextField
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:mtf_labelColor="#666"
            app:mtf_image="@drawable/location"
            app:mtf_openKeyboardOnFocus="true"
            >

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/location"
                android:hint="Selling Location"
                android:textColor="#333"
                android:textSize="15sp" />

        </com.github.florent37.materialtextfield.MaterialTextField>

        <ImageButton
            android:id="@+id/mapButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="40dp"
            android:layout_marginLeft="5dp"
            android:src="@drawable/map"/>

    </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginTop="20dp">

            <Button
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="OK"
                android:textColor="#ffffff"
                android:id="@+id/ok"
                android:background="@drawable/rounded_button_selector" />
            <LinearLayout
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content">

            </LinearLayout>

            <Button
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="Cancel"
                android:textColor="#ffffff"
                android:id="@+id/cancel"
                android:background="@drawable/rounded_button_selector" />

        </LinearLayout>

    </LinearLayout>
</ScrollView>
<!--ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#a1000000"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.example.anonymous.ghar_ka_khana.Selling_item_detail">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <FrameLayout
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_gravity="center"
        android:id="@+id/frame"
         >
        <com.mikepenz.materialdrawer.view.BezelImageView
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:src="@drawable/header"
         android:id="@+id/seelingfoodimage"/>

        <com.scalified.fab.ActionButton
            android:id="@+id/cambutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            fab:button_color="#ffffff"
            fab:button_colorPressed="#FF0000"
            fab:image="@drawable/cam"
            fab:image_size="34dp" />
    </FrameLayout>

    <com.github.florent37.materialtextfield.MaterialTextField
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:layout_below="@+id/frame"
        app:mtf_openKeyboardOnFocus="true"
        app:mtf_labelColor="#666"
        app:mtf_image="@drawable/ic_person_black_24dp"
        app:mtf_hasFocus="true"
        >

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/foodname"
            android:hint="Foodname"
            android:text="food"
            android:layout_marginTop="20dp"
            android:textColor="#333"
            android:textSize="15dp" />

    </com.github.florent37.materialtextfield.MaterialTextField-->

    <!--com.github.florent37.materialtextfield.MaterialTextField
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:layout_below="@+id/foodnames"
        android:id="@+id/dates"
        app:mtf_openKeyboardOnFocus="true"
        app:mtf_labelColor="#666"
        app:mtf_image="@drawable/calendar"
        app:mtf_hasFocus="true"
        >

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/date"
            android:hint="Foodname"
            android:text="food"
            android:layout_marginTop="20dp"
            android:textColor="#333"
            android:textSize="15sp" />

    </com.github.florent37.materialtextfield.MaterialTextField>

    <com.github.florent37.materialtextfield.MaterialTextField
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:layout_below="@+id/dates"
        android:id="@+id/times"
        app:mtf_openKeyboardOnFocus="true"
        app:mtf_labelColor="#666"
        app:mtf_image="@drawable/time"
        app:mtf_hasFocus="true"
        >

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/time"
            android:hint="Foodname"
            android:text="food"
            android:layout_marginTop="20dp"
            android:textColor="#333"
            android:textSize="15sp" />

    </com.github.florent37.materialtextfield.MaterialTextField>

    <com.github.florent37.materialtextfield.MaterialTextField
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:layout_below="@+id/times"
        android:id="@+id/plates"
        app:mtf_openKeyboardOnFocus="true"
        app:mtf_labelColor="#666"
        app:mtf_image="@drawable/plate"
        app:mtf_hasFocus="true"
        >

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/plate"
            android:hint="Foodname"
            android:text="food"
            android:layout_marginTop="20dp"
            android:textColor="#333"
            android:textSize="15sp" />

    </com.github.florent37.materialtextfield.MaterialTextField>
    </RelativeLayout>

</ScrollView-->

public static void showDatePicker(Context context, final AppCompatEditText editText) {
    // Get Current Date
    final Calendar c = Calendar.getInstance();
    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    mDay = c.get(Calendar.DAY_OF_MONTH);


    DatePickerDialog datePickerDialog = new DatePickerDialog(context,
            new DatePickerDialog.OnDateSetListener() {

                @Override
                public void onDateSet(DatePicker view, int year,
                                      int monthOfYear, int dayOfMonth) {
                    mYear = year;
                    mMonth = monthOfYear;
                    mDay = dayOfMonth;
                    editText.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);

                }
            }, mYear, mMonth, mDay);
    datePickerDialog.show();
}
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    v =inflater.inflate(R.layout.fragment_selling_item_detail, container, false);
    timetext = (EditText)v.findViewById(R.id.time);
    datetext = (EditText)v.findViewById(R.id.date);
    return v;
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("h:mma");
String date = mMonth+1 + "/" + mDay + "/" + mYear + " : " + simpleDateFormat.format(time);
 timetext.setText(date);