cordova - 上下文尝试显示警报或烤面包时出错

时间:2014-08-18 22:46:43

标签: java android cordova

在我的Cordova插件中,我尝试显示吐司或警报,但无法获取应用程序的上下文。在我的onTagWithRssiReceived()函数中 - 这是一个覆盖函数 - 我可以在Eclipse日志中显示我想要的所有值,但是当我尝试使用吐司或警报时,我的应用程序会崩溃。

我尝试用以下方式设置上下文:

Context context = this.cordova.getActivity().getApplicationContext();

但是我收到了错误:" cordova无法解析或者不是字段。"

我也用过:

Context c = ctx.getApplicationContext();

但即使Eclipse中没有可见的错误,它仍然会导致程序崩溃。

最后,我试过((Activity)ctx)并且它运行得很好。

这是我的Java代码:

public class HelloPlugin extends Plugin implements iRcpEvent2, OnCompletionListener, IOnHandlerMessage {

public static final String NATIVE_ACTION_STRING = "nativeAction";
public static final String SUCCESS_PARAMETER = "success";
public int maxTags = 1;
public int maxTime = 100;
public int repeatCycle = 0;

public PluginResult execute(String action, JSONArray dataArray,
        String callbackId) {

    if (NATIVE_ACTION_STRING.equals(action)) {

        String resultType = null;
        try {
            resultType = dataArray.getString(0);
        } catch (Exception ex) {
            Log.d("HelloPlugin", ex.toString());
        }

        if (resultType.equals(SUCCESS_PARAMETER)) {

            RcpApi2 rcpAPI = RcpApi2.getInstance();
            rcpAPI.setOnRcpEventListener(this);

            try {
                boolean t = rcpAPI.open();

                if (t = true) {
                    try {

                        boolean k = rcpAPI.startReadTagsWithRssi(maxTags,
                                maxTime, repeatCycle);

                        if (k = true) {     

                            return new PluginResult(PluginResult.Status.OK, k);

                        }
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        return new PluginResult(PluginResult.Status.ERROR,
                                "Context not set properly :(");
                    }
                } else {
                    return new PluginResult(PluginResult.Status.ERROR,
                            "Reader Not Opened :(");
                }

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        } else {
            return new PluginResult(PluginResult.Status.ERROR,
                    "Oops, Error :(");
        }
    }
    return null;
}

@Override
public void onTagWithRssiReceived(int[] arg0, int arg1) {
    // TODO Auto-generated method stub

    String epc = toHexString(arg0); 
    String p = Integer.toString(arg1);
    byte[] epcToByteArray = RcpLib.convertStringToByteArray(epc);

    int accessPassword = 00000000;
    int startAddress = 0;
    int targetLength = 0;

    RcpApi2 rcpAPI = RcpApi2.getInstance();

    boolean tid = rcpAPI.readFromTagMemory(
            Long.parseLong(Integer.toString(accessPassword), 16),
                RcpLib.convertStringToByteArray(epc),
                0,
                Integer.parseInt(Integer.toString(startAddress), 16),
                targetLength);

    Log.d("EPC:", epc);
    Log.d("EPCTOBYTEARRAY:", epcToByteArray.toString());
    Log.d("RETURNFROMTAGMEMORY:", Boolean.toString(tid));
    Log.d("RSSI:", p);

    AlertDialog.Builder builder1 = new AlertDialog.Builder(((Activity)ctx));
    builder1.setMessage("Write your message here.");
    AlertDialog alert11 = builder1.create();
    alert11.show();

}
}

0 个答案:

没有答案