Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在两个片段之间通信时发生java.lang.ClassCastException_Android_Android Fragments - Fatal编程技术网

Android 在两个片段之间通信时发生java.lang.ClassCastException

Android 在两个片段之间通信时发生java.lang.ClassCastException,android,android-fragments,Android,Android Fragments,我有两个片段,一个活动和一个接口 FragmentA包含一个按钮,FragmentB包含一个TextView和MainActivity为片段通信实现接口 这是mainActivity布局文件,我在其中遇到了一个大悲剧 活动\u main.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.

我有两个
片段
,一个
活动
和一个
接口

FragmentA
包含一个
按钮
FragmentB
包含一个
TextView
MainActivity
为片段通信实现
接口

这是
mainActivity布局
文件,我在其中遇到了一个大悲剧

活动\u 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>   
交换后的活动\u 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/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>
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);
    }

    }  

问题是什么,我无法理解片段的这种行为。

在您的代码中没有什么错误,您只需要清理并构建项目。
问题只是保留了一段时间的旧引用,所以要删除旧引用,请养成在解决一些错误或bug后清理并运行项目的习惯。

如果您已正确交换,但仍然面临错误,请尝试清理并构建项目,然后运行它again@SilentKiller清洗后它可以工作,但有什么问题吗这里?只是旧引用。所以eclipse不会自动清除旧引用
@Override
public void onClick(View v) {
counter++;
comm.respond("The button was clicked " +counter+ " times");

}  
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);
    }

    }