调用需要 API 级别 11(当前最低为 8):新 android.app.AlertDialog.Builder
Call requires API level 11 (current min is 8): new android.app.AlertDialog.Builder
我在做什么:
- 我在 activity
中使用它
- 我的 activity 扩展了 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();
}
问题:
我该如何解决这个问题
请看docs:
您使用的构造函数需要 API11.
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.
需要使用API1中添加的构造函数:
public AlertDialog.Builder (Context context)
Added in API level 1
Constructor using a context for this builder and the AlertDialog it creates.
使用构造函数
public AlertDialog.Builder(Context)
而不是
public AlertDialog.Builder(Context, int)
在 styles.xml
中声明对话框的主题。
使用此方法永久修复:
@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>
我在做什么:
- 我在 activity 中使用它
- 我的 activity 扩展了 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();
}
问题:
我该如何解决这个问题
请看docs:
您使用的构造函数需要 API11.
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.
需要使用API1中添加的构造函数:
public AlertDialog.Builder (Context context)
Added in API level 1
Constructor using a context for this builder and the AlertDialog it creates.
使用构造函数
public AlertDialog.Builder(Context)
而不是
public AlertDialog.Builder(Context, int)
在 styles.xml
中声明对话框的主题。
使用此方法永久修复:
@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>