我在我的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);
}
}
此片段的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-->
答案 0 :(得分:1)
我将自定义代码设置为打开date picker
并在文本上设置日期:
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();
}
答案 1 :(得分:0)
在片段的OnCreateView()
中声明您的eddittext。像这样:
@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);
然后在那些覆盖的方法中将日期/时间设置为eddittext。
timetext.setText(date);