我的目标是在MainActivity中实现一个方法,用于所有我将创建的片段。它的目的是允许生成另一个片段(例如,Fragment1是一个列表视图。它回调到mainActivity以创建detailedItemFragment。这个detailedItemFragment可以反过来生成具有相同回调的片段)。
每个片段包含相同的接口描述。回调根据所需的下一个片段传递一个ID。当我尝试将第二个回调附加到onAttach()中的Activity时,问题就出现了。我得到了一个ClassCastException。
我认为这是一个关于接口的概念性问题,我没有正确地实现它。现在我将使用单独的回调。我的问题是这是否正确的做法,如果是,为什么第二个片段的onAttach()会出现问题?
我的第一个片段是:
我的MainActivity实现了OnItemSelected接口,并包含以下内容:
错误块如下:
03-28 10:19:07.552: E/AndroidRuntime(6099): 致命异常:java.lang.ClassCastException: ca.test.code.MainActivity@4244bbe0 必须实现 OnItemSelected 03-28 10:19:07.552: E/AndroidRuntime(6099): at ca.test.code.fragments.Offer1Fragment.onAttach(Offer1Fragment.java:38) 03-28 10:19:07.552: E/AndroidRuntime(6099): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:844) 03-28 10:19:07.552: E/AndroidRuntime(6099): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057) 03-28 10:19:07.552: E/AndroidRuntime(6099): at android.app.BackStackRecord.run(BackStackRecord.java:682) 03-28 10:19:07.552: E/AndroidRuntime(6099): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435) 03-28 10:19:07.552: E/AndroidRuntime(6099): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441) 03-28 10:19:07.552: E/AndroidRuntime(6099): at android.os.Handler.handleCallback(Handler.java:725) 03-28 10:19:07.552: E/AndroidRuntime(6099): at android.os.Handler.dispatchMessage(Handler.java:92) 03-28 10:19:07.552: E/AndroidRuntime(6099): at android.os.Looper.loop(Looper.java:137) 03-28 10:19:07.552: E/AndroidRuntime(6099): at android.app.ActivityThread.main(ActivityThread.java:5039)
请注意,该错误是由于 MainActivity@4244bbe0 必须实现 OnItemSelected 所引起的。
每个片段包含相同的接口描述。回调根据所需的下一个片段传递一个ID。当我尝试将第二个回调附加到onAttach()中的Activity时,问题就出现了。我得到了一个ClassCastException。
我认为这是一个关于接口的概念性问题,我没有正确地实现它。现在我将使用单独的回调。我的问题是这是否正确的做法,如果是,为什么第二个片段的onAttach()会出现问题?
我的第一个片段是:
public class MainFragment extends Fragment implements OnClickListener {
public interface OnItemSelected {
public void launchNewFragment(FragId id);
}
private OnItemSelected callback;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
return view;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
callback = (OnItemSelected) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnItemSelected");
}
}
我的第二个碎片是:
public class Offer1Fragment extends Fragment implements OnClickListener {
public interface OnItemSelected {
public void launchNewFragment(FragId id);
}
private OnItemSelected callback;
private Context context;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_offer1, container, false);
context = getActivity();
return view;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
callback = (OnItemSelected) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnItemSelected");
}
}
我的MainActivity实现了OnItemSelected接口,并包含以下内容:
@Override
public void launchNewFragment(FragId id) {
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.addToBackStack(null);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
Fragment fragment = null;
switch(id) {
case Offer1:
fragment = new Offer1Fragment();
break;
//Other fragment ids are used to instantiate here as well
}
fragmentTransaction.replace(android.R.id.content, fragment);
fragmentTransaction.commit();
}
错误块如下:
03-28 10:19:07.552: E/AndroidRuntime(6099): 致命异常:java.lang.ClassCastException: ca.test.code.MainActivity@4244bbe0 必须实现 OnItemSelected 03-28 10:19:07.552: E/AndroidRuntime(6099): at ca.test.code.fragments.Offer1Fragment.onAttach(Offer1Fragment.java:38) 03-28 10:19:07.552: E/AndroidRuntime(6099): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:844) 03-28 10:19:07.552: E/AndroidRuntime(6099): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057) 03-28 10:19:07.552: E/AndroidRuntime(6099): at android.app.BackStackRecord.run(BackStackRecord.java:682) 03-28 10:19:07.552: E/AndroidRuntime(6099): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435) 03-28 10:19:07.552: E/AndroidRuntime(6099): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441) 03-28 10:19:07.552: E/AndroidRuntime(6099): at android.os.Handler.handleCallback(Handler.java:725) 03-28 10:19:07.552: E/AndroidRuntime(6099): at android.os.Handler.dispatchMessage(Handler.java:92) 03-28 10:19:07.552: E/AndroidRuntime(6099): at android.os.Looper.loop(Looper.java:137) 03-28 10:19:07.552: E/AndroidRuntime(6099): at android.app.ActivityThread.main(ActivityThread.java:5039)
请注意,该错误是由于 MainActivity@4244bbe0 必须实现 OnItemSelected 所引起的。