首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >调用需要API级别11 (当前最小值为8):新android.app.AlertDialog.Builder

调用需要API级别11 (当前最小值为8):新android.app.AlertDialog.Builder
EN

Stack Overflow用户
提问于 2015-01-06 15:32:18
回答 3查看 1K关注 0票数 1

我所做的是:

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

我得到的错误是:

代码语言:javascript
复制
Call requires API level 11 (current min is 8): new android.app.AlertDialog.Builder

代码

代码语言:javascript
复制
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();
    }

问题:

我如何解决这个问题?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-01-06 15:37:20

请看文档

您正在使用的构造函数需要API 11.

代码语言:javascript
复制
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:中添加的构造器

代码语言:javascript
复制
public AlertDialog.Builder (Context context)

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

Stack Overflow用户

发布于 2015-03-11 17:24:20

使用此方法永久修复以下内容:

代码语言:javascript
复制
@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风格:

代码语言:javascript
复制
<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>
票数 0
EN

Stack Overflow用户

发布于 2015-01-06 15:41:40

使用构造函数

代码语言:javascript
复制
public AlertDialog.Builder(Context)

而不是

代码语言:javascript
复制
public AlertDialog.Builder(Context, int)

styles.xml中声明对话框的主题。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27801884

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档