2015-06-15 64 views
1

自定義對話框聽衆,我想通過使用數字選擇器的創造的Android定製timepicker對話框。我將如何實現一個聽衆?下面是代碼片段,幫助創建的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> 

DurationPickerDialog.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); 
    } 
} 

上面的代碼會導致應用程序被點擊集時崩潰..我想知道的我都做在創建自定義對話框什麼錯誤。我希望將對話的時間傳遞給活動。

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對象爲空。請讓我知道,如果我的實現是正確的實際

+0

後logcat的錯誤.. –

回答

1

這是你在哪裏得到NullPointerException

listener.onDurationSet(v, hourPicker.getValue(), minutesPicker.getValue(), secondsPicker.getValue()); 

因爲listener是空行,你還沒有初始化它的任何地方。

0

在我的代碼的一切是正確的,但我沒有分配函數調用類成員時創建的新偵聽器對象。

錯碼

public DurationPickerDialog(Context context, onDurationSetlistener callBack) { 
    super(context); 
} 

正確的代碼

public DurationPickerDialog(Context context, onDurationSetlistener callBack) 
{ 
    super(context); 
    this.listener = callBack; 
}