理想情况下,我希望在应用程序暂停时调用,但为了使其工作,我从一个按钮调用该方法。我在ArrayList中有一个ArrayList,它有一个列表,如果列表和项目设置为活动状态,我想要发送到警报管理器。如果任何人都可以帮助我理解为什么这是失败的,我将不胜感激。发送待处理意图时应用程序崩溃报警管理器
这里是我的代码:
public class ReminderList extends AppCompatActivity {
public static ArrayList<ReminderType> AllReminders=new ArrayList<>();
public reminderCAdapter adapter;
public static ReminderType currentReminderType;
public static ItemType currentItemType;
public static TimeType currentTimesType;
Context context;
AlarmManager alarmMan;
PendingIntent pIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.context=this;
setContentView(R.layout.activity_reminder_list);
//generate list
try {
LoadData();
} catch (IOException | ClassNotFoundException | JSONException e) {
e.printStackTrace();
}
alarmMan=(AlarmManager) getSystemService(ALARM_SERVICE);
adapter=new reminderCAdapter(AllReminders,this);
//handle listview and assign adapter
ListView lView = (ListView)findViewById(R.id.listView);
lView.setAdapter(adapter);
}
//Iterates through all reminders to see if reminder is enabled and if item is enabled and has a time
public void CheckAllActiveReminders(View view){
//Go through each reminder
Log.e("1", "1");
for(int i=0;i<AllReminders.size();i++){
//if the reminder is active continue
Log.e("2", "1");
if(AllReminders.get(i).ReminderActive){
//Go through each item
Log.e("3", "1");
for (int j=0;j<AllReminders.get(i).Items.size();j++){
//if the item is active
Log.e("4", "1");
if(AllReminders.get(i).Items.get(j).ItemActive){
//Go through each timetype
Log.e("5", "1");
for (int h=0;h<AllReminders.get(i).Items.get(j).Times.size();h++){
Log.e("6", "1");
Intent rIntent=new Intent(this.context, Alarm_Receiver.class);
Log.e("7", "1");
pIntent=PendingIntent.getBroadcast(ReminderList.this,0, rIntent,PendingIntent.FLAG_UPDATE_CURRENT);
Log.e("8", AllReminders.get(i).ReminderName+", "+AllReminders.get(i).Items.get(j).ItemName+", "+AllReminders.get(i).Items.get(j).Times.get(h).displayTime);
alarmMan.set(AlarmManager.RTC_WAKEUP,AllReminders.get(i).Items.get(j).Times.get(h).cal.getTimeInMillis(),pIntent);
}
}else{
//If item is deactivated all times in it need to be cancelled
Log.e("Fail 2", "1");
}
}
}else{
//if reminder is not active all items in it need to be deactivated
Log.e("Fail 1", "1");
}
}
}
}
我已经能够证实,它使所有的方式,直到alarmMan.set因为日志显示它。这里的堆栈跟踪:
10-01 19:26:01.059 11560-11560/com.example.poods.remindmeandlogit E/8:提醒,项目,7:25 PM 10-01 19:26: 01.059 11560-11560/com.example.poods.remindmeandlogit E/AndroidRuntime:致命例外:main 进程:com.example.poods.remindmeandlogit,PID:11560 java.lang.IllegalStateException:无法执行android的方法:onClick at android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293) at android.view.View.performClick(View.java:4438) at android.view.View $ PerformClick.run(View.java:18422) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:136) 在android.app.ActivityThread.main(ActivityThread.java:5017) 在java.lang.reflect.Method.invokeNative(本机方法) 在java的。 lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main( ZygoteInit.java:595) (原生方法) 由于:java.lang.reflect.Method.invoke(java.lang.reflect.Method.invokeNative(Native Method) (java.lang.reflect.Method.invoke Method.java:515) at android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) at android.view.View.performClick(View.java:4438) at android.view。查看$ PerformClick.run(View.java:18422) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java .lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method) 引起:java.lang.NullPointerException at com.example.poods.remindmeandlogit.ReminderList.CheckAllActi veReminders(ReminderList。java:84) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener .onClick(AppCompatViewInflater.java:288) at android.view.View.performClick(View.java:4438) at android.view.View $ PerformClick.run(View.java:18422) at android.os.Handler .handleCallback(Handler.java:733) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:136) 在机器人。在java.lang.reflect.Method.invoke(Method.java:515) com.android上调用java.lang.reflect.Method.invokeNative(Native Method) .internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method)
事实证明我已经在最终的Calendar Cal = Calendar.getInstance();下调用cal(日历)的实例。所以,即使我已经将它设置为TimesType类的变量,它已经创建了另一个临时cal,它正在调整它。只需删除最终的日历部分它的工作。非常感谢! – Superdsg