当我使用android.support.v7.app.AlertDialog
时,如果我使用
的setView(视图)
AlertDialog的方法。如果我使用android.app.AlertDialog
,那么我没有崩溃,但对话框不是材料风格。
public class SearchFilterFragment extends DialogFragment {
boolean isAscending;
CheckBox checkbox;
View view;
SearchFilterInterface searchFilterInterface = null;
int which_one;
int array_id;
public interface SearchFilterInterface {
void onSelect(int which, boolean asc);
}
public static SearchFilterFragment newInstance(@ArrayRes int array_id) {
Bundle args = new Bundle();
args.putInt(GiantBomb.ARRAY,array_id);
SearchFilterFragment fragment = new SearchFilterFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
searchFilterInterface = (SearchFilterInterface) getTargetFragment();
} catch (Exception e) {
Toaster.make(getContext(), "interface cast exception");
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
array_id = getArguments().getInt(GiantBomb.ARRAY);
if (array_id==R.array.search_filter) {
which_one = SharedPreference.getFromSharedPreferences(GiantBomb.WHICH, 4, getContext());
isAscending = SharedPreference.getFromSharedPreferences(GiantBomb.ASCENDING, true, getContext());
}else {
which_one = SharedPreference.getFromSharedPreferences(GiantBomb.SORT_WHICH,1,getContext());
isAscending = SharedPreference.getFromSharedPreferences(GiantBomb.SORT_ASCENDING,true,getContext());
}
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
view = getActivity().getLayoutInflater().inflate(R.layout.search_option_layout,null);
checkbox = (CheckBox) view.findViewById(R.id.checkbox);
final int uncheck_color = ContextCompat.getColor(getContext(),R.color.black_white);
checkbox.setChecked(!isAscending);
if (!checkbox.isChecked()) { checkbox.setTextColor(uncheck_color) ;}
final AlertDialog dialog = new AlertDialog.Builder(getContext(),R.style.MyDialogTheme)
.setCancelable(true)
.setTitle("Sort result")
.setSingleChoiceItems(getResources().getStringArray(array_id), which_one, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
which_one = which;
}
})
//crashes here if i use v7 AlertDialog class
.setView(view)
}).create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(ContextCompat.getColor(getContext(),R.color.primary));
}
});
return dialog;
}
}
我的自定义视图
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
>
<CheckBox android:id="@+id/checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Descending"
android:textColor="?android:attr/textColorPrimary"
android:theme="@style/MyCheckBox"
android:drawableLeft="@android:color/transparent"
android:drawableStart="@android:color/transparent"
android:drawablePadding="20dp"
style="?android:attr/textAppearanceMedium"
/>
</FrameLayout>
MyDialogTheme
<style name="MyDialogTheme" parent="Theme.AppCompat.DayNight">
<!--item RadioButton or CheckBox color-->
<item name="colorControlNormal">@color/black_white</item>
<item name="colorControlActivated">@color/accent</item>
<!--item text color-->
<item name="textColorAlertDialogListItem">?android:attr/textColorPrimary</item>
<!--buttons color-->
<item name="colorAccent">@color/accent</item>
<!--title and message color-->
<item name="android:textColor">?android:attr/textColorPrimary</item>
<item name="android:background">@color/cardColor</item>
</style>
堆栈跟踪
java.lang.ClassCastException: android.support.v7.widget.LinearLayoutCompat$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams
at android.support.v7.app.AlertController.setupCustomContent(AlertController.java:655)
at android.support.v7.app.AlertController.setupView(AlertController.java:463)
at android.support.v7.app.AlertController.installContent(AlertController.java:226)
at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:257)
at android.app.Dialog.dispatchOnCreate(Dialog.java:361)
at android.app.Dialog.show(Dialog.java:262)
at android.support.v4.app.DialogFragment.onStart(DialogFragment.java:418)
at android.support.v4.app.Fragment.performStart(Fragment.java:2215)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1340)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:757)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2355)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2146)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2098)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2008)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5113)
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:796)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
这是support-v7库中的已知错误。要获得解决方法,您可以使用版本&lt; = 25.0.1
您还可以按照相关错误报告中讨论的错误进行操作: