ユーザーが日付 (yyyy/MM/dd) を入力できるようにアプリを用意しています。ユーザーがカレンダーにイベントを追加する場合は、チェックボックスを使用できます。これはローンの支払い(毎月と仮定)のためのものなので、ボックスがマークされていることを確認したい:
if(addCalander.isChecked())
宣言は次のとおりです。
CheckBox addCalander = (CheckBox) findViewById(R.id.checkBox1);
ユーザーがレイアウトの下部にある [送信] ボックスをクリックしたら、アクティビティでさまざまなフィールドからデータを収集し、チェックボックス (上記) を確認し、チェックされていると仮定して、毎月の定期的なイベントをカレンダーに追加します。ローンの基本期間 (後でプログラムしますが、次のように仮定します。
int debtTerm = 60; //60 months
以下は私の完全な.javaです::
public void ButtonOnClick(View v)
{
switch (v.getId()) {
case R.id.buttonSubmit:
TextView textDebtName = (TextView) findViewById(R.id.textDebtName);
String debtName = textDebtName.getText().toString();
TextView textDebtAmount = (TextView) findViewById(R.id.textDebtAmount);
TextView textDebtRate = (TextView) findViewById(R.id.textDebtRate);
TextView textDebtPayment = (TextView) findViewById(R.id.textDebtPayment);
TextView textDebtDate = (TextView) findViewById(R.id.textDebtDate);
EditText editDebtName = (EditText) findViewById(R.id.dispDebtName);
EditText editDebtAmount = (EditText) findViewById(R.id.dispDebtAmount);
EditText editDebtRate = (EditText) findViewById(R.id.dispDebtRate);
EditText editDebtPayment = (EditText) findViewById(R.id.dispDebtPayment);
EditText editDebtDate = (EditText) findViewById(R.id.dispDebtDate);
CheckBox addCalander = (CheckBox) findViewById(R.id.checkBox1);
String CalanderStr = addCalander.getText().toString();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
try {
Date startDate = dateFormat.parse("string");
} catch (ParseException e) {
e.printStackTrace();
}
if(addCalander.isChecked())
{
Uri eventsUri;
if (android.os.Build.VERSION.SDK_INT <= 7) {
eventsUri = Uri.parse("content://calendar/events");
} else {
eventsUri = Uri.parse("content://com.android.calendar/events");
}
Calendar cal = Calendar.getInstance();
ContentValues event = new ContentValues();
event.put("calendar_id", 1);
event.put("title", debtName);
event.put("description", "Loan Payment Due");
event.put("eventLocation", "My Lender");
event.put("dtstart",CalanderStr);
event.put("rrule", "FREQ=MONTHLY;WKST=SU;BYDAY=SA");
event.put("allDay", 1); // 0 for false, 1 for true
event.put("eventStatus", 1);
event.put("hasAlarm", 1); // 0 for false, 1 for true
event.put("duration","P90000S");
Uri url = getContentResolver().insert(eventsUri, event);
}
break;
}
}
これをAndroidフォンでエミュレーターとして使用すると、何もしません。エラーはありませんが、何も追加されていません。私は一日中送信を押すことができますが、座っているだけです。
手伝ってください!
ありがとう!
ボタンに OnClick を追加して、少なくともスクリプトを読み取れるようにしました。以下は、現在受け取っているエラーの LogCat です。
11-02 15:28:10.450: W/IInputConnectionWrapper(2597): showStatusIcon on inactive InputConnection
11-02 15:49:36.841: W/System.err(2772): java.text.ParseException: Unparseable date: string
11-02 15:49:36.841: W/System.err(2772): at java.text.DateFormat.parse(DateFormat.java:645)
11-02 15:49:36.841: W/System.err(2772): at biz.midl.debtsnowball.AddDebt.ButtonOnClick(AddDebt.java:52)
11-02 15:49:36.841: W/System.err(2772): at java.lang.reflect.Method.invokeNative(Native Method)
11-02 15:49:36.841: W/System.err(2772): at java.lang.reflect.Method.invoke(Method.java:521)
11-02 15:49:36.841: W/System.err(2772): at android.view.View$1.onClick(View.java:2067)
11-02 15:49:36.851: W/System.err(2772): at android.view.View.performClick(View.java:2411)
11-02 15:49:36.851: W/System.err(2772): at android.view.View$PerformClick.run(View.java:8819)
11-02 15:49:36.851: W/System.err(2772): at android.os.Handler.handleCallback(Handler.java:587)
11-02 15:49:36.851: W/System.err(2772): at android.os.Handler.dispatchMessage(Handler.java:92)
11-02 15:49:36.851: W/System.err(2772): at android.os.Looper.loop(Looper.java:123)
11-02 15:49:36.851: W/System.err(2772): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-02 15:49:36.851: W/System.err(2772): at java.lang.reflect.Method.invokeNative(Native Method)
11-02 15:49:36.851: W/System.err(2772): at java.lang.reflect.Method.invoke(Method.java:521)
11-02 15:49:36.851: W/System.err(2772): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-02 15:49:36.851: W/System.err(2772): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-02 15:49:36.851: W/System.err(2772): at dalvik.system.NativeStart.main(Native Method)
11-02 15:49:37.051: W/dalvikvm(2772): threadid=1: thread exiting with uncaught exception (group=0x40020b80)
11-02 15:49:37.131: E/AndroidRuntime(2772): FATAL EXCEPTION: main
11-02 15:49:37.131: E/AndroidRuntime(2772): java.lang.IllegalStateException: Could not execute method of the activity
11-02 15:49:37.131: E/AndroidRuntime(2772): at android.view.View$1.onClick(View.java:2072)
11-02 15:49:37.131: E/AndroidRuntime(2772): at android.view.View.performClick(View.java:2411)
11-02 15:49:37.131: E/AndroidRuntime(2772): at android.view.View$PerformClick.run(View.java:8819)
11-02 15:49:37.131: E/AndroidRuntime(2772): at android.os.Handler.handleCallback(Handler.java:587)
11-02 15:49:37.131: E/AndroidRuntime(2772): at android.os.Handler.dispatchMessage(Handler.java:92)
11-02 15:49:37.131: E/AndroidRuntime(2772): at android.os.Looper.loop(Looper.java:123)
11-02 15:49:37.131: E/AndroidRuntime(2772): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-02 15:49:37.131: E/AndroidRuntime(2772): at java.lang.reflect.Method.invokeNative(Native Method)
11-02 15:49:37.131: E/AndroidRuntime(2772): at java.lang.reflect.Method.invoke(Method.java:521)
11-02 15:49:37.131: E/AndroidRuntime(2772): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-02 15:49:37.131: E/AndroidRuntime(2772): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-02 15:49:37.131: E/AndroidRuntime(2772): at dalvik.system.NativeStart.main(Native Method)
11-02 15:49:37.131: E/AndroidRuntime(2772): Caused by: java.lang.reflect.InvocationTargetException
11-02 15:49:37.131: E/AndroidRuntime(2772): at biz.midl.debtsnowball.AddDebt.ButtonOnClick(AddDebt.java:79)
11-02 15:49:37.131: E/AndroidRuntime(2772): at java.lang.reflect.Method.invokeNative(Native Method)
11-02 15:49:37.131: E/AndroidRuntime(2772): at java.lang.reflect.Method.invoke(Method.java:521)
11-02 15:49:37.131: E/AndroidRuntime(2772): at android.view.View$1.onClick(View.java:2067)
11-02 15:49:37.131: E/AndroidRuntime(2772): ... 11 more
11-02 15:49:37.131: E/AndroidRuntime(2772): Caused by: java.lang.SecurityException: Permission Denial: writing com.android.providers.calendar.CalendarProvider2 uri content://com.android.calendar/events from pid=2772, uid=10046 requires android.permission.WRITE_CALENDAR
11-02 15:49:37.131: E/AndroidRuntime(2772): at android.os.Parcel.readException(Parcel.java:1247)
11-02 15:49:37.131: E/AndroidRuntime(2772): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:160)
11-02 15:49:37.131: E/AndroidRuntime(2772): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)
11-02 15:49:37.131: E/AndroidRuntime(2772): at android.content.ContentProviderProxy.insert(ContentProviderNative.java:421)
11-02 15:49:37.131: E/AndroidRuntime(2772): at android.content.ContentResolver.insert(ContentResolver.java:587)
11-02 15:49:37.131: E/AndroidRuntime(2772): ... 15 more
11-02 15:49:58.801: W/System.err(2792): java.text.ParseException: Unparseable date: string
11-02 15:49:58.811: W/System.err(2792): at java.text.DateFormat.parse(DateFormat.java:645)
11-02 15:49:58.811: W/System.err(2792): at biz.midl.debtsnowball.AddDebt.ButtonOnClick(AddDebt.java:52)
11-02 15:49:58.811: W/System.err(2792): at java.lang.reflect.Method.invokeNative(Native Method)
11-02 15:49:58.811: W/System.err(2792): at java.lang.reflect.Method.invoke(Method.java:521)
11-02 15:49:58.811: W/System.err(2792): at android.view.View$1.onClick(View.java:2067)
11-02 15:49:58.811: W/System.err(2792): at android.view.View.performClick(View.java:2411)
11-02 15:49:58.811: W/System.err(2792): at android.view.View$PerformClick.run(View.java:8819)
11-02 15:49:58.811: W/System.err(2792): at android.os.Handler.handleCallback(Handler.java:587)
11-02 15:49:58.811: W/System.err(2792): at android.os.Handler.dispatchMessage(Handler.java:92)
11-02 15:49:58.811: W/System.err(2792): at android.os.Looper.loop(Looper.java:123)
11-02 15:49:58.811: W/System.err(2792): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-02 15:49:58.811: W/System.err(2792): at java.lang.reflect.Method.invokeNative(Native Method)
11-02 15:49:58.811: W/System.err(2792): at java.lang.reflect.Method.invoke(Method.java:521)
11-02 15:49:58.811: W/System.err(2792): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-02 15:49:58.811: W/System.err(2792): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-02 15:49:58.811: W/System.err(2792): at dalvik.system.NativeStart.main(Native Method)
11-02 15:49:58.831: W/dalvikvm(2792): threadid=1: thread exiting with uncaught exception (group=0x40020b80)
11-02 15:49:58.841: E/AndroidRuntime(2792): FATAL EXCEPTION: main
11-02 15:49:58.841: E/AndroidRuntime(2792): java.lang.IllegalStateException: Could not execute method of the activity
11-02 15:49:58.841: E/AndroidRuntime(2792): at android.view.View$1.onClick(View.java:2072)
11-02 15:49:58.841: E/AndroidRuntime(2792): at android.view.View.performClick(View.java:2411)
11-02 15:49:58.841: E/AndroidRuntime(2792): at android.view.View$PerformClick.run(View.java:8819)
11-02 15:49:58.841: E/AndroidRuntime(2792): at android.os.Handler.handleCallback(Handler.java:587)
11-02 15:49:58.841: E/AndroidRuntime(2792): at android.os.Handler.dispatchMessage(Handler.java:92)
11-02 15:49:58.841: E/AndroidRuntime(2792): at android.os.Looper.loop(Looper.java:123)
11-02 15:49:58.841: E/AndroidRuntime(2792): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-02 15:49:58.841: E/AndroidRuntime(2792): at java.lang.reflect.Method.invokeNative(Native Method)
11-02 15:49:58.841: E/AndroidRuntime(2792): at java.lang.reflect.Method.invoke(Method.java:521)
11-02 15:49:58.841: E/AndroidRuntime(2792): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-02 15:49:58.841: E/AndroidRuntime(2792): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-02 15:49:58.841: E/AndroidRuntime(2792): at dalvik.system.NativeStart.main(Native Method)
11-02 15:49:58.841: E/AndroidRuntime(2792): Caused by: java.lang.reflect.InvocationTargetException
11-02 15:49:58.841: E/AndroidRuntime(2792): at biz.midl.debtsnowball.AddDebt.ButtonOnClick(AddDebt.java:79)
11-02 15:49:58.841: E/AndroidRuntime(2792): at java.lang.reflect.Method.invokeNative(Native Method)
11-02 15:49:58.841: E/AndroidRuntime(2792): at java.lang.reflect.Method.invoke(Method.java:521)
11-02 15:49:58.841: E/AndroidRuntime(2792): at android.view.View$1.onClick(View.java:2067)
11-02 15:49:58.841: E/AndroidRuntime(2792): ... 11 more
11-02 15:49:58.841: E/AndroidRuntime(2792): Caused by: java.lang.SecurityException: Permission Denial: writing com.android.providers.calendar.CalendarProvider2 uri content://com.android.calendar/events from pid=2792, uid=10046 requires android.permission.WRITE_CALENDAR
11-02 15:49:58.841: E/AndroidRuntime(2792): at android.os.Parcel.readException(Parcel.java:1247)
11-02 15:49:58.841: E/AndroidRuntime(2792): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:160)
11-02 15:49:58.841: E/AndroidRuntime(2792): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)
11-02 15:49:58.841: E/AndroidRuntime(2792): at android.content.ContentProviderProxy.insert(ContentProviderNative.java:421)
11-02 15:49:58.841: E/AndroidRuntime(2792): at android.content.ContentResolver.insert(ContentResolver.java:587)
11-02 15:49:58.841: E/AndroidRuntime(2792): ... 15 more
エラーは、スクリプトの次の部分にあるようです。
Uri url = getContentResolver().insert(eventsUri, event);