简体   繁体   English

无法创建Cordova插件

[英]unable to create cordova plugin

I am new to Android development. 我是Android开发的新手。 I am using PhoneGap for Android development. 我正在使用PhoneGap进行Android开发。 I am creating a CalendarEvent plugin and getting the following error. 我正在创建一个CalendarEvent插件,并收到以下错误。 I don't know why this error occurs. 我不知道为什么会发生此错误。

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

This is the javascript code: 这是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);

And this is the plugin code: 这是插件代码:

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;
    }
}

Please tell where I am wrong. 请告诉我我错了。 I searched, but couldn't get any useful help. 我进行了搜索,但无法获得任何有用的帮助。

I tested this code with a Nexus 7 device running Android 4.2.2 by creating a Cordova 2.6 app and running it, and everything worked fine. 我通过运行Cordova 2.6应用程序并运行它,在运行Android 4.2.2的Nexus 7设备上测试了此代码,一切正常。 I had to add this to my config.xml: <plugin name="CalendarPlugin" value="com.redobot.plugin.CalendarPlugin" /> and add the code inside of an onDeviceReady() check, but other than that, no problem. 我必须将其添加到config.xml中: <plugin name="CalendarPlugin" value="com.redobot.plugin.CalendarPlugin" />并将代码添加到onDeviceReady()检查中,但是onDeviceReady() ,没有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM