无法获取Alertdialog标题,分隔符和按钮ID(Nullpointer异常)

时间:2014-12-15 23:16:44

标签: android alertdialog android-alertdialog

我不明白为什么我会得到一个NPE ..我在另一个班级使用同样的东西而且它正在运作:

    public void showSettingsAlert(){
    AlertDialog.Builder alertdialog = new AlertDialog.Builder(mcontext);
    alertdialog.setTitle("Title");
    alertdialog.setIcon(R.drawable.ic_gps);
    alertdialog.setMessage("Message");

    /*
    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);
        }
    });

    alertdialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
    final AlertDialog d = alertdialog.show();
    int dividerId = d.getContext().getResources().getIdentifier("android:id/titleDivider", null, null);
    View divider = d.findViewById(dividerId);
    divider.setBackgroundColor(getResources().getColor(R.color.very_light_gray));

    int textViewId = d.getContext().getResources().getIdentifier("android:id/alertTitle", null, null);
    TextView tv = (TextView) d.findViewById(textViewId);
    tv.setTextColor(getResources().getColor(R.color.hot_orange));
    tv.setTypeface(Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/Roboto-Light.ttf"));

    Button b = d.getButton(DialogInterface.BUTTON_NEGATIVE);
    b.setTextColor(getResources().getColor(R.color.gray2));
    b.setBackgroundColor(getResources().getColor(R.color.snow_primary));

    Button b2 = d.getButton(DialogInterface.BUTTON_POSITIVE);
    b2.setTextColor(getResources().getColor(R.color.gray2));
    b2.setBackgroundColor(getResources().getColor(R.color.snow_primary));
    d.show();

正如您所看到的,在我尝试访问按钮,divier等之前,我调用了alerdialog.show()。 我有这个函数的类扩展activity,我在另一个扩展DialogFragment的类上使用相同的代码,它可以工作.. 感谢。

0 个答案:

没有答案