【问题标题】:App crashing when sending pending intent Alarm Manager发送待处理意图警报管理器时应用程序崩溃
【发布时间】:2016-10-01 23:31:51
【问题描述】:

理想情况下,我想在应用程序暂停时调用,但为了让它工作,我从按钮调用该方法。如果列表和项目设置为活动状态,我在 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 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 的方法 在 android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293) 在 android.view.View.performClick(View.java:4438) 在 android.view.View$PerformClick.run(View.java:18422) 在 android.os.Handler.handleCallback(Handler.java:733) 在 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(Native Method) 在 java.lang.reflect.Method.invoke(Method.java:515) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 在 dalvik.system.NativeStart.main(本机方法) 引起:java.lang.reflect.InvocationTargetException 在 java.lang.reflect.Method.invokeNative(Native Method) 在 java.lang.reflect.Method.invoke(Method.java:515) 在 android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 在 android.view.View.performClick(View.java:4438) 在 android.view.View$PerformClick.run(View.java:18422) 在 android.os.Handler.handleCallback(Handler.java:733) 在 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(Native Method) 在 java.lang.reflect.Method.invoke(Method.java:515) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 在 dalvik.system.NativeStart.main(本机方法) 引起:java.lang.NullPointerException 在 com.example.poods.remindmeandlogit.ReminderList.CheckAllActiveReminders(ReminderList.java:84) 在 java.lang.reflect.Method.invokeNative(Native Method) 在 java.lang.reflect.Method.invoke(Method.java:515) 在 android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 在 android.view.View.performClick(View.java:4438) 在 android.view.View$PerformClick.run(View.java:18422) 在 android.os.Handler.handleCallback(Handler.java:733) 在 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(Native Method) 在 java.lang.reflect.Method.invoke(Method.java:515) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 在 dalvik.system.NativeStart.main(本机方法)

【问题讨论】:

    标签: java android-studio android-intent alarmmanager


    【解决方案1】:

    Caused by: java.lang.NullPointerException at com.example.poods.remindmeandlogit.ReminderList.CheckAllActiveReminders(ReminderList.java:84)

    第 84 行是什么?,您正在那里访问一个空对象。 见What is a NullPointerException, and how do I fix it?

    【讨论】:

    • 原来我在 final Calendar cal= Calendar.getInstance(); 下再次调用了 cal (calendar) 的实例。因此,即使我将它设置为 TimesType 类的变量,它也会在创建时创建另一个临时校准,它正在调整。只需删除它工作的最后一个日历部分。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-11
    • 2020-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多