Interface not implemented (IceCreamSandwich 4.0.3)
I'm writing a small app to practice with the AlarmManager which should be usable in old Android phones. I think 4.0.3 IceCreamSandwich is low enough for my purposes.
The app is not complete yet, yet enough functionality is there to crash in real life. When I run the app on the emulator using the Nexus 5X (API 25) the app so far works well and the values from the timepicker are read and shown on screen. However, when I run it real life on my HTC One V (Android 4.0.3 API 15), the values are not read from the timepicker and I get the following error in the LogCat:
E/Handler: Failed to handle callback; interface not implemented,
callback:android.view.View$PerformClick@40e1cb80
java.lang.NoSuchMethodError: android.widget.TimePicker.getHour
The code I have is below. The OnClickListener calls display_time(), which fails when reading the hour and minute values from the timepicker.
package com.schalkx.alarm101;
import android.app.AlarmManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
AlarmManager alarm_manager;
TimePicker alarm_time_picker;
TextView update_text;.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize the alarm manager
alarm_manager = (AlarmManager) getSystemService(ALARM_SERVICE);
// Initialize the time picker
alarm_time_picker = (TimePicker) findViewById(R.id.timePicker);
// Initialize the update box
update_text = (TextView) findViewById(R.id.update_text);
// Create a calendar instance
final Calendar calendar = Calendar.getInstance();
// Initialize the start alarm button
Button alarm_on = (Button) findViewById(R.id.alarm_on);
// Implement OnClickListener
alarm_on.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Update the update_text
display_time();
}
});
}
private void set_alarm_text(String output) {
update_text.setText(output);
}
public void display_time() {
int hours;
int minutes;
String hours_string;
String minutes_string;
hours = alarm_time_picker.getHour(); // *** Error occurs here
minutes = alarm_time_picker.getMinute();
hours_string = String.valueOf(hours);
minutes_string = String.valueOf(minutes);
set_alarm_text(hours_string + ":" + minutes_string);
}
}
Any ideas why the error in 4.0.3? Thanks!
Kx
https://developer.android.com/reference/android/widget/TimePicker.html#getHour()
Added in API level 23.
Your HTC phone uses a lower level of the API.
Just switch to using getCurrentHour(), which while deprecated, will be available on both phones.
If you really want to go nuts, you can use the java.lang.Class object for TimePicker to check if the method getCurrentHour() exists using a method like getDeclaredMethod, https://developer.android.com/reference/java/lang/Class.html#getDeclaredMethod(java.lang.String, java.lang.Class...), but that's really not necessary in this case.
链接地址: http://www.djcxy.com/p/69062.html上一篇: C ++复合模板类工厂