2013-07-15 31 views
0

我一直在尋找幾個小時,沒有運氣。我正在使用Eclipse創建一個帶有timepicker和datepicker的Android應用程序,當我調用OnCreateDialog中的timepicker偵聽器時,datepicker可以正常工作,因此無法將timePickerListener解析爲變量錯誤。timepicker偵聽器無法解析爲變量

我跟着整個堆的例子,對我來說,它看起來像我複製,但無法擺脫這個錯誤。特別是在這個問題底部的分辨率

Using DatePicker and TimePicker dialog boxes - Android Java in Eclipse

我的Java代碼如下:

private TextView tvDisplayDate; 
private DatePicker dpResult; 
private TimePicker timepicker; 
private Button infringementbutton; 
private Button timebutton; 
private TextView tvDisplayDate2; 
private TextView tvDisplayTime; 

private int year; 
private int month; 
private int day; 
private int yearA; 
private int monthA; 
private int dayA; 
private int hour; 
private int minute; 


static final int DATE_DIALOG_ID = 1; 
static final int TIME_DIALOG_ID = 2;  

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    setCurrentDateOnView(); 
    setCurrentTimeOnView(); 
    addListenerOnButton(); 
    } 


    // display current date 
public void setCurrentDateOnView() { 

    tvDisplayDate = (TextView) findViewById(R.id.tvDate); 
    tvDisplayDate2 = (TextView) findViewById(R.id.tvDateA); 
    dpResult = (DatePicker) findViewById(R.id.dpResult); 

    final Calendar c = Calendar.getInstance(); 
    year = c.get(Calendar.YEAR); 
    month = c.get(Calendar.MONTH); 
    day = c.get(Calendar.DAY_OF_MONTH); 
    hour = c.get(Calendar.HOUR_OF_DAY); 
    minute = c.get(Calendar.MINUTE); 



    // set current date into textview 
    tvDisplayDate.setText(new StringBuilder() 
     // Month is 0 based, just add 1 


     .append(day).append("-").append(month + 1).append("-") 
     .append(year).append(" ")); 

    c.add(Calendar.DAY_OF_MONTH, 42); 
    dayA = c.get(Calendar.DAY_OF_MONTH); 
    monthA = c.get(Calendar.MONTH); 
    yearA = c.get(Calendar.YEAR); 
    tvDisplayDate2.setText(new StringBuilder() 
    // Month is 0 based, just add 1 


    .append(dayA).append("-").append(monthA + 1).append("-") 
    .append(yearA).append(" ")); 


    // set current date into datepicker 
    dpResult.init(year, month, day, null); 

} 
//Display current time 
public void setCurrentTimeOnView() { 
    tvDisplayTime = (TextView) findViewById(R.id.tvTime); 
    timepicker = (TimePicker) findViewById(R.id.timepicker); 



    // set current time into textview 
      tvDisplayTime.setText(
         new StringBuilder().append(pad(hour)) 
              .append(":").append(pad(minute))); 

      // set current time into timepicker 
      timepicker.setCurrentHour(hour); 
      timepicker.setCurrentMinute(minute); 

} 

public void addListenerOnButton() { 

    infringementbutton = (Button) findViewById(R.id.infringementbutton); 
timebutton = (Button) findViewById(R.id.timebutton); 

    infringementbutton.setOnClickListener(new OnClickListener() { 


     @SuppressWarnings("deprecation") 
     @Override 
     public void onClick(View view) { 

      showDialog(DATE_DIALOG_ID); 

     } 

    }); 
timebutton.setOnClickListener(new View.OnClickListener() { 

      @SuppressWarnings("deprecation") 
      @Override 
      public void onClick(View v) { 

       showDialog(TIME_DIALOG_ID); 

      } 
     }); 
} 

protected Dialog onCreateDialog(int id) { 


    switch (id) { 
    case DATE_DIALOG_ID: 
     // set date picker as current date 
     return new DatePickerDialog(this, datePickerListener, 
        year, month,day); 
     break; 

    switch (id) { 

    case TIME_DIALOG_ID: 
     // set time picker as current time 
     return new TimePickerDialog(this, 
       timePickerListener, hour, minute,false);} 
    return null; 


}} 

