我想用String.xml中的字符串构建alertdialog
但是R.string.string1返回int.请告诉我在string.xml中获取字符串prestent的方法
这是我的代码..
但是R.string.string1返回int.请告诉我在string.xml中获取字符串prestent的方法
这是我的代码..
public static void showAlert(String alertMessage) { final AlertDialog.Builder builder = new AlertDialog.Builder(null); builder.setMessage(alertMessage); final AlertDialog alert = builder.create(); alert.show(); } showAlert(R.string.string1);
解决方法
使用getResources().getString从strings.xml获取字符串值:
String str=getResources().getString(R.string.internetNotAvailable); showAlert(str);
并且您还将null传递给AlertDialog.Builder以创建AlertDialog.请传递当前的Activity上下文而不是null,以创建AlertDialog:
AlertDialog.Builder builder = new AlertDialog.Builder(Your_Current_Activity.this);