我有一个带有复选框的自定义对话框片段。 onClick
方法在xml中被调用。
我收到以下错误:
java.lang.IllegalStateException:无法在ID为“ checkBox2”的视图类androidx.appcompat.widget.AppCompatCheckBox上定义的android:onClick属性的父级或祖先上下文中找到方法onCheckboxClicked(View)
我已经对相关问题进行了各种回答,但是由于视图在onCreateView
中而不是在onCreate
中被夸大,因此无法弄清楚如何在对话框片段中实现
DialogFragment.java
public class TypeDialogFragment extends DialogFragment {
CheckBox type1, type2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_type_dialog, container, false);
type1=(CheckBox) v.findViewById(R.id.checkBox2);
type2=(CheckBox)v.findViewById(R.id.checkBox3) ;
// Do all the stuff to initialize your custom view
// set "origin" to top left corner, so to speak
Window window = getDialog().getWindow();
window.setGravity(Gravity.TOP|Gravity.LEFT);
// after that, setting values for x and y works "naturally"
WindowManager.LayoutParams params = window.getAttributes();
params.x = 100;
params.y = 250;
window.setAttributes(params);
//Log.d(TAG, String.format("Positioning DialogFragment to: x %d; y %d", params.x, params.y));
return v;
}
@Override public void onStart() {
super.onStart();
Window window = getDialog().getWindow();
WindowManager.LayoutParams windowParams = window.getAttributes();
windowParams.dimAmount = 0.00f;
windowParams.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setAttributes(windowParams);
}
public void onCheckboxClicked(View view) {
boolean checked = ((CheckBox) view).isChecked();
String str="";
// Check which checkbox was clicked
switch(view.getId()) {
case R.id.checkBox2:
str = checked?"Android Selected":"Android Deselected";
break;
case R.id.checkBox3:
str = checked?"AngularJS Selected":"AngularJS Deselected";
break;
}
Toast.makeText(view.getContext(), str, Toast.LENGTH_SHORT).show();
}
}
fragment_type_dialog.xml
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
app:cardElevation="2dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constrainLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Type 1"
android:textAlignment="viewStart"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
android:onClick="onCheckboxClicked"/>
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="8dp"
android:text="Type 2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/checkBox2"
android:onClick="onCheckBoxClicked"/>
....
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>