private DatePickerDialog.OnDateSetListener datePickerListener 
      = new DatePickerDialog.OnDateSetListener() { 

    // when dialog box is closed, below method will be called. 
    public void onDateSet(DatePicker view, int selectedYear, 
      int selectedMonth, int selectedDay) { 
     year = selectedYear; 
     month = selectedMonth; 
     day = selectedDay; 



     // set selected date into textview 
     tvDisplayDate.setText(new StringBuilder() 
     .append(day).append("-").append(month + 1).append("-").append(year) 
      .append(" ")); 

     Calendar c = new GregorianCalendar(); 
     c.set(Calendar.YEAR,year); 
     c.set(Calendar.MONTH, month); 
     c.set(Calendar.DAY_OF_MONTH, day); 
     c.add(Calendar.DAY_OF_MONTH,42); 
     dayA = c.get(Calendar.DAY_OF_MONTH); 
     monthA = c.get(Calendar.MONTH); 
     yearA = c.get(Calendar.YEAR); 

     tvDisplayDate2.setText(new StringBuilder() 
     .append(dayA).append("-").append(monthA + 1).append("-").append(yearA) 
      .append(" ")); 

     // set selected date into datepicker also 
     dpResult.init(year, month, day, null); 



    } 


    private TimePickerDialog.OnTimeSetListener timePickerListener = 
      new TimePickerDialog.OnTimeSetListener() { 
     public void onTimeSet(TimePicker view, int selectedHour, 
       int selectedMinute) { 
      hour = selectedHour; 
      minute = selectedMinute; 

      // set current time into textview 
      tvDisplayTime.setText(new StringBuilder().append(pad(hour)) 
        .append(":").append(pad(minute))); 

      // set current time into timepicker 
      timepicker.setCurrentHour(hour); 
      timepicker.setCurrentMinute(minute); 

     } 
    }; 
}; 


private static String pad(int c) { 
    if (c >= 10) 
     return String.valueOf(c); 
    else 
     return "0" + String.valueOf(c); 
} 

下面是main.xml中

<ScrollView 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/ScrollView01" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_weight="1"> 

<TableLayout 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_margin="@dimen/margin" 
android:orientation="vertical" 
tools:context=".MainActivity" > 

<TableRow> 

<Button 
    android:id="@+id/infringementbutton" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/infringement" 
    /> 

<DatePicker 
    android:id="@+id/dpResult" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:visibility="gone" /> 

<TextView 
    android:id="@+id/tvDate" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

</TableRow><TableRow> 
<Button 
    android:id="@+id/button2" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/penaltydate" /> 
<TextView 
    android:id="@+id/tvDateA" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 
</TableRow> 
    <TableRow > 

    <Button 
    android:id="@+id/timebutton" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/time" 
    /> 

<TimePicker 
    android:id="@+id/timepicker" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:visibility="gone" /> 

<TextView 
    android:id="@+id/tvTime" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 
</TableRow> 

希望代碼ISN」太混亂因爲我只是在這個東西新的。謝謝。

+0

哪行代碼具體導致錯誤?有很多地方需要閱讀。 – Kon

+0

http://developer.android.com/guide/topics/ui/controls/pickers.html。檢查文檔。 – Raghunandan

+0

我不確定哪一行完全正確,但它幾乎在下方的OnCreateDialog方法下的TIME_DIALOG_ID案例 – Joel

回答

0

我評論的佈局相關的代碼,因爲我天璣有它NDü沒有張貼.... 檢查一次的部份了,讓我KNW ...

公共類DialogScreen延伸活動{ 私人TextView tvDisplayDate; 私人DatePicker dpResult; 私人TimePicker timepicker; 私人按鈕侵權按鈕; 私人按鈕時間按鈕; private TextView tvDisplayDate2; private TextView tvDisplayTime;

private int year; 
private int month; 
private int day; 
private int yearA; 
private int monthA; 
private int dayA; 
private int hour; 
private int minute; 

static final int DATE_DIALOG_ID = 1; 
static final int TIME_DIALOG_ID = 2; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    setCurrentDateOnView(); 
    setCurrentTimeOnView(); 
    addListenerOnButton(); 
} 

