我的XML如下-live_test_question_change_dialog_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/live_test_change_question_option_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/a"
android:background="@drawable/option_number_background"
android:textStyle="bold"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingStart="10dp"
android:paddingEnd="10dp"
/>
</RelativeLayout>
ShowDialog方法如下-
/*---------------------------------------------------*/
public void showDialog(){
final Dialog dialog = new Dialog(getApplicationContext(), R.style.FullHeightDialog);
dialog.setContentView(R.layout.live_test_question_change_dialog_layout);
dialog.setCancelable(false);
if (dialog.getWindow() != null){
dialog.getWindow().setLayout(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
}
RecyclerView recyclerView = dialog.findViewById(R.id.live_test_question_change_dialog_rec_view_id);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
ArrayList<String> questionNumberList = new ArrayList<>();
for(int i = 1; i<= testModel.getTotalQuestions(); i++){
questionNumberList.add(String.valueOf(i));
}
CustomAdapter customAdapter = new CustomAdapter(questionNumberList);
recyclerView.setAdapter(customAdapter);
dialog.show();
}
/*--------------------------------------------------*/
但是当我尝试显示对话框时,应用程序崩溃-
android.view.InflateException: Binary XML file line #7: Error inflating class android.support.v7.widget.RecyclerView
此代码有什么问题?如何解决这个问题?
答案 0 :(得分:2)
我认为您正在使用androidx
依赖项
您应该使用
<androidx.recyclerview.widget.RecyclerView
代替
<android.support.v7.widget.RecyclerView
答案 1 :(得分:0)
您必须创建一个从DialogFragment扩展的新“片段”
public class MyDialogWithRecyclerView extends DialogFragment
官方文档很好,我用它来在DialogFragment中创建Firestore RecyclerView。常规的回收站视图也是如此。
https://developer.android.com/reference/android/app/DialogFragment
如果您使用过RV和片段,则应该没问题,但是您可以问我任何问题。
编辑 抱歉,该版本已弃用,这是正确的文档 https://developer.android.com/reference/android/support/v4/app/DialogFragment.html