gpt4 book ai didi

android - 为警报调用 Intent 时出现 IllegalStateException

转载 作者:行者123 更新时间:2023-11-29 17:00:00 24 4
gpt4 key购买 nike

我正在尝试使用 Intent 设置警报,但是当我尝试打开 Intent 时它因 IllegalStateException 而崩溃。

下面是单击按钮时打开 Intent 的代码:

public void guardarClick(View view) {
Intent i = new Intent(AlarmClock.ACTION_SET_ALARM);
i.putExtra(AlarmClock.EXTRA_MESSAGE, descriptionAlarm.getText().toString());
i.putExtra(AlarmClock.EXTRA_HOUR, datePicker.getHour());
i.putExtra(AlarmClock.EXTRA_MINUTES, datePicker.getMinute());
startActivityForResult(i, 1);
}

这是错误:

04-12 20:01:19.288 4559-4559/edu.lasalle.pprog2.ac3_E/AndroidRuntime: FATAL EXCEPTION: main
Process: edu.lasalle.pp2, PID: 4559
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:5637)
at android.view.View$PerformClick.run(View.java:22429)
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:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5637) 
at android.view.View$PerformClick.run(View.java:22429) 
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:6119) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
Caused by: java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.SET_ALARM cmp=com.android.deskclock/.HandleApiCalls (has extras) } from ProcessRecord{723e06e 4559:edu.lasalle.pprog2.ac3/u0a81} (pid=4559, uid=10081) requires com.android.alarm.permission.SET_ALARM
at android.os.Parcel.readException(Parcel.java:1684)
at android.os.Parcel.readException(Parcel.java:1637)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3101)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1518)
at android.app.Activity.startActivityForResult(Activity.java:4225)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
at android.app.Activity.startActivityForResult(Activity.java:4183)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
at edu.lasalle.pprog2.ac3.AlarmActivity.guardarClick(AlarmActivity.java:59)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:5637) 
at android.view.View$PerformClick.run(View.java:22429) 
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:6119) 
at java.lang.reflect.Method.invoke(Native Method)

我做错了什么?

最佳答案

  1. 您应该将权限 com.android.alarm.permission.SET_ALARM 添加到您的 AndroidManifest.xml 文件中。

    <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
  2. 使用 startActivity(i) 而不是 startActivityForResult(i, 1)

    Intent i = new Intent(AlarmClock.ACTION_SET_ALARM);
    i.putExtra(AlarmClock.EXTRA_MESSAGE, descriptionAlarm.getText().toString());
    i.putExtra(AlarmClock.EXTRA_HOUR, datePicker.getHour());
    i.putExtra(AlarmClock.EXTRA_MINUTES, datePicker.getMinute());
    startActivity(i);

关于android - 为警报调用 Intent 时出现 IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43376453/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com