![](/img/trans.png)
[英]How to change the divider color of an arraylist in AlertDialog.builder
[英]failed to change the color of the header and divider of the alertdialog
我不明白为什么我要得到NPE ...这是我的代码:
public void showSettingsAlert(){
AlertDialog.Builder alertdialog = new AlertDialog.Builder(mcontext);
alertdialog.setTitle("Oups ! pas de données GPS");
alertdialog.setMessage("GPS n'est pas activé sur votre appareil, voulez vous l'activer ?");
/*
handling the buttons :
*/
alertdialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mcontext.startActivity(intent);
}
});
// on pressing cancel button
alertdialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
Dialog d = alertdialog.show();
int dividerId = d.getContext().getResources().getIdentifier("android:id/titleDivider", null, null);
int textViewId = d.getContext().getResources().getIdentifier("android:id/alertTitle", null, null);
View divider = d.findViewById(dividerId);
// the next line is where i get the NPE :
divider.setBackgroundColor(getResources().getColor(R.color.main_green));
TextView tv = (TextView) d.findViewById(textViewId);
tv.setTextColor(getResources().getColor(R.color.main_green));
alertdialog.show();
}
谢谢你的帮助 :)
PS:颜色main_green确实存在,我已经在其他地方使用它并且可以使用。
如果我没有记错,您应该在此处使用AlertDialog而不是Dialog:
Dialog d = alertdialog.show(); //Use AlertDialog instead
同样,您不需要调用show();
在方法结束时再次
编辑
我刚看过第一次读您的代码时就错过的东西。 请替换为:
AlertDialog d = alertdialog.show();
int dividerId = d.getContext().getResources().getIdentifier("android:id/titleDivider", null, null);
int textViewId = d.getContext().getResources().getIdentifier("android:id/alertTitle", null, null);
View divider = d.findViewById(dividerId);
有了这个:
AlertDialog d = alertdialog.show();
int dividerId = mcontext.getResources().getIdentifier("android:id/titleDivider", null, null);
int textViewId = mcontext.getResources().getIdentifier("android:id/alertTitle", null, null);
View divider = d.findViewById(dividerId);
dividerId为null,因为您提供了错误的Context
以获取该值。 或者,如果您的代码在“ Activity
,则只需删除mcontext部分并执行以下操作:
getResources().getIdentifier()... etc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.