我需要按照this Question
中的要求更改AlertDialog标题下方分隔线的默认蓝色在搜索上我找到了一些解决方法,但尝试使用此代码时会给出异常。
继承我的代码:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Hello World")
.setTitle("Alert Dialog")
.setIcon(android.R.drawable.ic_dialog_alert)
.setCancelable(false)
.setPositiveButton("Close",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}
);
AlertDialog dialog = builder.create();
try {
((ViewGroup) ((ViewGroup) dialog.getWindow().getDecorView()).getChildAt(0))
.getChildAt(1)
.setBackgroundColor(getResources().getColor(R.color.app_orange));
}catch (NullPointerException ex){
Timber.e(ex.getStackTrace().toString());
}
dialog.show();
以下是LogCat:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.example/com.android.example.activities.HomeScreenActivity}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:318)
at com.android.internal.app.AlertController.installContent(AlertController.java:235)
at android.app.AlertDialog.onCreate(AlertDialog.java:337)
at android.app.Dialog.dispatchOnCreate(Dialog.java:363)
at android.app.Dialog.show(Dialog.java:264)
at com.android.example.fragments.HomeScreenFragment.onActivityCreated(HomeScreenFragment.java:114)
at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1508)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:958)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1189)
at android.app.Activity.performStart(Activity.java:5436)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
如果不存在,调用getDecorView()
会创建装饰视图。
在对话框上调用show()
会调用requestFeature()
,如果已经创建了装饰视图,则会因此异常而失败。大多数情况下,装饰视图是通过第一次调用setContentView()
创建的。因此,例外中的措辞。
代码看似hacky,但您可以尝试在对话框getDecorView()
之后移动涉及show()
的try-catch。