描述:
我使用TimePickerDialog,当我点击我的Button时,发生了异常。
这是我的源代码:
private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute, int second) {
String theTime = null;
String thetimeStr = ((hourOfDay != 0) ? "%1$s" + " " + getString(R.string.hour_lowercase) + " " : "")
+ ((minute != 0) ? "%2$s" + " " + getString(R.string.minute_lowercase) + " " : "")
+ ((second != 0) ? "%3$s" + " " + getString(R.string.second_lowercase) : "");
theTime = String.format(thetimeStr, hourOfDay, minute, second);
String intervalInfo = mUploadIntervalTextView.getText() + theTime;
mUploadIntervalTextView.setText(String.valueOf(intervalInfo));
if ((hourOfDay * 3600 + minute * 60 + second) != 0) {
mSharedPreference.edit().putInt(ServerMonitorPlus.UPLOAD_ERROR_DATA_INTERVAL,
(hourOfDay * 3600 + minute * 60 + second)).apply();
} else {
Toast.makeText(ReportServerConfigurationActivity.this,
getResources().getString(R.string.interval_error),
Toast.LENGTH_SHORT).show();
}
}
};
mPickTimeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int interval = mSharedPreference.getInt(ServerMonitorPlus.UPLOAD_ERROR_DATA_INTERVAL,300);
int hour = interval / 3600;
int minute = interval % 3600 / 60;
int second = interval % 3600 % 60;
TimePickerDialog timePickerDialog = new TimePickerDialog(ReportServerConfigurationActivity.this,
mTimeSetListener, hour, minute,second, true);
timePickerDialog.show();
}
});
这是我的xml布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/last_upload_textView"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:textSize="15sp"
android:hint="@string/ip_address"
android:textColor="@color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/upload_error_info_interval"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:text="@string/upload_interval_seconds"
/>
<Button
android:id="@+id/pick_time_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/ip_address_editText"
android:paddingLeft="5dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/domain_name_edit" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/is_start_upload_error_checkBox"
android:layout_marginLeft="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/enable_upload_error_info"/>
<Button
android:id="@+id/test_btn"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/test"/>
</LinearLayout>
当我点击我的按钮时,我遇到了以下问题
以下是错误详情:
android.view.InflateException: Binary XML file line #20: Binary XML file line #20: Error inflating class com.luckyxmobile.servermonitorplus.timepicker.TimePicker
Caused by: android.view.InflateException: Binary XML file line #20: Error inflating class com.luckyxmobile.servermonitorplus.timepicker.TimePicker
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
at android.view.LayoutInflater.createView(LayoutInflater.java:645)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at com.luckyxmobile.servermonitorplus.timepicker.TimePickerDialog.<init>(TimePickerDialog.java:109)
at com.luckyxmobile.servermonitorplus.timepicker.TimePickerDialog.<init>(TimePickerDialog.java:79)
at com.luckyxmobile.servermonitorplus.activity.ReportServerConfigurationActivity$3.onClick(ReportServerConfigurationActivity.java:134)
at android.view.View.performClick(View.java:5609)
at android.view.View$PerformClick.run(View.java:22262)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: android.view.InflateException: Binary XML file line #34: Binary XML file line #34: Error inflating class com.luckyxmobile.servermonitorplus.numberpicker.NumberPicker
Caused by: android.view.InflateException: Binary XML file line #34: Error inflating class com.luckyxmobile.servermonitorplus.numberpicker.NumberPicker
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
at android.view.LayoutInflater.createView(LayoutInflater.java:645)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at com.luckyxmobile.servermonitorplus.timepicker.TimePicker.<init>(TimePicker.java:159)
at com.luckyxmobile.servermonitorplus.timepicker.TimePicker.<init>(TimePicker.java:140)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
at android.view.LayoutInflater.createView(LayoutInflater.java:645)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at com.luckyxmobile.servermonitorplus.timepicker.TimePickerDialog.<init>(TimePickerDialog.java:109)
at com.luckyxmobile.servermonitorplus.timepicker.TimePickerDialog.<init>(TimePickerDialog.java:79)
at android.view.View.performClick(View.java:5609)
at android.view.View$PerformClick.run(View.java:22262)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:190)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2094)
at android.content.res.Resources.getLayout(Resources.java:1111)
at android.view.LayoutInflater.inflate(LayoutInflater.java:424)
at com.luckyxmobile.servermonitorplus.numberpicker.NumberPicker.<init>(NumberPicker.java:637)
at com.luckyxmobile.servermonitorplus.numberpicker.NumberPicker.<init>(NumberPicker.java:562)
有我的活动源代码:
public class ReportServerConfigurationActivity extends AppCompatActivity {
private ServerMonitorPlus mServerMonitor;
private EditText mIpAddressEditText;
private Button mTestBtn,mPickTimeBtn;
private CheckBox mIsUploadErrorCheckBox;
private TextView mReportTimeTextView,mUploadIntervalTextView;
private SharedPreferences mSharedPreference;
private Retrofit mRetrofit;
private UpLoadErrorInfoService upLoadErrorInfoService;
private MenuItem miActionProgressItem;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.report_server_activity);
findViews();
initValues();
setListeners();
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.progress_action, menu);
// Store instance of the menu item containing progress
miActionProgressItem = menu.findItem(R.id.miActionProgress);
// Extract the action-view from the menu item
// Return to finish
return super.onPrepareOptionsMenu(menu);
}
private void initValues() {
mSharedPreference = getSharedPreferences(ServerMonitorPlus.SEND_ERROR_PREF, 0);
}
private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute, int second) {
String theTime = null;
String theTimeStr = ((hourOfDay != 0) ? "%1$s" + " " + getString(R.string.hour_lowercase) + " " : "")
+ ((minute != 0) ? "%2$s" + " " + getString(R.string.minute_lowercase) + " " : "")
+ ((second != 0) ? "%3$s" + " " + getString(R.string.second_lowercase) : "");
theTime = String.format(theTimeStr, hourOfDay, minute, second);
String intervalInfo = mUploadIntervalTextView.getText() + theTime;
mUploadIntervalTextView.setText(String.valueOf(intervalInfo));
if ((hourOfDay * 3600 + minute * 60 + second) != 0) {
mSharedPreference.edit().putInt(ServerMonitorPlus.UPLOAD_ERROR_DATA_INTERVAL,
(hourOfDay * 3600 + minute * 60 + second)).apply();
} else {
Toast.makeText(ReportServerConfigurationActivity.this,
getResources().getString(R.string.interval_error),
Toast.LENGTH_SHORT).show();
}
}
};
private void setListeners() {
mIpAddressEditText.addTextChangedListener(new IpWatcher());
mTestBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
testServer(true);
}
});
mPickTimeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int interval = mSharedPreference.getInt(ServerMonitorPlus.UPLOAD_ERROR_DATA_INTERVAL,300);
int hour = interval / 3600;
int minute = interval % 3600 / 60;
int second = interval % 3600 % 60;
TimePickerDialog timePickerDialog = new TimePickerDialog(ReportServerConfigurationActivity.this,
mTimeSetListener, hour, minute,second, true);
timePickerDialog.show();
}
});
mIsUploadErrorCheckBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isChecked = mIsUploadErrorCheckBox.isChecked();
Log.d("DEBUG","isChecked"+isChecked);
if (isChecked) {
testServer(false);
boolean ipOK = mSharedPreference.getBoolean(ServerMonitorPlus.IS_TEST_IP_OK, false);
if (ipOK) {
long currentMillis = System.currentTimeMillis();
int interval = mSharedPreference.getInt(ServerMonitorPlus.UPLOAD_ERROR_DATA_INTERVAL, 300);
Log.d("DEBUG","is-permitted-to-upload"+mSharedPreference.getBoolean(ServerMonitorPlus.IS_PERMITTED_TO_UPLOAD,false));
mSharedPreference.edit().putLong(TIME_OF_START_UPLOAD_ERROR, currentMillis - interval * 1000).apply();
mSharedPreference.edit().putLong(TIME_OF_END_UPLOAD_ERROR, currentMillis).apply();
Intent intent = new Intent(ReportServerConfigurationActivity.this, SendErrorService.class);
startService(intent);
}else {
}
} else {
// 取消上传
Log.i("调试", "onClick-no checked");
mSharedPreference.edit().putBoolean(ServerMonitorPlus.IS_PERMITTED_TO_UPLOAD, false).apply();
}
}
});
}
private void testServer(final boolean isTestOnly) {
hideKeyboard();
final String url = mIpAddressEditText.getText().toString();
mRetrofit = new Retrofit.Builder()
.baseUrl(urlHeader + url + testUrlTail)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
upLoadErrorInfoService = mRetrofit.create(UpLoadErrorInfoService.class);
if (IsInternet.checkNetwork(ReportServerConfigurationActivity.this)) {
upLoadErrorInfoService.loadRepo()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<TestIpInfo>() {
@Override
public void onStart() {
super.onStart();
miActionProgressItem.setVisible(true);
}
@Override
public void onCompleted() {
miActionProgressItem.setVisible(false);
}
@Override
public void onError(Throwable e) {
miActionProgressItem.setVisible(false);
DialogAdapter adapter = new DialogAdapter(ReportServerConfigurationActivity.this);
adapter.createDialog(getString(R.string.server_configuration_is_unavailable));
}
@Override
public void onNext(TestIpInfo testIpInfo) {
if (testIpInfo != null ) {
if(isTestOnly){ //如果仅仅是测试的话就弹出对话框提示测试成功
DialogAdapter adapter = new DialogAdapter(ReportServerConfigurationActivity.this);
adapter.createDialog(getString(R.string.server_configuration_is_ok));
}else { //如果是上传的话再保存
mSharedPreference.edit().putBoolean(ServerMonitorPlus.IS_PERMITTED_TO_UPLOAD,true).apply();
}
mSharedPreference.edit().putBoolean(ServerMonitorPlus.IS_TEST_IP_OK,true).apply();
mSharedPreference.edit().putString(ServerMonitorPlus.IP_ADDRESS,url).apply();
}
}
});
}
}
private void findViews() {
mIpAddressEditText = (EditText) findViewById(R.id.ip_address_editText);
mTestBtn = (Button) findViewById(R.id.test_btn);
mIsUploadErrorCheckBox = (CheckBox) findViewById(R.id.is_start_upload_error_checkBox);
mServerMonitor = (ServerMonitorPlus) getApplicationContext();
mReportTimeTextView = (TextView) findViewById(R.id.last_upload_textView);
mUploadIntervalTextView = (TextView) findViewById(R.id.upload_error_info_interval);
mPickTimeBtn = (Button) findViewById(R.id.pick_time_btn);
}
private class IpWatcher implements TextWatcher {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
}
public void hideKeyboard() {
View view = getCurrentFocus();
if (view != null) {
((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).
hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
@Override
public void onPause() {
super.onPause();
MobclickAgent.onPageEnd("ReportServerConfigurationActivity");
}
@Override
public void onResume() {
super.onResume();
MobclickAgent.onPageStart("ReportServerConfigurationActivity");
long lastUploadTime = mSharedPreference.getLong(TIME_OF_END_UPLOAD_ERROR, -1);
String currentTime;
if(lastUploadTime == -1){
currentTime = TimeFormatter.getLocalTime(System.currentTimeMillis(), this, "yyyy-mm-dd HH:mm");
}else {
currentTime = TimeFormatter.getLocalTime(lastUploadTime, this, "yyyy-mm-dd HH:mm");
}
mReportTimeTextView.setText(getString(R.string.last_report_time) + " " + currentTime);
Log.d("DEBUG","currentTime"+currentTime);
boolean isUploadPermitted = mSharedPreference.getBoolean(ServerMonitorPlus.IS_PERMITTED_TO_UPLOAD, false);
Log.d("DEBUG","isUploadPermitted"+isUploadPermitted);
if (isUploadPermitted) {
mIsUploadErrorCheckBox.setChecked(true);
} else {
mIsUploadErrorCheckBox.setChecked(false);
}
String ipAddress = mSharedPreference.getString(ServerMonitorPlus.IP_ADDRESS, "");
Log.d("DEBUG","ipAddress"+ipAddress);
mIpAddressEditText.setText(ipAddress);
}}
这是我的TimePickerDialog
public class TimePickerDialog extends AlertDialog
implements OnClickListener, OnTimeChangedListener {
public interface OnTimeSetListener {
void onTimeSet(TimePicker view, int hourOfDay, int minute,int second);
}
private static final String HOUR = "hour";
private static final String MINUTE = "minute";
private static final String SECOND = "second";
private static final String IS_24_HOUR = "is24hour";
private final TimePicker mTimePicker;
private final OnTimeSetListener mCallback;
int mInitialHourOfDay;
int mInitialMinute;
int mInitialSecond;
boolean mIs24HourView;
/**
* @param context Parent.
* @param callBack How parent is notified.
* @param hourOfDay The initial hour.
* @param minute The initial minute.
* @param is24HourView Whether this is a 24 hour view, or AM/PM.
*/
public TimePickerDialog(Context context,
OnTimeSetListener callBack,
int hourOfDay, int minute, int second, boolean is24HourView) {
this(context, Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB ? R.style.Theme_Dialog_Alert : 0, callBack, hourOfDay, minute, second, is24HourView);
}
/**
* @param context Parent.
* @param theme the theme to apply to this dialog
* @param callBack How parent is notified.
* @param hourOfDay The initial hour.
* @param minute The initial minute.
* @param is24HourView Whether this is a 24 hour view, or AM/PM.
*/
public TimePickerDialog(Context context,
int theme,
OnTimeSetListener callBack,
int hourOfDay, int minute, int second, boolean is24HourView) {
super(context, theme);
mCallback = callBack;
mInitialHourOfDay = hourOfDay;
mInitialMinute = minute;
mInitialSecond = second;
mIs24HourView = is24HourView;
setIcon(0);
setTitle(R.string.time_picker_dialog_title);
Context themeContext = getContext();
setButton(BUTTON_POSITIVE, themeContext.getText(R.string.date_time_done), this);
LayoutInflater inflater =
(LayoutInflater) themeContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.time_picker_dialog, null);
setView(view);
mTimePicker = (TimePicker) view.findViewById(R.id.timePicker);
// initialize state
mTimePicker.setIs24HourView(mIs24HourView);
mTimePicker.setCurrentHour(mInitialHourOfDay);
mTimePicker.setCurrentMinute(mInitialMinute);
mTimePicker.setCurrentSecond(mInitialSecond);
mTimePicker.setOnTimeChangedListener(this);
}
public void onClick(DialogInterface dialog, int which) {
tryNotifyTimeSet();
}
public void updateTime(int hourOfDay, int minutOfHour, int secondofminute) {
mTimePicker.setCurrentHour(hourOfDay);
mTimePicker.setCurrentMinute(minutOfHour);
mTimePicker.setCurrentSecond(secondofminute);
}
public void onTimeChanged(TimePicker view, int hourOfDay, int minute, int second) {
/* do nothing */
}
private void tryNotifyTimeSet() {
if (mCallback != null) {
mTimePicker.clearFocus();
mCallback.onTimeSet(mTimePicker, mTimePicker.getCurrentHour(),
mTimePicker.getCurrentMinute(),mTimePicker.getCurrentSecond());
}
}
@Override
protected void onStop() {
tryNotifyTimeSet();
super.onStop();
}
@Override
public Bundle onSaveInstanceState() {
Bundle state = super.onSaveInstanceState();
state.putInt(HOUR, mTimePicker.getCurrentHour());
state.putInt(MINUTE, mTimePicker.getCurrentMinute());
state.putInt(SECOND,mTimePicker.getCurrentSecond());
state.putBoolean(IS_24_HOUR, mTimePicker.is24HourView());
return state;
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
int hour = savedInstanceState.getInt(HOUR);
int minute = savedInstanceState.getInt(MINUTE);
int second = savedInstanceState.getInt(SECOND);
mTimePicker.setIs24HourView(savedInstanceState.getBoolean(IS_24_HOUR));
mTimePicker.setCurrentHour(hour);
mTimePicker.setCurrentMinute(minute);
mTimePicker.setCurrentSecond(second);
}}