无法创建cordova插件

时间:2013-05-22 00:53:50

标签: android cordova

我是Android开发的新手。我正在使用PhoneGap进行Android开发。我正在创建一个CalendarEvent插件并收到以下错误。我不知道为什么会出现这种错误。

05-21 22:39:12.171: W/dalvikvm(516): VFY: unable to resolve direct method 345: 
Landroid/webkit/WebView;. (Landroid/content/Context;Landroid/util/AttributeSet;IZ)V

这是javascript代码:

window.createEvent = function(title, location, description, startDate, endDate) {
    return cordova.exec(function(arg){}, function(error){}, 'CalendarPlugin', 'createEvent', 
           [title, location, description, startDate, endDate]);
};

var title = "Test Event";
var location = "Nashville, TN";
var description = "Very interesting event.";
var startDateTime = "2015-09-09T16:00:00-06:00"; // ISO 8601 date
var endDateTime = "2015-09-09T18:00:00-06:00";

window.createEvent(title, location, description, startDateTime, endDateTime);

这是插件代码:

package com.redobot.plugin;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;

import android.content.Intent;
import android.provider.CalendarContract.Events;

public class CalendarPlugin extends Plugin {
    final static String ISO8601DATEFORMAT = "yyyy-MM-dd'T'HH:mm:ssZ";

    @Override
    public PluginResult execute(String action, JSONArray args, final String callbackId) {
        if (action.equals("createEvent")) {
            try {
                this.createEvent(args.getString(0), args.getString(1), args.getString(2), args.getString(3), args.getString(4));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return new PluginResult(PluginResult.Status.OK, true );
        }
        return new PluginResult(PluginResult.Status.OK, false );
    }

    private void createEvent(String title, String location, String description, String startDate, String endDate){
        Calendar calendarStart = CalendarPlugin.getCalendarFromISO(startDate);
        Calendar calendarEnd = CalendarPlugin.getCalendarFromISO(endDate);

        Intent intent = new Intent(Intent.ACTION_EDIT);
        intent.setType("vnd.android.cursor.item/event");
        intent.putExtra(Events.TITLE, title);
        intent.putExtra(Events.EVENT_LOCATION, location);
        intent.putExtra(Events.DESCRIPTION, description);
        intent.putExtra("beginTime", calendarStart.getTimeInMillis());
        intent.putExtra("endTime", calendarEnd.getTimeInMillis());

        this.cordova.getActivity().startActivity(intent);
    }

    public static Calendar getCalendarFromISO(String dateString) {
        dateString = dateString.trim().replaceAll(":00$", "00"); // Changing format for SimpleDateFormat

        Calendar calendar = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
        SimpleDateFormat dateformat = new SimpleDateFormat(ISO8601DATEFORMAT, Locale.getDefault());

        try {
            Date date = dateformat.parse(dateString);
            calendar.setTime(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return calendar;
    }
}

请告诉我哪里错了。我搜索过,但无法获得任何有用的帮助。

1 个答案:

答案 0 :(得分:0)

我使用运行Android 4.2.2的Nexus 7设备通过创建Cordova 2.6应用并运行它来测试此代码,一切正常。我必须将它添加到我的config.xml:<plugin name="CalendarPlugin" value="com.redobot.plugin.CalendarPlugin" />并在onDeviceReady()检查中添加代码,但除此之外,没问题。