【问题标题】:Android Fragment does not Implement Activity's Interface errorAndroid Fragment 没有实现 Activity 的接口错误
【发布时间】:2018-07-12 08:56:10
【问题描述】:

我的活动 (NewTaskInput) 声明了一个接口 OnFABClickedListener,其中一个名为 onFABClicked 的方法需要 NewTaskInputFragment 片段类在活动的 onFragmentAttached 方法中实现它。我的片段确实实现了接口和 onFABClicked 方法,该方法将 Parcelable POJO (TaskItem) 返回给活动。

但是,我收到片段未实现接口的错误消息?错误信息:

引起:java.lang.ClassCastException: ReportFragment{d8395ee #0 android.arch.lifecycle.LifecycleDispatcher.report_fragment_tag} 必须 在 ... 实现 OnFABClickedListener 活动.NewTaskInput.onAttachFragment(NewTaskInput.java:48)

知道为什么会出现错误吗?

NewTask.java(活动)

public class NewTaskInput extends AppCompatActivity {

    // Interface
    public interface OnFABClickedListener {
        TaskItem onFABClicked();
    }

    // Member variables
    private OnFABClickedListener fabClickedListener;
    private TaskItem mTaskItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_task_input);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mTaskItem = fabClickedListener.onFABClicked();
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    public void onAttachFragment(Fragment fragment) {
        try {
            fabClickedListener = (OnFABClickedListener)fragment;
        } catch (ClassCastException e) {
            throw new ClassCastException(fragment.toString() + " must implement OnFABClickedListener");
        }
        super.onAttachFragment(fragment);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        fabClickedListener = null;
    }
}

NewTaskInputFragment.java

public class NewTaskInputFragment extends Fragment implements NewTaskInput.OnFABClickedListener {
  .
  .
  .

    @Override
    public void onAttach(Context activtiy) {
        myContext = (FragmentActivity)activtiy;
        super.onAttach(activtiy);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_newtask_input, container, false);

        initializeViews(rootView);
        initializeListeners();

        return rootView;
    }

    @Override
    public TaskItem onFABClicked() {

        // Initialize a new TaskItem instance
        mTaskItem = new TaskItem();

        mTaskItem.seTitle(editTask.getText().toString());

        return mTaskItem;
    }

activity_new_task_input.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activities.NewTaskInput">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_new_task_input" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@mipmap/save" />

</android.support.design.widget.CoordinatorLayout>

content_new_task_input.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment"
    android:name="....fragments.NewTaskInputFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:layout="@layout/fragment_newtaskgoal_input" />

【问题讨论】:

  • Activity中附加片段的代码在哪里?
  • 我使用 标签添加它:activity_new_task_input.xml 和 content_new_task_input.xml 到我上面的代码

标签: java android android-activity interface fragment


【解决方案1】:

你读错了吗? :)

原因:java.lang.ClassCastException: ReportFragment{d8395ee #0 android.arch.lifecycle.LifecycleDispatcher.report_fragment_tag} 必须在 ...activity.NewTaskInput.onAttachFragment(NewTaskInput.java :48)

它说ReportFragment 没有实现你的OnFABClickedListener 接口。您尚未包含此片段的代码,但我怀疑堆栈跟踪没有说谎,并且该片段确实没有实现该接口。

onAttachFragment 会为附加到您的活动的任何片段调用,而不仅仅是NewTaskInputFragment

【讨论】:

  • 但是我什至没有在任何地方声明一个名为 ReportFragment 的片段?
  • 我不可能知道.. 我认为如果你根本不使用onAttachFragment 并且只使用FragmentManager 通过id 或标签来查找你的片段会更简单.然后你可以像现在一样调用onFABClicked
  • 谢谢,我宁愿那样做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-17
  • 1970-01-01
相关资源
最近更新 更多