这是我的活动类启动了警报管理器,按下我的片段上的按钮会出错!
活动 -
public class Schedule extends AppCompatActivity
{
CardView cardView = null;
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
ViewPagerAdapter viewPagerAdapter;
Context context;
private int[] tabIcons = {
R.drawable.ic_timeline_icon,
R.drawable.ic_schedule_icon,
R.drawable.ic_explore_icon
};
public Schedule() {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_schedule);
//initilize this variable
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
cardView = (CardView) findViewById(R.id.cv);
viewPager = (ViewPager) findViewById(R.id.viewpager);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPagerAdapter.addFragment(new Profile(), "PROFILE");
viewPagerAdapter.addFragment(new Scope(), "SCOPE");
viewPagerAdapter.addFragment(new Explore(), "EXPLORE");
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
this.context = this;
}
public void alarmHouse() {
Calendar cal = Calendar.getInstance();
Intent intent = new Intent(getApplicationContext(), Notification_receiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm_manager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarm_manager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
}`
//这是Alarm的Fragment类
public class Scope extends Fragment {
protected static Button txtDate, txtDateEnds, txtTimeIn, txtTimeOut;
Context context;
public Scope() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_scope, container, false);
txtDate = (Button) view.findViewById(R.id.btn_termBegins);
txtDateEnds = (Button) view.findViewById(R.id.btn_termEnds);
txtTimeIn = (Button) view.findViewById(R.id.time_in);
txtTimeOut = (Button) view.findViewById(R.id.time_out);
assert txtDate != null;
assert txtDateEnds != null;
assert txtTimeIn != null;
assert txtTimeOut != null;
termBegins();
termEnds();
periodTimeIn();
periodTimeOut();
return view;
}
public void termBegins() {
txtDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SectionEndsDate mDatePicker = new SectionEndsDate();
mDatePicker.show(getFragmentManager(), "Select date");
}
});
}
public void termEnds() {
txtDateEnds.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SectionBeginsDate mDatePicker = new SectionBeginsDate();
mDatePicker.show(getFragmentManager(), "Select date");
}
});
}
public static class SectionEndsDate extends DialogFragment implements DatePickerDialog.OnDateSetListener
{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar cal = Calendar.getInstance();
int year_x = cal.get(Calendar.YEAR);
int month_x = cal.get(Calendar.MONTH);
int day_x = cal.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, year_x, month_x, day_x);
}
public void onDateSet(DatePicker view, int year_x, int month_x, int day_x) {
txtDate.setText("Section Begins: " + String.valueOf(day_x) + " - " + String.valueOf(month_x + 1) + " - " + String.valueOf(year_x));
}
}
public static class SectionBeginsDate extends DialogFragment implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar cal = Calendar.getInstance();
int year_x = cal.get(Calendar.YEAR);
int month_x = cal.get(Calendar.MONTH);
int day_x = cal.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, year_x, month_x, day_x);
}
public void onDateSet(DatePicker view, int year_x, int month_x, int day_x) {
txtDateEnds.setText("Section Ends: " + String.valueOf(day_x) + " / " + String.valueOf(month_x + 1) + " / " + String.valueOf(year_x));
}
}
public void periodTimeIn() {
txtTimeIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Schedule schedule = new Schedule();
schedule.alarmHouse();
TimeInPicker mTimePicker = new TimeInPicker();
mTimePicker.show(getFragmentManager(), "Select time");
}
});
}
public void periodTimeOut() {
txtTimeOut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TimeOutPicker mTimePicker = new TimeOutPicker();
mTimePicker.show(getFragmentManager(), "Select time");
}
});
}
public static class TimeInPicker extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity()));
}
@Override
public void onTimeSet(android.widget.TimePicker view, int hourOfDay, int minute) {
txtTimeIn.setText("Time In: " +
String.valueOf((hourOfDay == 0 || hourOfDay == 12) ? 12 : hourOfDay % 12) + " : "
+ String.valueOf((minute < 10) ? "0" + minute : minute) + " " + (hourOfDay < 12 ? "AM" : "PM"));
}
}
public static class TimeOutPicker extends DialogFragment implements TimePickerDialog.OnTimeSetListener
{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity()));
}
@Override
public void onTimeSet(android.widget.TimePicker view, int hourOfDay, int minute) {
txtTimeOut.setText("Time Out: " +
String.valueOf((hourOfDay == 0 || hourOfDay == 12) ? 12 : hourOfDay % 12) + " : "
+ String.valueOf((minute < 10) ? "0" + minute : minute) + " " + (hourOfDay < 12 ? "AM" : "PM"));
}
}
答案 0 :(得分:0)
您正在创建Activity类的新实例,并尝试像普通Java类一样调用其方法。
Schedule schedule = new Schedule();
schedule.alarmHouse();
在这种情况下,您将没有在alarmHouse()
中设置闹钟所需的应用程序上下文。
您需要使用getActivity()
从您的片段中调用容器活动的任何公共方法。
所以它会像 -
`getActivity().alarmHouse();`