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

rav*_*avz 0 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); 
Run Code Online (Sandbox Code Playgroud)

我的输出:

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)
Run Code Online (Sandbox Code Playgroud)

提前致谢.

Sud*_*hee 5

只需改变这一行:

TextView titleTxt= (TextView)dialog.findViewById(getApplicationContext().getResources().getIdentifier( "alertTitle", "id", "android" ));
Run Code Online (Sandbox Code Playgroud)