2013-02-25 12 views
6

Próbowałem zmienić czcionkę AlertDialog używane przez tę funkcjęGet AlertDialog tytuł przez findViewById

private void saveDialog(){ 

    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setTitle(res.getString(R.string.dialog_title)) 
       .setMessage(res.getString(R.string.dialog_saveconfirm)) 
       .setCancelable(false) 
       .setNegativeButton(res.getString(R.string.dialog_cancel), new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        dialog.cancel(); 
       } 
       }) 
       .setPositiveButton(res.getString(R.string.dialog_ok), new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        //do some thing 
      }); 
     AlertDialog alert = builder.create(); 
     alert.show(); 

     TextView tit = (TextView) alert.findViewById(android.R.id.title); 
     TextView msg = (TextView) alert.findViewById(android.R.id.message); 
     Button btn2 = alert.getButton(DialogInterface.BUTTON_NEGATIVE); 
     Button btn1 = alert.getButton(DialogInterface.BUTTON_POSITIVE); 
     tit.setTypeface(UtiliShare.getTf()); 
     msg.setTypeface(UtiliShare.getTf()); 
     btn1.setTypeface(UtiliShare.getTf()); 
     btn2.setTypeface(UtiliShare.getTf()); 

} 

Kiedy wywołanie funkcji aktywności miałem 02-25 17:59:04.759: E/AndroidRuntime(1014): java.lang.NullPointerException dla tit kiedy ustawić krój, ale kiedy usunąć tit dialogowe dobrej pracy.

Myślę, że błąd w TextView tit = (TextView) alert.findViewById(android.R.id.title); jest zwracana wartość null.

Jak mogę to rozwiązać?


Aktualizacja Ten link zawiera odpowiedzi na moje pytanie Answer

Dzięki Sam

+0

nie wierzę Android pozwala ci uzyskać dostęp do tych TextView tak, ale powinieneś być w stanie korzystać stylami i robić to, co chcesz: [Zmień styl AlertDialog] (http://stackoverflow.com/q/8011899/1267661) – Sam

+0

Mój post może Ci pomóc http://stackoverflow.com/questions/4025605/custom-dialog-on-android-how-can-i-center-its -title/13359573 # 13359573 " –

+2

Hmm, ta odpowiedź twierdzi, że możesz użyć' alertTitle' zamiast 'title': [Zmiana rozmiaru czcionki na AlertDialog] (http://stackoverflow.com/a/9183354/1267661). (Nie próbowałem tego samemu.) – Sam

Odpowiedz