Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java android日历事件问题_Java_Android_Android Calendar_Android Event - Fatal编程技术网

Java android日历事件问题

Java android日历事件问题,java,android,android-calendar,android-event,Java,Android,Android Calendar,Android Event,大家好,我是一个新手,正在学习android…我从 这篇文章提供了答案,只是代码。我希望有人帮助我解决我遇到的问题,首先是代码: onClickListenerEvent的代码如下: btnOne.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Calendar dateToShow = Calendar.getInstan

大家好,我是一个新手,正在学习android…我从

这篇文章提供了答案,只是代码。我希望有人帮助我解决我遇到的问题,首先是代码:

onClickListenerEvent的代码如下:

btnOne.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // Calendar dateToShow = Calendar.getInstance();
                // dateToShow.set(2013, Calendar.MAY, 10, 9, 0);
                //
                // showCalendarAtTime(dateToShow);
                Uri event1;
                long epoch, epoch1;
                Uri EVENTS_URI = Uri.parse(getCalendarUriBase(this) + "events");
                ContentResolver cr = getContentResolver();

                ContentValues values = new ContentValues();

                try 
                {
                    epoch = new java.text.SimpleDateFormat ("yyyy-MM-dd hh:mm a").parse(date+" "+time).getTime();
                    //epoch=epoch;
                    Log.e("epoch",String.valueOf(epoch));
                    epoch1 = new java.text.SimpleDateFormat ("yyyy-MM-dd hh:mm a").parse(date+" "+time).getTime();
                    //epoch1=epoch1;
                    Log.e("epoch1",String.valueOf(epoch1));
                } catch (ParseException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                values.put("calendar_id", 1);
                values.put("title", "Appoitment");
                values.put("allDay", 0);
                values.put("dtstart",epoch); // event starts at 11 minutes from now
                values.put("dtend", epoch1 ); // ends 60 minutes from now
                values.put("description", "Your consulting date and time ");
                values.put("visibility", 0);
                values.put("hasAlarm", 1);
                if(EVENTS_URI!=null){
                    event1 = cr.insert(EVENTS_URI, values);
                }

                // reminder insert
                Uri REMINDERS_URI = Uri.parse(getCalendarUriBase(this) + "reminders");
                values = new ContentValues();
                values.put( "event_id", Long.parseLong(event1.getLastPathSegment()));
                values.put( "method", 1 );
                values.put( "minutes", 10 );
                if(REMINDERS_URI!=null){   
                    cr.insert( REMINDERS_URI, values );
                }
                alertDialog.setTitle("Event Saved");
                Dismiss();
                alertDialog.show();
                }

                // reminder insert
                Uri REMINDERS_URI = Uri.parse(getCalendarUriBase(this)+ "reminders");
                values = new ContentValues();
                values.put("event_id", id);
                values.put("method", 1);
                values.put("minutes", 0);
                cr.insert(REMINDERS_URI, values);

            }

        });
getCalendarUriBase代码:

private String getCalendarUriBase(Activity act) {
String calendarUriBase = null;
Uri calendars = Uri.parse("content://calendar/calendars");
Cursor managedCursor = null;
try {
    managedCursor = act.managedQuery(calendars, null, null, null, null);
} catch (Exception e) {
}
if (managedCursor != null) {
    calendarUriBase = "content://calendar/";
} else {
    calendars = Uri.parse("content://com.android.calendar/calendars");
    try {
        managedCursor = act.managedQuery(calendars, null, null, null, null);
    } catch (Exception e) {
    }
    if (managedCursor != null) {
        calendarUriBase = "content://com.android.calendar/";
    }
}
return calendarUriBase;
}
我对上面的代码有问题,我在帖子中发现了一些答案,比如
event1
是一个URI类型的变量,但我有以下问题:

onClickListenerEvent()中的问题

  • 下面的代码在代码下面画了一条红线,出现了这个错误
  • “类型
    MainActivity
    中的方法
    getCalendarUriBase(Activity)
    不适用于参数
    (new View.OnClickListener(){})
    ”,代码为:

    Uri.parse(getCalendarUriBase(this)+ "reminders");
    
  • 下一行中的历元、日期和时间的返回类型是什么
  • epoch=new java.text.simpleDataFormat(“yyyy-MM-dd hh:MM a”).parse(date+“”+time).getTime()

  • 以下代码是什么?因为在eclipse中,它为
    alertDialog
    提供了8个可用的修复:第一个是“创建一个新变量”,如果是,是什么类型的变量,是什么?我是否需要它们来获取结果或在日历中添加事件

    alertDialog.setTitle(“事件已保存”)

    disclose()

    alertDialog.show()

  • 在eclipse中的以下行中,我收到了一条不推荐使用的警告:

    managedCursor=act.managedQuery(日历,null,null,null,null)

  • @Abhi或任何有经验的人能帮我吗?因为我是一个新手,安卓根本就不是用户


    谢谢。

    听起来好像发生了两件事:

    1) 对于示例代码的作用或含义,您有一些基本问题。这些是与Java相关的基本问题

    2) 您正在使用日历API(可能是API 14之前的版本?),这很复杂

    关于“onClickListenerEvent()”中的问题1:在调用getCalendarBaseUri()方法时,不能使用“this”作为对活动的引用,因为此时您处于匿名类中。通过在“this”关键字前面加上类名来限定该关键字。例如,“MyActivity.this”

    关于第二个问题,“getTime()”将返回一个long(c.f.)

    最后,API 11中不推荐使用Activity类上的“managedQuery()”方法。这对你来说可能重要,也可能不重要(取决于你的项目目标)

    总而言之,在我看来,日历代码比它需要的要复杂一点(可能是因为原始作者试图支持在api 14之前推送事件?)。如果您有机会瞄准API14+,请查看CalendarContract()