Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.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 cordova-上下文试图显示警报或toast时出错_Java_Android_Cordova - Fatal编程技术网

Java cordova-上下文试图显示警报或toast时出错

Java cordova-上下文试图显示警报或toast时出错,java,android,cordova,Java,Android,Cordova,在我的Cordova插件中,我试图显示toast或alert,但无法获取应用程序的上下文。在我的onTagWithRssiReceived()函数中(这是一个覆盖函数),我可以在Eclipse日志中显示我想要的所有值,但是当我尝试使用toast或警报时,我的应用程序会崩溃 我试图用以下内容设置我的上下文: Context context = this.cordova.getActivity().getApplicationContext(); 但我得到了一个错误:“科尔多瓦无法解决或不是一个领

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

我试图用以下内容设置我的上下文:

Context context = this.cordova.getActivity().getApplicationContext();
但我得到了一个错误:“科尔多瓦无法解决或不是一个领域。”

我还使用了:

Context c = ctx.getApplicationContext();
但是,即使Eclipse中没有可见的错误,它仍然会使我的程序崩溃

最后,我尝试了((活动)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();

}
}

我没有一个很好的答案,但是查看这个插件代码可能对您有用:tldr;有一个execute方法重载,该方法接受上下文,然后调用插件特定的方法。谢谢Dawson。因为它是一个PhoneGap插件,我正在使用javascript与之通信,所以我不确定在哪里调用execute方法。我认为电话鸿沟的魔力正在那里发生。因此,如果我重载它,我不知道将上下文作为附加参数传递到何处,也不知道如何实现重载方法。另外,感谢分享您的插件。不幸的是,当我尝试做一些像。。。这个.cordova.getActivity()-可能是因为我使用的是旧版本的cordova,但不完全确定。