1

我有我的应用程序,以便用户输入日期(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;
    }

}

当我在我的安卓手机上使用它作为模拟器时,它什么也没做。没有错误,但也没有添加任何内容。我可以整天按提交,但它只是坐着。

请协助!

谢谢!

我在按钮上添加了一个 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);
4

1 回答 1

1

您是否在按钮中声明了 OnClick?

<Button
        android:id="@+id/buttonSubmit"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:onClick="ButtonOnClick"
        android:text="Calculate (Minimum Payment)" />
于 2013-11-02T21:49:06.413 回答