以下代码用于设置自定义通知。我已设法设置我的变量,如日期和时间选择器等,但我按下按钮,以使一切进展似乎不起作用。我没有得到祝酒,活动似乎很紧张。
xml中的我的按钮...
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:id="@+id/button_set_notifications"
android:text="Set Your Notifications"
android:background="@color/green_A700"
android:textColor="@color/white"
android:padding="10dp"
android:textStyle="bold"
android:paddingBottom="16dp"
android:onClick="SetNotificationsButton"/>
我意识到它有很多代码,但是除此之外还要添加通知。
public class GrowAssistantActivity extends AppCompatActivity implements
TimePickerDialog.OnTimeSetListener,
DatePickerDialog.OnDateSetListener{
private EditText mTitleText;
private TextView mDateText, mTimeText, mRepeatText, mRepeatNoText, mRepeatTypeText;
//private FloatingActionButton mFAB1;
//private FloatingActionButton mFAB2;
private Calendar mCalendar;
private int mYear, mMonth, mHour, mMinute, mDay;
private long mRepeatTime;
private String mTitle;
private String mTime;
private String mDate;
private String mRepeat;
private String mRepeatNo;
private String mRepeatType;
private String mActive;
// Values for orientation change
private static final String KEY_TITLE = "title_key";
private static final String KEY_TIME = "time_key";
private static final String KEY_DATE = "date_key";
private static final String KEY_REPEAT = "repeat_key";
private static final String KEY_REPEAT_NO = "repeat_no_key";
private static final String KEY_REPEAT_TYPE = "repeat_type_key";
private static final String KEY_ACTIVE = "active_key";
// Constant values in milliseconds
private static final long milMinute = 60000L;
private static final long milHour = 3600000L;
private static final long milDay = 86400000L;
private static final long milWeek = 604800000L;
private static final long milMonth = 2592000000L;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grow_assistant_activity_layout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Grow Assistant");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// Initialize Views
mTitleText = (EditText) findViewById(R.id.reminder_title);
mDateText = (TextView) findViewById(R.id.set_date);
mTimeText = (TextView) findViewById(R.id.set_time);
mRepeatText = (TextView) findViewById(R.id.set_repeat);
mRepeatNoText = (TextView) findViewById(R.id.set_repeat_no);
mRepeatTypeText = (TextView) findViewById(R.id.set_repeat_type);
//mFAB1 = (FloatingActionButton) findViewById(R.id.starred1);
//mFAB2 = (FloatingActionButton) findViewById(R.id.starred2);
// Initialize default values
mActive = "true";
mRepeat = "true";
mRepeatNo = Integer.toString(1);
mRepeatType = "Hour";
mCalendar = Calendar.getInstance();
mHour = mCalendar.get(Calendar.HOUR_OF_DAY);
mMinute = mCalendar.get(Calendar.MINUTE);
mYear = mCalendar.get(Calendar.YEAR);
mMonth = mCalendar.get(Calendar.MONTH) + 1;
mDay = mCalendar.get(Calendar.DATE);
mDate = mDay + "/" + mMonth + "/" + mYear;
mTime = mHour + ":" + mMinute;
// Setup Reminder Title EditText
mTitleText.addTextChangedListener(new 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) {
mTitle = s.toString().trim();
mTitleText.setError(null);
}
@Override
public void afterTextChanged(Editable s) {}
});
// Setup TextViews using reminder values
mDateText.setText(mDate);
mTimeText.setText(mTime);
mRepeatNoText.setText(mRepeatNo);
mRepeatTypeText.setText(mRepeatType);
mRepeatText.setText("Every " + mRepeatNo + " " + mRepeatType + "(s)");
// To save state on device rotation
if (savedInstanceState != null) {
String savedTitle = savedInstanceState.getString(KEY_TITLE);
mTitleText.setText(savedTitle);
mTitle = savedTitle;
String savedTime = savedInstanceState.getString(KEY_TIME);
mTimeText.setText(savedTime);
mTime = savedTime;
String savedDate = savedInstanceState.getString(KEY_DATE);
mDateText.setText(savedDate);
mDate = savedDate;
String saveRepeat = savedInstanceState.getString(KEY_REPEAT);
mRepeatText.setText(saveRepeat);
mRepeat = saveRepeat;
String savedRepeatNo = savedInstanceState.getString(KEY_REPEAT_NO);
mRepeatNoText.setText(savedRepeatNo);
mRepeatNo = savedRepeatNo;
String savedRepeatType = savedInstanceState.getString(KEY_REPEAT_TYPE);
mRepeatTypeText.setText(savedRepeatType);
mRepeatType = savedRepeatType;
mActive = savedInstanceState.getString(KEY_ACTIVE);
}
/*/ Setup up active buttons
if (mActive.equals("false")) {
mFAB1.setVisibility(View.VISIBLE);
mFAB2.setVisibility(View.GONE);
} else if (mActive.equals("true")) {
mFAB1.setVisibility(View.GONE);
mFAB2.setVisibility(View.VISIBLE);
}/*/
}
// To save state on device rotation
@Override
protected void onSaveInstanceState (Bundle outState) {
super.onSaveInstanceState(outState);
outState.putCharSequence(KEY_TITLE, mTitleText.getText());
outState.putCharSequence(KEY_TIME, mTimeText.getText());
outState.putCharSequence(KEY_DATE, mDateText.getText());
outState.putCharSequence(KEY_REPEAT, mRepeatText.getText());
outState.putCharSequence(KEY_REPEAT_NO, mRepeatNoText.getText());
outState.putCharSequence(KEY_REPEAT_TYPE, mRepeatTypeText.getText());
outState.putCharSequence(KEY_ACTIVE, mActive);
}
// On clicking Time picker
public void setTime(View v){
Calendar now = Calendar.getInstance();
TimePickerDialog tpd = TimePickerDialog.newInstance(
this,
now.get(Calendar.HOUR_OF_DAY),
now.get(Calendar.MINUTE),
false
);
tpd.setThemeDark(false);
tpd.show(getFragmentManager(), "Timepickerdialog");
}
// On clicking Date picker
public void setDate(View v){
Calendar now = Calendar.getInstance();
DatePickerDialog dpd = DatePickerDialog.newInstance(
this,
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH)
);
dpd.show(getFragmentManager(), "Datepickerdialog");
}
// Obtain time from time picker
@Override
public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute) {
mHour = hourOfDay;
mMinute = minute;
if (minute < 10) {
mTime = hourOfDay + ":" + "0" + minute;
} else {
mTime = hourOfDay + ":" + minute;
}
mTimeText.setText(mTime);
}
// Obtain date from date picker
@Override
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
monthOfYear ++;
mDay = dayOfMonth;
mMonth = monthOfYear;
mYear = year;
mDate = dayOfMonth + "/" + monthOfYear + "/" + year;
mDateText.setText(mDate);
}
/*/ On clicking the active button
public void selectFab1(View v) {
mFAB1 = (FloatingActionButton) findViewById(R.id.starred1);
mFAB1.setVisibility(View.GONE);
mFAB2 = (FloatingActionButton) findViewById(R.id.starred2);
mFAB2.setVisibility(View.VISIBLE);
mActive = "true";
}/*/
/*/ On clicking the inactive button
public void selectFab2(View v) {
mFAB2 = (FloatingActionButton) findViewById(R.id.starred2);
mFAB2.setVisibility(View.GONE);
mFAB1 = (FloatingActionButton) findViewById(R.id.starred1);
mFAB1.setVisibility(View.VISIBLE);
mActive = "false";
}/*/
// On clicking the repeat switch
public void onSwitchRepeat(View view) {
boolean on = ((Switch) view).isChecked();
if (on) {
mRepeat = "true";
mRepeatText.setText("Every " + mRepeatNo + " " + mRepeatType + "(s)");
} else {
mRepeat = "false";
mRepeatText.setText(R.string.repeat_off);
}
}
// On clicking repeat type button
public void selectRepeatType(View v){
final String[] items = new String[5];
items[0] = "Every Minute";
items[1] = "Hourly";
items[2] = "Daily";
items[3] = "Weekly";
items[4] = "Monthly";
// Create List Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Type");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
mRepeatType = items[item];
mRepeatTypeText.setText(mRepeatType);
mRepeatText.setText("Every " + mRepeatNo + " " + mRepeatType + "(s)");
}
});
AlertDialog alert = builder.create();
alert.show();
}
// On clicking repeat interval button
public void setRepeatNo(View v){
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Enter Number");
// Create EditText box to input repeat number
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
alert.setView(input);
alert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
if (input.getText().toString().length() == 0) {
mRepeatNo = Integer.toString(1);
mRepeatNoText.setText(mRepeatNo);
mRepeatText.setText("Every " + mRepeatNo + " " + mRepeatType + "(s)");
}
else {
mRepeatNo = input.getText().toString().trim();
mRepeatNoText.setText(mRepeatNo);
mRepeatText.setText("Every " + mRepeatNo + " " + mRepeatType + "(s)");
}
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// do nothing
}
});
alert.show();
}
// On clicking the set notifications button
public void SetNotificationsButton(){
ReminderDatabase rb = new ReminderDatabase(this);
// Creating Reminder
int ID = rb.addReminder(new Reminder(mTitle, mDate, mTime, mRepeat, mRepeatNo, mRepeatType, mActive));
// Set up calender for creating the notification
mCalendar.set(Calendar.MONTH, --mMonth);
mCalendar.set(Calendar.YEAR, mYear);
mCalendar.set(Calendar.DAY_OF_MONTH, mDay);
mCalendar.set(Calendar.HOUR_OF_DAY, mHour);
mCalendar.set(Calendar.MINUTE, mMinute);
mCalendar.set(Calendar.SECOND, 0);
// Check repeat type
if (mRepeatType.equals("Minute")) {
mRepeatTime = Integer.parseInt(mRepeatNo) * milMinute;
} else if (mRepeatType.equals("Hour")) {
mRepeatTime = Integer.parseInt(mRepeatNo) * milHour;
} else if (mRepeatType.equals("Day")) {
mRepeatTime = Integer.parseInt(mRepeatNo) * milDay;
} else if (mRepeatType.equals("Week")) {
mRepeatTime = Integer.parseInt(mRepeatNo) * milWeek;
} else if (mRepeatType.equals("Month")) {
mRepeatTime = Integer.parseInt(mRepeatNo) * milMonth;
}
// Create a new notification
if (mActive.equals("true")) {
if (mRepeat.equals("true")) {
new AlarmReceiver().setRepeatAlarm(getApplicationContext(), mCalendar, ID, mRepeatTime);
} else if (mRepeat.equals("false")) {
new AlarmReceiver().setAlarm(getApplicationContext(), mCalendar, ID);
}
}
// Create toast to confirm new reminder
Toast.makeText(getApplicationContext(), "Saved",
Toast.LENGTH_SHORT).show();
onBackPressed();
}
// On pressing the back button
@Override
public void onBackPressed() {
super.onBackPressed();
}
}
我的logcat
FATAL EXCEPTION: main
Process: com.cannamaster.growassistant.mmj, PID: 32671
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5106)
at android.view.View$PerformClick.run(View.java:20329)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5912)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5106)
at android.view.View$PerformClick.run(View.java:20329)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5912)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
Caused by: java.lang.IllegalArgumentException: Component class com.cannamaster.growassistant.mmj.grow_assistant.BootReceiver does not exist in com.cannamaster.growassistant.mmj
at android.os.Parcel.readException(Parcel.java:1550)
at android.os.Parcel.readException(Parcel.java:1499)
at android.content.pm.IPackageManager$Stub$Proxy.setComponentEnabledSetting(IPackageManager.java:4116)
at android.app.ApplicationPackageManager.setComponentEnabledSetting(ApplicationPackageManager.java:1967)
at com.cannamaster.growassistant.mmj.grow_assistant.AlarmReceiver.setRepeatAlarm(AlarmReceiver.java:120)
at com.cannamaster.growassistant.mmj.grow_assistant.GrowAssistantActivity.SetNotificationsButton(GrowAssistantActivity.java:341)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5106)
at android.view.View$PerformClick.run(View.java:20329)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5912)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
答案 0 :(得分:0)
更改您的方法语法如下:
public void SetNotificationsButton(View view){
// Method must have View Object as parameter.
}