【问题标题】:Call requires API level 11 (current min is 8): new android.app.AlertDialog.Builder调用需要 API 级别 11(当前最低为 8):新的 android.app.AlertDialog.Builder
【发布时间】:2015-04-16 23:46:06
【问题描述】:

我在做什么:

  • 我在活动中使用这个
  • 我的活动扩展了 ActionBarActivity
  • 我的最小 sdk 在清单中是 8 个

我得到的错误是:

Call requires API level 11 (current min is 8): new android.app.AlertDialog.Builder

代码

public void openSettings(String custMsg){

        final AlertDialog.Builder alert = new AlertDialog.Builder(this,AlertDialog.THEME_DEVICE_DEFAULT_DARK);
        alert.setMessage(custMsg);
        alert.setCancelable(false);
        alert.setNegativeButton(getResources().getString(R.string.Cancel), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                dialog.dismiss();
                tryAgainId.setVisibility(View.VISIBLE);
            }
        });
        alert.setPositiveButton(getResources().getString(R.string.Ok), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                locationValidationDone=true;
                dialog.dismiss();
                startActivity(new Intent(Settings.ACTION_SETTINGS));
            }
        });

        alert.show();
    }

问题:

我该如何解决这个问题

【问题讨论】:

  • AlertDialog.Builder(context,theme) 在 API 级别 11 中添加,但最小 sdk 为 8。为避免此错误,请在方法前使用 AlertDialog.Builder(context) 或 @NewApi
  • @ρяσѕρєя K .........是的,修复! ...所以如果我需要像我所做的那样为对话框使用自定义样式...只有使用更高 API 的方式?
  • @Devrath : 请参阅How to change theme for AlertDialog 帖子以在 api 级别 11 之前设置自定义主题
  • @Devrath 你要选择一个答案吗?谢谢!

标签: android


【解决方案1】:

请看docs

您使用的构造函数需要 API 11。

public AlertDialog.Builder (Context context, int theme)

Added in API level 11
Constructor using a context and theme for this builder and the AlertDialog it creates. The actual theme that an AlertDialog uses is a private implementation, however you can here supply either the name of an attribute in the theme from which to get the dialog's style (such as alertDialogTheme or one of the constants AlertDialog.THEME_TRADITIONAL, AlertDialog.THEME_HOLO_DARK, or AlertDialog.THEME_HOLO_LIGHT.

您需要使用API​​ 1中添加的构造器:

public AlertDialog.Builder (Context context)

Added in API level 1
Constructor using a context for this builder and the AlertDialog it creates.

【讨论】:

  • 谁否决了这个答案?这直接来自android docs
  • 确实如此,但最好给出全面的解决方案
  • @ImanMarashi 你在说什么?我展示了旧的和新的api。我什至提供了文档。你有什么问题?
【解决方案2】:

用这种方法永久修复:

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static void showAlert(int paramInt, String title, Activity act,
        String message) {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
        AlertDialog.Builder localBuilder = new AlertDialog.Builder(act,
                R.style.CustomDialog);
        TextView messageText = new TextView(act);
        messageText.setText(message);
        localBuilder.setTitle(title);
        messageText
                .setTextColor(act.getResources().getColor(R.color.white));
        messageText.setGravity(Gravity.CENTER);
        messageText.setTextSize(18);
        messageText.setLineSpacing(1f, 1.5f);
        localBuilder.setView(messageText);
        localBuilder.setPositiveButton("Ok", null);
        messageText.setMovementMethod(new ScrollingMovementMethod());
        AlertDialog dialog = localBuilder.show();
        dialog.show();
    } else {
        Dialog dialog = new Dialog(act);
        dialog.setTitle(title);
        TextView messageText = new TextView(act);
        messageText.setText(message);
        dialog.setContentView(messageText);
        dialog.show();
    }

}

CustomDialog风格:

<style name="CustomDialog" parent="android:Theme.Dialog">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:scrollbars">vertical</item>
</style>

【讨论】:

    【解决方案3】:

    使用构造函数

    public AlertDialog.Builder(Context)
    

    而不是

    public AlertDialog.Builder(Context, int)
    

    styles.xml中声明对话主题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-17
      • 2012-09-14
      • 1970-01-01
      • 2016-12-20
      • 1970-01-01
      • 2015-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多