// display current date 
public void setCurrentDateOnView() { 

    tvDisplayDate = (TextView) findViewById(R.id.tvDate); 
    tvDisplayDate2 = (TextView) findViewById(R.id.tvDateA); 
    dpResult = (DatePicker) findViewById(R.id.dpResult); 

    final Calendar c = Calendar.getInstance(); 
    year = c.get(Calendar.YEAR); 
    month = c.get(Calendar.MONTH); 
    day = c.get(Calendar.DAY_OF_MONTH); 
    hour = c.get(Calendar.HOUR_OF_DAY); 
    minute = c.get(Calendar.MINUTE); 

    // set current date into textview 
    tvDisplayDate.setText(new StringBuilder() 
      // Month is 0 based, just add 1 

      .append(day).append("-").append(month + 1).append("-") 
      .append(year).append(" ")); 

    c.add(Calendar.DAY_OF_MONTH, 42); 
    dayA = c.get(Calendar.DAY_OF_MONTH); 
    monthA = c.get(Calendar.MONTH); 
    yearA = c.get(Calendar.YEAR); 
    tvDisplayDate2.setText(new StringBuilder() 
      // Month is 0 based, just add 1 

      .append(dayA).append("-").append(monthA + 1).append("-") 
      .append(yearA).append(" ")); 

    // set current date into datepicker 
    dpResult.init(year, month, day, null); 

} 

// Display current time 
public void setCurrentTimeOnView() { 
    tvDisplayTime = (TextView) findViewById(R.id.tvTime); 
    timepicker = (TimePicker) findViewById(R.id.timepicker); 

    // set current time into textview 
    tvDisplayTime.setText(new StringBuilder().append(pad(hour)).append(":") 
      .append(pad(minute))); 

    // set current time into timepicker 
    timepicker.setCurrentHour(hour); 
    timepicker.setCurrentMinute(minute); 

} 

public void addListenerOnButton() { 

    infringementbutton = (Button) findViewById(R.id.infringementbutton); 
    timebutton = (Button) findViewById(R.id.timebutton); 

    infringementbutton.setOnClickListener(new View.OnClickListener() { 

     @SuppressWarnings("deprecation") 
     @Override 
     public void onClick(View view) { 

      showDialog(DATE_DIALOG_ID); 

     } 

    }); 
    timebutton.setOnClickListener(new View.OnClickListener() { 

     @SuppressWarnings("deprecation") 
     @Override 
     public void onClick(View v) { 

      showDialog(TIME_DIALOG_ID); 

     } 
    }); 
} 

protected Dialog onCreateDialog(int id) { 

    switch (id) { 
    case DATE_DIALOG_ID: 
     // set date picker as current date 
     return new DatePickerDialog(this, datePickerListener, year, month, 
       day); 
    case TIME_DIALOG_ID: 
     // set time picker as current time 
     return new TimePickerDialog(this, timePickerListener, hour, minute, 
       false); 
    } 
    return null; 
} 

private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() { 

    // when dialog box is closed, below method will be called. 
    public void onDateSet(DatePicker view, int selectedYear, 
      int selectedMonth, int selectedDay) { 
     year = selectedYear; 
     month = selectedMonth; 
     day = selectedDay; 

     // set selected date into textview 
     tvDisplayDate.setText(new StringBuilder().append(day).append("-") 
       .append(month + 1).append("-").append(year).append(" ")); 

     Calendar c = new GregorianCalendar(); 
     c.set(Calendar.YEAR, year); 
     c.set(Calendar.MONTH, month); 
     c.set(Calendar.DAY_OF_MONTH, day); 
     c.add(Calendar.DAY_OF_MONTH, 42); 
     dayA = c.get(Calendar.DAY_OF_MONTH); 
     monthA = c.get(Calendar.MONTH); 
     yearA = c.get(Calendar.YEAR); 

     tvDisplayDate2.setText(new StringBuilder().append(dayA).append("-") 
       .append(monthA + 1).append("-").append(yearA).append(" ")); 

     // set selected date into datepicker also 
     dpResult.init(year, month, day, null); 

    } 

}; 
private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() { 
    public void onTimeSet(TimePicker view, int selectedHour, 
      int selectedMinute) { 
     hour = selectedHour; 
     minute = selectedMinute; 

     // set current time into textview 
     tvDisplayTime.setText(new StringBuilder().append(pad(hour)) 
       .append(":").append(pad(minute))); 

     // set current time into timepicker 
     timepicker.setCurrentHour(hour); 
     timepicker.setCurrentMinute(minute); 

    } 
}; 

