AlertDialogはtitle,message,buttonスタイルの空のポインタの問題を修正します

2048 ワード

AlertDialog alertDialog = new AlertDialog.Builder(this).setTitle("Title").setMessage("Message").setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        }).setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        }).create();
        alertDialog.show();
    // ,button  alertDialog.show(); 
    // ,button  alertDialog.show(); 
    // ,button  alertDialog.show(); 
        Button btnPositive =
                alertDialog.getButton(android.app.AlertDialog.BUTTON_POSITIVE);
        Button btnNegative =
                alertDialog.getButton(android.app.AlertDialog.BUTTON_NEGATIVE);
        btnNegative.setTextColor(getResources().getColor(R.color.red));
        btnNegative.setTextSize(18);
        btnPositive.setTextColor(getResources().getColor(R.color.green));
        btnPositive.setTextSize(18);

の原因となる


ここではソースの角度から簡単に説明します.なぜ後ろに置くのですか.
ctrl+ , 
 if (!mCreated) {
            dispatchOnCreate(null);
        }

 ctrl+ 
 void dispatchOnCreate(Bundle savedInstanceState) {
        if (!mCreated) {
            onCreate(savedInstanceState);  // oncreate 
            mCreated = true;
        }
    }

 AlertDialog oncreate() 
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAlert.installContent();
    }
ctrl+ 
  public void installContent() {
        final int contentView = selectContentView();
        mDialog.setContentView(contentView);
        setupView();
    }
    setupView() 
   setupButtons(buttonPanel);
    
   mButtonPositive = (Button) buttonPanel.findViewById(android.R.id.button1);
    mButtonPositive.setOnClickListener(mButtonHandler);

 button show() , button show() , 
         ```