我的活动(NewTaskInput)使用一个名为onFABClicked的方法声明了一个OnFABClickedListener接口,该方法需要NewTaskInputFragment片段类在活动的onFragmentAttached方法中实现该接口。我的片段确实实现了接口和方法onFABClicked,该方法将Parcelable POJO(TaskItem)返回给活动。
但是我收到错误消息,该片段没有使接口无效?错误信息:
引起原因:java.lang.ClassCastException:ReportFragment {d8395ee#0
android.arch.lifecycle.LifecycleDispatcher.report_fragment_tag}必须
在...上实现OnFABClickedListener
Activities.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" />
最佳答案
你读错了吗? :)
引起原因:java.lang.ClassCastException:ReportFragment {d8395ee#0 android.arch.lifecycle.LifecycleDispatcher.report_fragment_tag}必须在... activity.NewTaskInput.onAttachFragment(NewTaskInput.java:48)上实现OnFABClickedListener。
它说ReportFragment
没有实现您的OnFABClickedListener
接口。您尚未包含此片段的代码,但我怀疑堆栈跟踪没有在说谎,并且该片段确实未实现该接口。onAttachFragment
是针对附加到您的活动的任何片段而调用的,而不仅仅是NewTaskInputFragment
。