应用程序在 API 27 时崩溃,但在 API 21 时正确运行
App falls down with API 27 but wolks correctly with API 21
我正在尝试为 DialogFragment 设置标题文本大小
我为我的旧测试设备 (Android 5.0 API 21) 编码,一切正常
但是在我开始使用新的测试设备后应用程序下降了(Android 8.1 API 27)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_multiple_info_dialog, null, false);
getDialog().setTitle(R.string.dialog_fragment_BAC_info_title);
TextView title = getDialog().findViewById(android.R.id.title);
title.setTextSize(30);
title.setTextColor(getResources().getColor(R.color.white));
它落在 setTextSize(30) 行,错误
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.TextView.setTextSize(float)' on a null object reference
是的,日志告诉我标题 == null
我试图将部分代码替换为 onDialogCreate(Bundle savedInstanceState)...
但同样的事情 - 现在应用程序落在 onDialogCreate 内的同一行上...
有什么解决办法吗?
来自 Gradle
compileSdkVersion 27
minSdkVersion 21
targetSdkVersion 27
this may help, pass the textsize as well as the unit.
* @param unit The desired dimension unit.
* @param size The desired size in the given units.
*
* @attr ref android.R.styleable#TextView_textSize
*/
public void setTextSize(int unit, float size)
感谢 Mike M.
正确的解决方案是
在 onCreate() 中定义对话框片段的样式并在 DialogFragment 的 onCreateDialog() 方法中设置标题参数 class
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogFragmentStyle);
}
需要在styles.xml
中添加样式
<style name="DialogFragmentStyle" parent="Theme.AppCompat.Light.Dialog">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">false</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">false</item>
</style>
之后需要设计对话框
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
TextView title = dialog.findViewById(android.R.id.title); //here it works!
title.setTextSize(30);
title.setText(R.string.dialog_fragment_title);
title.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
title.setTextColor(getResources().getColor(R.color.orange));
dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
return dialog;
}
我正在尝试为 DialogFragment 设置标题文本大小
我为我的旧测试设备 (Android 5.0 API 21) 编码,一切正常
但是在我开始使用新的测试设备后应用程序下降了(Android 8.1 API 27)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_multiple_info_dialog, null, false);
getDialog().setTitle(R.string.dialog_fragment_BAC_info_title);
TextView title = getDialog().findViewById(android.R.id.title);
title.setTextSize(30);
title.setTextColor(getResources().getColor(R.color.white));
它落在 setTextSize(30) 行,错误
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setTextSize(float)' on a null object reference
是的,日志告诉我标题 == null
我试图将部分代码替换为 onDialogCreate(Bundle savedInstanceState)...
但同样的事情 - 现在应用程序落在 onDialogCreate 内的同一行上...
有什么解决办法吗?
来自 Gradle
compileSdkVersion 27
minSdkVersion 21
targetSdkVersion 27
this may help, pass the textsize as well as the unit.
* @param unit The desired dimension unit.
* @param size The desired size in the given units.
*
* @attr ref android.R.styleable#TextView_textSize
*/
public void setTextSize(int unit, float size)
感谢 Mike M.
正确的解决方案是
在 onCreate() 中定义对话框片段的样式并在 DialogFragment 的 onCreateDialog() 方法中设置标题参数 class
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogFragmentStyle);
}
需要在styles.xml
中添加样式<style name="DialogFragmentStyle" parent="Theme.AppCompat.Light.Dialog">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">false</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">false</item>
</style>
之后需要设计对话框
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
TextView title = dialog.findViewById(android.R.id.title); //here it works!
title.setTextSize(30);
title.setText(R.string.dialog_fragment_title);
title.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
title.setTextColor(getResources().getColor(R.color.orange));
dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
return dialog;
}