尝试在AlertDialog上设置标题大小时出现NullPointerException异常

时间:2013-05-10 06:37:19

标签: android android-layout android-alertdialog android-dialog

在AlertDialog中,我可以设置Message的大小,但是当我为Title执行相同操作时,它会崩溃。

代码:

AlertDialog dialog = new AlertDialog.Builder(this).setMessage(message).setTitle(title)
               .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                      dialog.cancel();
                   }
               }).show();
     TextView titleTxt= (TextView)dialog.findViewById(android.R.id.title);
     **titleTxt.setTextSize(TypedValue.COMPLEX_UNIT_SP, 40);** // crash here 
     TextView textView = (TextView) dialog.findViewById(android.R.id.message);
     **textView.setTextSize(40);** // works fine



 Button btn1 = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
   btn1.setTextSize(36); 

我的输出:

05-10 11:49:56.917: E/AndroidRuntime(8376): FATAL EXCEPTION: main
05-10 11:49:56.917: E/AndroidRuntime(8376): java.lang.NullPointerException
05-10 11:49:56.917: E/AndroidRuntime(8376):     at com.binary.taxitop.LoginScreen.showAlertDialog(LoginScreen.java:116)
05-10 11:49:56.917: E/AndroidRuntime(8376):     at com.binary.taxitop.LoginScreen.VerifyCredentialsAndEnterNextScreen(LoginScreen.java:103)
05-10 11:49:56.917: E/AndroidRuntime(8376):     at com.binary.taxitop.LoginScreen.onClick(LoginScreen.java:84)
05-10 11:49:56.917: E/AndroidRuntime(8376):     at android.view.View.performClick(View.java:3511)
05-10 11:49:56.917: E/AndroidRuntime(8376):     at android.view.View$PerformClick.run(View.java:14109)
05-10 11:49:56.917: E/AndroidRuntime(8376):     at android.os.Handler.handleCallback(Handler.java:605)
05-10 11:49:56.917: E/AndroidRuntime(8376):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-10 11:49:56.917: E/AndroidRuntime(8376):     at android.os.Looper.loop(Looper.java:137)
05-10 11:49:56.917: E/AndroidRuntime(8376):     at android.app.ActivityThread.main(ActivityThread.java:4424)
05-10 11:49:56.917: E/AndroidRuntime(8376):     at java.lang.reflect.Method.invokeNative(Native Method)
05-10 11:49:56.917: E/AndroidRuntime(8376):     at java.lang.reflect.Method.invoke(Method.java:511)
05-10 11:49:56.917: E/AndroidRuntime(8376):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-10 11:49:56.917: E/AndroidRuntime(8376):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-10 11:49:56.917: E/AndroidRuntime(8376):     at dalvik.system.NativeStart.main(Native Method)

提前致谢。

3 个答案:

答案 0 :(得分:5)

只需改变这一行:

TextView titleTxt= (TextView)dialog.findViewById(getApplicationContext().getResources().getIdentifier( "alertTitle", "id", "android" ));

答案 1 :(得分:0)

据我所知,没有直接方法可以修改title

中的AlertDialog

您可以创建自定义对话框或使用反射

下面的反映代码来自net,希望它有用

AlertDialog dialog = (AlertDialog) getDialog();  
try {  
     Field mAlert = AlertDialog.class.getDeclaredField("mAlert");  
     mAlert.setAccessible(true);  
     Object alertController = mAlert.get(dialog);  

     Field mTitleView = alertController.getClass().getDeclaredField("mTitleView");  
     mTitleView.setAccessible(true);  

     TextView title = (TextView) mTitleView.get(alertController);  
     title.setTextColor(0xff33b5e5);   

} catch (NoSuchFieldException e) {  
    e.printStackTrace();  
} catch (IllegalArgumentException e) {  
     e.printStackTrace();  
} catch (IllegalAccessException e) {  
    e.printStackTrace();  
}  

答案 2 :(得分:0)

当我遇到像你这样的情况时,这是我的代码。

修改: -

    String title = "Title";
    SpannableStringBuilder Builder = new SpannableStringBuilder(title);//Your title goes here
     StyleSpan spanState1 = new StyleSpan(Typeface.ITALIC);//You can make your title italic ,bold etc here
     //ScaleXSpan spanState2 = new ScaleXSpan(4);// As far your font is concern, this will help

Builder.setSpan(new RelativeSizeSpan(2.8f),  0, title.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);// set your size there                     
     Builder.setSpan(spanState1, 0, title.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
     //Builder.setSpan(spanState2, 0, title.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);

      AlertDialog.Builder dialog_build = new AlertDialog.Builder(MainActivity.this);

      dialog_build.setTitle(Builder);

      dialog_build.show();

仅关注标题,因为这是你的关注......

希望,这有帮助