Piyush Kukadiya
Piyush Kukadiya

Reputation: 1890

java.lang.ClassCastException while communicating between two Fragments

I have two Fragments , one Activity and one Interface

FragmentA contains one Button ,FragmentB contains one TextView and MainActivity implements Interface for fragment communication that's it.

This is the mainActivity layout file where i have a big tragedy.

activity_main.xml :

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" 
        android:id="@+id/main">
           <fragment
            android:id="@+id/fragment1"
            android:name="com.example.fragment3.FragmentA"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/fragment2"
            android:layout_alignParentTop="true"
            android:layout_marginTop="157dp" />


            <fragment
            android:id="@+id/fragment2"
            android:name="com.example.fragment3.FragmentB"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/fragment1"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="40dp" />

            </RelativeLayout>   

Everything works fine with above layout when i click button,code runs as expected But when i swap above two fragments in the same layout and then click on button it gives following error:

09-02 11:03:54.569: E/AndroidRuntime(1750): FATAL EXCEPTION: main
09-02 11:03:54.569: E/AndroidRuntime(1750): java.lang.ClassCastException: com.example.fragment3.FragmentA cannot be cast to com.example.fragment3.FragmentB
09-02 11:03:54.569: E/AndroidRuntime(1750):     at com.example.fragment3.MainActivity.respond(MainActivity.java:20)
09-02 11:03:54.569: E/AndroidRuntime(1750):     at com.example.fragment3.FragmentA.onClick(FragmentA.java:39)
09-02 11:03:54.569: E/AndroidRuntime(1750):     at android.view.View.performClick(View.java:4084)
09-02 11:03:54.569: E/AndroidRuntime(1750):     at android.view.View$PerformClick.run(View.java:16966)
09-02 11:03:54.569: E/AndroidRuntime(1750):     at android.os.Handler.handleCallback(Handler.java:615)
09-02 11:03:54.569: E/AndroidRuntime(1750):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-02 11:03:54.569: E/AndroidRuntime(1750):     at android.os.Looper.loop(Looper.java:137)
09-02 11:03:54.569: E/AndroidRuntime(1750):     at android.app.ActivityThread.main(ActivityThread.java:4745)
09-02 11:03:54.569: E/AndroidRuntime(1750):     at java.lang.reflect.Method.invokeNative(Native Method)
09-02 11:03:54.569: E/AndroidRuntime(1750):     at java.lang.reflect.Method.invoke(Method.java:511)
09-02 11:03:54.569: E/AndroidRuntime(1750):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-02 11:03:54.569: E/AndroidRuntime(1750):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-02 11:03:54.569: E/AndroidRuntime(1750):     at dalvik.system.NativeStart.main(Native Method)  

activity_main.xml after swapping :

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" 
    android:id="@+id/main">

  <fragment
        android:id="@+id/fragment2"
        android:name="com.example.fragment3.FragmentB"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/fragment1"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="40dp" />

    <fragment
        android:id="@+id/fragment1"
        android:name="com.example.fragment3.FragmentA"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/fragment2"
        android:layout_alignParentTop="true"
        android:layout_marginTop="157dp" />

      </RelativeLayout>

OnClick method of FragmentA:

@Override
public void onClick(View v) {
counter++;
comm.respond("The button was clicked " +counter+ " times");

}  

MainActivity.java

public class MainActivity extends Activity implements communicator{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void respond(String data) {
        FragmentManager manager =getFragmentManager();
        FragmentB f2 =(FragmentB) manager.findFragmentById(R.id.fragment2);
        f2.changeText(data);
    }

    }  

What's the problem,i can't understand such behavior of fragment.

Upvotes: 0

Views: 97

Answers (1)

SilentKiller
SilentKiller

Reputation: 6942

In your code there is nothing wrong you just need to clean and build the project. Issue is just that some time old reference remains so to remove that old reference make a habit to clean and run the project after resolving some errors or bug.

Upvotes: 2

Related Questions