Search code examples
javaandroidclasscastexception

ClassCastException - getting Activity's view reference from adapter class for dialog fragment


I have an activity that has an EditText and a Dialogfragment. For DialogFragment I have a ListView and an Adapter class for that listview. The listview has a TextView and a button. I want to write the listitem's text to EditText of Activity on listitem's Button click (and then dismiss the Dialog). Pretty much I've achieved this all but it's showing ClassCastException in some devices. Here is the list item's Button clickListener:

holder.button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            EditText editText=((MainActivity)v.getContext()).findViewById(R.id.write_text);
            editText.removeTextChangedListener(((MainActivity) Objects.requireNonNull(v.getContext())).textWatcher);
            editText.addTextChangedListener(((MainActivity) Objects.requireNonNull(v.getContext())).textWatcher2);

            editText.getText().insert(editText.getSelectionStart(), listData.get(position).getBio());

            editText.removeTextChangedListener(((MainActivity) Objects.requireNonNull(v.getContext())).textWatcher2);
            editText.addTextChangedListener(((MainActivity) Objects.requireNonNull(v.getContext())).textWatcher);

            dialog.dismiss();


        }
    });

I don't understand why is there ClassCastException. Here is the stack trace from Crashlytics:

Fatal Exception: java.lang.ClassCastException: b.b.q.v0 cannot be cast to com.xx.xx.xx.MainActivity
       at com.xx.xx.Adapters.ListAdapter.access$000(ListAdapter.java)
       at android.view.View.performClick(View.java:4508)
       at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java)
       at android.view.View$PerformClick.run(View.java:18675)
       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:5590)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:515)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1280)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1096)
       at dalvik.system.NativeStart.main(NativeStart.java)

Any help will be highy appreciated.

EDIT: It's happening only in Android 4.4 devices.


Solution

  • when you initialize your adapter class as an alternative could be pass an activity reference as below.

    public class MyAdapter(Activity activity)
    

    and when you create an instance of your adapter in the dialog fragment

    Adapter adapter = new Adapter(getActivity());
    

    in that case getActivity method will return you the attached activity to dialog fragment.

    And finally you'll be able to use activity instance your adapter as below

     EditText editText = activity.findViewById(R.id.write_text);
    

    another approach could be to create a communication through interfaces

    regards