2

因为我想自定义我自己的时间选择器对话框,所以我不使用提供的时间选择器对话框。但是,我发现如果我优先调用对话框,它就不起作用。但如果我把它放在一个普通的 xml 布局中,它就可以工作。eclipse没有检查代码错误,但是如果我在手机中运行,nullpointerexception从下面的粗线开始出现。

public class Prefs extends PreferenceActivity{

    Preference time;

    void openCustomDialog() {
        AlertDialog.Builder customDialog = new AlertDialog.Builder(Prefs.this);
        customDialog.setTitle("Set Delay Time");
        LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.timepicker, null); 
        TimePicker minute = (TimePicker) findViewById(R.id.mPicker);
        **minute.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener()** {
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
            }
        });
        minute.setCurrentMinute(usb.minute);

        TimePicker second = (TimePicker) findViewById(R.id.sPicker);
        second.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            public void onTimeChanged(TimePicker view, int hourOfDay, int second) {
            }
        });
        second.setCurrentMinute(usb.second);
        customDialog.setPositiveButton("OK",new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                    }
                });
        customDialog.setView(view);
        customDialog.show();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.prefs);

        time = (Preference)findPreference("time");

        time.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference arg0) { 
                openCustomDialog();  
                return true;
            }
        });
}
4

0 回答 0