Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 无法获取Alertdialog标题、分隔符和按钮ID(空指针异常)_Android_Android Alertdialog - Fatal编程技术网

Android 无法获取Alertdialog标题、分隔符和按钮ID(空指针异常)

Android 无法获取Alertdialog标题、分隔符和按钮ID(空指针异常),android,android-alertdialog,Android,Android Alertdialog,我不明白为什么我会得到一个NPE..我在使用同一个来上另一个课,它在工作: public void showSettingsAlert(){ AlertDialog.Builder alertdialog = new AlertDialog.Builder(mcontext); alertdialog.setTitle("Title"); alertdialog.setIcon(R.drawable.ic_gps); alertdialog.setMess

我不明白为什么我会得到一个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,我在另一个扩展DialogFragmentation的类上使用了相同的代码,它可以工作。。
谢谢。

当我试图将背景色设置为分隔符或按钮时,空指针究竟在哪里。