You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Fragment中显示自定义AlertDialog?解决构造器未定义错误

在Fragment中创建AlertDialog时构造器未定义的解决方法

你在名为MoviesFragFragmentActivity的Fragment中尝试显示自定义AlertDialog,但编译时报错:AlertDialog.Builder(MoviesFragFragmentActivity)构造器未定义。这段代码在Activity中可以正常运行,需要修改以适配Fragment环境。

原报错代码

final AlertDialog dialog1 = new AlertDialog.Builder(MoviesFragFragmentActivity.this).create();
View inflate = getLayoutInflater().inflate(R.layout.custom3,null); 
dialog1.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog1.setView(inflate);
TextView t1 = (TextView) inflate.findViewById(R.id.t1);

TextView t2 = (TextView) inflate.findViewById(R.id.t2);

TextView t3 = (TextView) inflate.findViewById(R.id.t3);

LinearLayout b1 = (LinearLayout) inflate.findViewById(R.id.b1);

ImageView i1 = (ImageView) inflate.findViewById(R.id.i1);

LinearLayout bg = (LinearLayout) inflate.findViewById(R.id.bg);
t1.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/en_medium.ttf"), 0);
t2.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/sansation_regular.ttf"), 0);
t3.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/sansation_regular.ttf"), 0);
i1.setImageResource(R.drawable.splash);
i1.getDrawable().setColorFilter(Color.parseColor("#008DCD"), PorterDuff.Mode.SRC_IN);
t1.setText("Oh,No...");
t2.setText("This feature is not available yet, try after new update ");
t3.setText("Try again");
_rippleRoundStroke(bg, "#FAFAFA", "#000000", 40, 0, "#000000");
_CardView(b1, 10, 100, "#008DCD", true);
b1.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ 
        // Default item selected on bottom navigation onCreate
        bottomnavigation4.getMenu().findItem(2).setChecked(true);
        SketchwareUtil.showMessage(getApplicationContext(), "Try again later");
        dialog1.dismiss();
    }
});
dialog1.setCancelable(false);
dialog1.show();

报错信息

ERROR in /storage/emulated/0/.sketchware/mysc/680/app/src/main/java/com/liquidapps/movieapp/MoviesFragFragmentActivity.java (at line 601)
final AlertDialog dialog1 = new AlertDialog.Builder(MoviesFragFragmentActivity.this).create();
The constructor AlertDialog.Builder(MoviesFragFragmentActivity) is undefined


问题原因

AlertDialog.Builder的构造器需要传入Context对象,但Fragment本身不是Context,直接传MoviesFragFragmentActivity.this(Fragment的实例)不符合参数要求。Activity中this本身就是Context,所以可以正常运行,Fragment必须获取关联的Activity作为Context。

修改步骤

  1. 替换AlertDialog.Builder的参数:用requireContext()(推荐,避免空指针)或getContext()替代MoviesFragFragmentActivity.this。
  2. 替换资源获取方法:getAssets()改为requireContext().getAssets(),getApplicationContext()改为requireContext()或requireContext().getApplicationContext()。

修正后的完整代码

final AlertDialog dialog1 = new AlertDialog.Builder(requireContext()).create();
View inflate = getLayoutInflater().inflate(R.layout.custom3, null); 
dialog1.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog1.setView(inflate);
TextView t1 = (TextView) inflate.findViewById(R.id.t1);

TextView t2 = (TextView) inflate.findViewById(R.id.t2);

TextView t3 = (TextView) inflate.findViewById(R.id.t3);

LinearLayout b1 = (LinearLayout) inflate.findViewById(R.id.b1);

ImageView i1 = (ImageView) inflate.findViewById(R.id.i1);

LinearLayout bg = (LinearLayout) inflate.findViewById(R.id.bg);
t1.setTypeface(Typeface.createFromAsset(requireContext().getAssets(),"fonts/en_medium.ttf"), 0);
t2.setTypeface(Typeface.createFromAsset(requireContext().getAssets(),"fonts/sansation_regular.ttf"), 0);
t3.setTypeface(Typeface.createFromAsset(requireContext().getAssets(),"fonts/sansation_regular.ttf"), 0);
i1.setImageResource(R.drawable.splash);
i1.getDrawable().setColorFilter(Color.parseColor("#008DCD"), PorterDuff.Mode.SRC_IN);
t1.setText("Oh,No...");
t2.setText("This feature is not available yet, try after new update ");
t3.setText("Try again");
_rippleRoundStroke(bg, "#FAFAFA", "#000000", 40, 0, "#000000");
_CardView(b1, 10, 100, "#008DCD", true);
b1.setOnClickListener(new View.OnClickListener(){ 
    public void onClick(View v){ 
        // Default item selected on bottom navigation onCreate
        bottomnavigation4.getMenu().findItem(2).setChecked(true);
        SketchwareUtil.showMessage(requireContext(), "Try again later");
        dialog1.dismiss();
    }
});
dialog1.setCancelable(false);
dialog1.show();

额外说明

  • requireContext()会返回Fragment关联的Activity的Context,若Fragment未附加到Activity会抛出异常,适合确保Fragment已处于活跃状态的场景。
  • 若需兼容Fragment未附加的情况,可使用getContext(),但需添加非空判断:if (getContext() != null) { ... }。

内容的提问来源于stack exchange,提问作者user17747424

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 08:45:46