无法解析方法' startActivity(android.content.Intent)'

时间:2016-06-15 17:04:04

标签: android

正如我所说,我得到一个错误,其中无法识别startActivity。这是代码:

AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle("Deal");
builder.setMessage("Hello");
builder.setPositiveButton("Call ME", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {

        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        callIntent.setData(Uri.parse("tel:" + "528543871"));
        startActivity(callIntent);

    }
});
builder.create();
builder.show();

3 个答案:

答案 0 :(得分:0)

您没有创建对话框。你正在创建一个构建器。 请尝试如下:

AlertDialog myDialog = new AlertDialog.Builder(this).create();
myDialog.setTitle("Deal");
myDialog.setMessage("Hello");
myDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Call me", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {

        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        callIntent.setData(Uri.parse("tel:" + "528543871"));
        startActivity(callIntent);

    }
})
myDialog.show();

答案 1 :(得分:0)

正如评论中的其他人已经指出的那样,方法startActivity需要从上下文context.startActivity()明确调用,或者如果您在活动startActivity()中调用,则需要Activity实现Context接口。在您的情况下,我认为这应该解决它(使用您已经拥有的mContext):

AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle("Deal");
builder.setMessage("Hello");
builder.setPositiveButton("Call ME", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

    Intent callIntent = new Intent(Intent.ACTION_CALL);
    callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    callIntent.setData(Uri.parse("tel:" + "528543871"));
    mContext.startActivity(callIntent);

}
});

builder.create();
builder.show();

答案 2 :(得分:0)

在上下文中调用方法startActivity()。当您将方法调用onClick()时,这是OnCLickListener的方法,因此该方法尚未解决。正如我在您的代码中看到的那样,您已在mContext构造函数中传递了AlertDialog.Builder,因此您也可以使用该Context变量来调用startActivity()方法。

AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle("Deal");
builder.setMessage("Hello");
builder.setPositiveButton("Call ME", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {

        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        callIntent.setData(Uri.parse("tel:" + "528543871"));
        mContext.startActivity(callIntent); //Correct this line

    }
});
builder.create();
builder.show();

OR

AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle("Deal");
builder.setMessage("Hello");
builder.setPositiveButton("Call ME", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {

        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        callIntent.setData(Uri.parse("tel:" + "528543871"));
        getActivity().startActivity(callIntent);

    }
});
builder.create();
builder.show();