private static String pad(int c) { 
    if (c >= 10) 
     return String.valueOf(c); 
    else 
     return "0" + String.valueOf(c); 
} 

}

XML資源

<?xml version="1.0" encoding="utf-8"?> 

<TableLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <TableRow> 

     <Button 
      android:id="@+id/infringementbutton" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Infringement" /> 

     <DatePicker 
      android:id="@+id/dpResult" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:visibility="gone" /> 

     <TextView 
      android:id="@+id/tvDate" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceLarge" /> 
    </TableRow> 

    <TableRow> 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Penaltydate" /> 

     <TextView 
      android:id="@+id/tvDateA" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceLarge" /> 
    </TableRow> 

    <TableRow> 

     <Button 
      android:id="@+id/timebutton" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Time" /> 

     <TimePicker 
      android:id="@+id/timepicker" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:visibility="gone" /> 

     <TextView 
      android:id="@+id/tvTime" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="" 
      android:textAppearance="?android:attr/textAppearanceLarge" /> 
    </TableRow> 
</TableLayout> 

+0

通過佈局相關的代碼你是指xml文件嗎? – Joel

+0

是的資源/佈局 – user1140237

+0

只是把它現在 – Joel

0

你有幾個未接放置{這是很難找到的。 您可以有一個switch語句。

protected Dialog onCreateDialog(int id) { 

    switch (id) { 
    case DATE_DIALOG_ID: 
     // set date picker as current date 
     return new DatePickerDialog(this, datePickerListener, 
        year, month,day); // return here 
     break; //then break 

    switch (id) { // then switch again no need for another switch 

    case TIME_DIALOG_ID: 
     // set time picker as current time 
     return new TimePickerDialog(this, 
       timePickerListener, hour, minute,false);} 
    return null; 
} 
} 

還有些方法是deprecated.So我建議你看一下文檔

http://developer.android.com/guide/topics/ui/controls/pickers.html

具有時間選取器和日期選取器以及代碼片段的詳細exaplanation。

public class pickerdate extends Activity { 

private TextView tvDisplayDate; 
private DatePicker dpResult; 
private TimePicker timepicker; 
private Button infringementbutton; 
private Button timebutton; 
private TextView tvDisplayDate2; 
private TextView tvDisplayTime; 

private int year; 
private int month; 
private int day; 
private int yearA; 
private int monthA; 
private int dayA; 
private int hour; 
private int minute; 


static final int DATE_DIALOG_ID = 1; 
static final int TIME_DIALOG_ID = 2;  

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    setCurrentDateOnView(); 
    setCurrentTimeOnView(); 
    addListenerOnButton(); 
    } 


    // display current date 
public void setCurrentDateOnView() { 

     tvDisplayDate = (TextView) findViewById(R.id.tvDate); 
     tvDisplayDate2 = (TextView) findViewById(R.id.tvDateA); 
     dpResult = (DatePicker) findViewById(R.id.dpResult); 


    final Calendar c = Calendar.getInstance(); 
    year = c.get(Calendar.YEAR); 
    month = c.get(Calendar.MONTH); 
    day = c.get(Calendar.DAY_OF_MONTH); 
    hour = c.get(Calendar.HOUR_OF_DAY); 
    minute = c.get(Calendar.MINUTE); 



    // set current date into textview 
    tvDisplayDate.setText(new StringBuilder() 
     // Month is 0 based, just add 1 


     .append(day).append("-").append(month + 1).append("-") 
     .append(year).append(" ")); 

    c.add(Calendar.DAY_OF_MONTH, 42); 
    dayA = c.get(Calendar.DAY_OF_MONTH); 
    monthA = c.get(Calendar.MONTH); 
    yearA = c.get(Calendar.YEAR); 
    tvDisplayDate2.setText(new StringBuilder() 
    // Month is 0 based, just add 1 


    .append(dayA).append("-").append(monthA + 1).append("-") 
    .append(yearA).append(" ")); 


    // set current date into datepicker 
    dpResult.init(year, month, day, null); 

} 
//Display current time 
public void setCurrentTimeOnView() { 
    tvDisplayTime = (TextView) findViewById(R.id.tvTime); 
    timepicker = (TimePicker) findViewById(R.id.timepicker); 



    // set current time into textview 
      tvDisplayTime.setText(
         new StringBuilder().append(pad(hour)) 
              .append(":").append(pad(minute))); 

      // set current time into timepicker 
      timepicker.setCurrentHour(hour); 
      timepicker.setCurrentMinute(minute); 

} 

public void addListenerOnButton() { 

    infringementbutton = (Button) findViewById(R.id.infringementbutton); 
    timebutton = (Button) findViewById(R.id.timebutton); 

    infringementbutton.setOnClickListener(new OnClickListener() { 


     @SuppressWarnings("deprecation") 
     @Override 
     public void onClick(View view) { 

      showDialog(DATE_DIALOG_ID); 

     } 

    }); 
timebutton.setOnClickListener(new View.OnClickListener() { 

      @SuppressWarnings("deprecation") 
      @Override 
      public void onClick(View v) { 

       showDialog(TIME_DIALOG_ID); 

      } 
     }); 
} 

protected Dialog onCreateDialog(int id) { 


    switch (id) { 
    case DATE_DIALOG_ID: 
     // set date picker as current date 
     return new DatePickerDialog(this, datePickerListener, 
        year, month,day); 

    case TIME_DIALOG_ID: 
     // set time picker as current time 
     return new TimePickerDialog(this, 
       timePickerListener, hour, minute,false); 
     } 
    return null; 



    } 

private DatePickerDialog.OnDateSetListener datePickerListener 
      = new DatePickerDialog.OnDateSetListener() { 

    // when dialog box is closed, below method will be called. 
    public void onDateSet(DatePicker view, int selectedYear, 
      int selectedMonth, int selectedDay) { 
     year = selectedYear; 
     month = selectedMonth; 
     day = selectedDay; 



     // set selected date into textview 
     tvDisplayDate.setText(new StringBuilder() 
     .append(day).append("-").append(month + 1).append("-").append(year) 
      .append(" ")); 

     Calendar c = new GregorianCalendar(); 
     c.set(Calendar.YEAR,year); 
     c.set(Calendar.MONTH, month); 
     c.set(Calendar.DAY_OF_MONTH, day); 
     c.add(Calendar.DAY_OF_MONTH,42); 
     dayA = c.get(Calendar.DAY_OF_MONTH); 
     monthA = c.get(Calendar.MONTH); 
     yearA = c.get(Calendar.YEAR); 

     tvDisplayDate2.setText(new StringBuilder() 
     .append(dayA).append("-").append(monthA + 1).append("-").append(yearA) 
      .append(" ")); 

     // set selected date into datepicker also 
     dpResult.init(year, month, day, null); 
    } 
    }; 


    private TimePickerDialog.OnTimeSetListener timePickerListener = 
      new TimePickerDialog.OnTimeSetListener() { 
     public void onTimeSet(TimePicker view, int selectedHour, 
       int selectedMinute) { 
      hour = selectedHour; 
      minute = selectedMinute; 

      // set current time into textview 
      tvDisplayTime.setText(new StringBuilder().append(pad(hour)) 
        .append(":").append(pad(minute))); 

      // set current time into timepicker 
      timepicker.setCurrentHour(hour); 
      timepicker.setCurrentMinute(minute); 

     } 
    }; 



private static String pad(int c) { 
    if (c >= 10) 
     return String.valueOf(c); 
    else 
     return "0" + String.valueOf(c); 
} 
} 
+0

謝謝,我在一個點上使用了一個switch語句,但無法讓它工作 – Joel

+0

@Joel我也建議你使用在文檔中,因爲某些方法在較新的api版本中被棄用 – Raghunandan

+0

好的,謝謝我會檢查它 – Joel