我想通过使用数字选择器在Android中创建自定义时间戳对话框。我如何实现一个监听器?
以下是我的代码片段:
intervalEditText.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
DurationPickerDialog durationPickerDialog = new DurationPickerDialog(getActivity(),
new DurationPickerDialog.onDurationSetlistener() {
@Override
public void onDurationSet(View v, int hour, int minute, int seconds) {
intervalEditText.setText(hour + ":" + minute + ":" + seconds);
}
});
durationPickerDialog.show();
}
});
DurationPickerDialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primaryColor"
android:divider="@drawable/horizontal_divider_red"
android:gravity="center"
android:orientation="vertical"
android:showDividers="middle">
<com.identiv.utrust.sense.uielements.FLightTextView
android:id="@+id/duration"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:textColor="@color/labelSetupText"
android:text="Interval: 00:00:00"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:gravity="center"
android:orientation="horizontal">
<NumberPicker
android:id="@+id/numberPicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="6dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="6dp"
android:layout_marginTop="6dp"
android:focusable="false"
android:gravity="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=":" />
<NumberPicker
android:id="@+id/numberPicker2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:focusable="false"
android:gravity="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=":" />
<NumberPicker
android:id="@+id/numberPicker3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="6dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="24dp"
android:layout_marginTop="6dp"
android:focusable="false"
android:gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@drawable/vertical_divider_red"
android:orientation="horizontal"
android:showDividers="middle">
<com.identiv.utrust.sense.uielements.FRomanButton
android:id="@+id/cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:text="Cancel"
android:textColor="@color/labelSetupText"
android:textSize="14sp" />
<com.identiv.utrust.sense.uielements.FRomanButton
android:id="@+id/set"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:text="Set"
android:textColor="@color/labelSetupText"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
duration picker dialog.Java
public class DurationPickerDialog extends Dialog {
private static NumberPicker hourPicker;
private static NumberPicker minutesPicker;
private static NumberPicker secondsPicker;
private static Button cancelButton;
private static Button setButton;
private static TextView duration;
private onDurationSetlistener listener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.datepickerdialog);
hourPicker = (NumberPicker)findViewById(R.id.numberPicker1);
minutesPicker = (NumberPicker)findViewById(R.id.numberPicker2);
secondsPicker = (NumberPicker)findViewById(R.id.numberPicker3);
cancelButton = (Button)findViewById(R.id.cancel);
setButton = (Button)findViewById(R.id.set);
duration = (TextView)findViewById(R.id.duration);
hourPicker.setMaxValue(9);
hourPicker.setMinValue(0);
setDividerColor(hourPicker);
hourPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
minutesPicker.setMaxValue(59);
minutesPicker.setMinValue(0);
setDividerColor(minutesPicker);
minutesPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
secondsPicker.setMaxValue(59);
secondsPicker.setMinValue(0);
setDividerColor(secondsPicker);
secondsPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
setButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onDurationSet(v, hourPicker.getValue(), minutesPicker.getValue(), secondsPicker.getValue());
}
});
hourPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
duration.setText("Interval: " + String.format("%02d", hourPicker.getValue()) + ":" + String.format("%02d", minutesPicker.getValue()) + ":" + String.format("%02d", secondsPicker.getValue()));
}
});
minutesPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
duration.setText("Interval: " + String.format("%02d", hourPicker.getValue()) + ":" + String.format("%02d", minutesPicker.getValue()) + ":" + String.format("%02d", secondsPicker.getValue()));
}
});
secondsPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
duration.setText("Interval: " + String.format("%02d", hourPicker.getValue()) + ":" + String.format("%02d", minutesPicker.getValue()) + ":" + String.format("%02d", secondsPicker.getValue()));
}
});
}
private void setDividerColor(NumberPicker picker) {
java.lang.reflect.Field[] pickerFields = NumberPicker.class.getDeclaredFields();
for (java.lang.reflect.Field pf : pickerFields) {
if (pf.getName().equals("mSelectionDivider")) {
pf.setAccessible(true);
try {
pf.set(picker, getContext().getResources().getDrawable(R.drawable.horizontal_divider_red));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (Resources.NotFoundException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
break;
}
}
}
private void updateTitle(int hour, int min, int sec){
duration.setText("Duration: " + hour + ":" + min + ":" + sec);
}
public DurationPickerDialog(Context context, onDurationSetlistener callBack) {
super(context);
}
public static interface onDurationSetlistener {
public abstract void onDurationSet (View v, int hour, int minute, int seconds);
}
}
上面的代码导致应用程序在单击set时崩溃。我想知道在创建自定义对话框时我犯了什么错误。我希望将对话框中的时间传递给活动。
LogcatReport
Process: com.identiv.utrust.sense.temperaturetracker, PID: 27790
java.lang.NullPointerException
at com.identiv.utrust.sense.uielements.DurationPickerDialog$2.onClick(DurationPickerDialog.java:71)
at android.view.View.performClick(View.java:4710)
at android.view.View$PerformClick.run(View.java:19510)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5678)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
我发现onDurationListener对象为null。我的实施是否正确?
这是你获得NullPointerException
的路线
listener.onDurationSet(v, hourPicker.getValue(), minutesPicker.getValue(), secondsPicker.getValue());
因为listener
为null,所以你没有在任何地方初始化它。
在我的代码中,一切都是正确的,除了我没有将在函数调用期间创建的新侦听器对象分配给类成员。
错误的代码
public DurationPickerDialog(Context context, onDurationSetlistener callBack) {
super(context);
}
正确的代码
public DurationPickerDialog(Context context, onDurationSetlistener callBack)
{
super(context);
this.listener = callBack;
}