android - AlertDialog.Builder错误 - “构造函数AlertDialog.Builder(HelloPlugin)未定义”

时间:2014-08-13 21:41:06

标签: android cordova alert

我正在尝试在android中显示一个警告框但新的AlertDialog.Builder(this)中的“this”给出错误,“构造函数AlertDialog.Builder(HelloPlugin)未定义”。有谁知道如何解决这个问题?

爪哇:

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

public static final String KEY_ENCODING = "my_encoding";
public static final String KEY_SAVELOG = "my_saveLog";
public static final String NATIVE_ACTION_STRING = "nativeAction";
public static final String SUCCESS_PARAMETER = "success";


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) {

                            AlertDialog.Builder builder = new AlertDialog.Builder(this);
                            builder.setTitle("Message")
                                .setNeutralButton("OK", null);
                            AlertDialog dialog = builder.create();
                            dialog.show();

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

                        }
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } 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;
}

2 个答案:

答案 0 :(得分:0)

Constractor必须得到Context

AlertDialog

尝试使用getApplicationContext()

或将Context传递给此类

答案 1 :(得分:0)

如果有人对此感到奇怪,这就是答案:

要获取Cordova插件的上下文,您可以使用以下代码:

((Activity)ctx)

例如,如果要创建警报构建器,请执行以下操作:

AlertDialog.Builder builder1 = new AlertDialog.Builder(((Activity)ctx));
builder1.setMessage("Test Alert");
AlertDialog alert11 = builder1.create();
alert11.show();

希望这有帮助!