2017-12-04 33 views
0

私はRecyclerViewを持っていますが、ダイアログフラグメントを呼び出そうとしていますが、クラスキャスト例外が発生しています。私はスタック上で多くのソリューションを試しましたが、それは役に立たなかった。私を助けてください。ClassCastException 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); 
} 

答えて

0

これは、フォロー中の活動に対する文脈ローディングコード:

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

これはエラーです。

あなたはcontext活動ないアプリケーションであることを確認する必要があります。

+0

adapter.plsでコンストラクタをポストして質問を編集しました – user8601021

+0

'AllCallsRecyclerAdapter(List list、Context context)'をどうやって呼びますか?コンテキストをアクティビティとして使用する必要があります。 –

+0

ありがとう、たくさん働いています。 – user8601021

関連する問題