【问题标题】:ClassCastException on calling a dialogfragment from RecyclerView从 RecyclerView 调用 dialogfragment 时出现 ClassCastException
【发布时间】:2017-12-04 08:44:38
【问题描述】:

我有一个 RecyclerView,我试图从中调用一个对话框片段,但我得到一个类转换异常。我在堆栈上尝试了很多解决方案,但没有帮助。请帮帮我。

    java.lang.ClassCastException: android.app.Application cannot be cast to android.app.Activity
12-04 14:08:52.495 6146-    6146/com.opentok.example.efflorescence.myaudiotranscription W/System.err:     at com.opentok.example.efflorescence.myaudiotranscription.AllCallsRecyclerAdapter.showTranscriptionDialog(AllCallsRecyclerAdapter.java:249)
12-04 14:08:52.495 6146-6146/com.opentok.example.efflorescence.myaudiotranscription W/System.err:     at com.opentok.example.efflorescence.myaudiotranscription.AllCallsRecyclerAdapter$1$1.run(AllCallsRecyclerAdapter.java:210)
12-04 14:08:52.495 6146-6146/com.opentok.example.efflorescence.myaudiotranscription W/System.err:     at android.os.Handler.handleCallback(Handler.java:815)
12-04 14:08:52.495 6146-6146/com.opentok.example.efflorescence.myaudiotranscription W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:104)
12-04 14:08:52.495 6146-6146/com.opentok.example.efflorescence.myaudiotranscription W/System.err:     at android.os.Looper.loop(Looper.java:207)
12-04 14:08:52.495 6146-6146/com.opentok.example.efflorescence.myaudiotranscription W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5765)
12-04 14:08:52.495 6146-6146/com.opentok.example.efflorescence.myaudiotranscription W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
12-04 14:08:52.495 6146-6146/com.opentok.example.efflorescence.myaudiotranscription W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
12-04 14:08:52.496 6146-6146/com.opentok.example.efflorescence.myaudiotranscription W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

我的适配器代码 onClick

public void showTranscriptionDialog() {
  try {
    FragmentManager fr = ((Activity) context).getFragmentManager();
    TranscriptionDialogFragment msgDialog = new TranscriptionDialogFragment();
    msgDialog.show(fr, "Dialog");
  }
    catch (Exception e) {
    e.printStackTrace();
    Log.e("error displaying dialog",e.getLocalizedMessage());
  }
}

我的片段

public class TranscriptionDialogFragment extends DialogFragment {

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_transcription, container, false);
    getDialog().setTitle("Simple Dialog");
    return rootView;
  }
}

适配器中的我的构造函数:

public AllCallsRecyclerAdapter(List<AllCallsData> list, Context context) {
  this.mInflater = LayoutInflater.from(context);
  this.list = list;
  this.context = context;
  mydb = new DBHelper(context);
}

【问题讨论】:

    标签: android android-recyclerview dialogfragment


    【解决方案1】:

    这是因为您试图将 Application 上下文转换为以下代码中的活动:

    FragmentManager fr = ((Activity) context).getFragmentManager();
    

    这是一个错误。

    您需要确保context活动而不是应用程序

    【讨论】:

    • 我已经编辑了我的问题,在适配器中发布了构造函数。请检查它
    • 你怎么称呼AllCallsRecyclerAdapter(List&lt;AllCallsData&gt; list, Context context)?您需要使用活动作为上下文。
    猜你喜欢
    • 1970-01-01
    • 2012-01-06
    • 2013-01-16
    • 1970-01-01
    • 2017-02-14
    • 1970-01-01
    • 2023-03-18
    • 2017-09-15
    • 1970-01-01
    相关资源
    最近更新 更多