我正在尝试使用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)
我做错了什么?
答案 0 :(得分:3)
您应该在com.android.alarm.permission.SET_ALARM
文件中添加权限AndroidManifest.xml
。
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
使用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);