我是一位资深的程序员
我有一个DialogFragment
。 DialogFragment
有一个interface
:
public class FragmentEditClass extends DialogFragment
{
//. . .
public interface NotifyChanged
{
void updateRemoveClass(int position);
}
}
我在“片段”选项卡中使用了界面:
public class SecondTab extends Fragment implements FragmentEditClass.NotifyChanged
{
//. . .
@Override
public void updateRemoveClass(int position)
{
//. . .
}
}
我在适配器中显示DialogFragment
FragmentEditClass.newInstance(classId , className , position)
.show(secondTab.getFragmentManager() , "DialogFragmentClass");
我想通过以下方式在fragmentTab
中的interface
和DialogFragment
之间进行通信:
public class FragmentEditClass extends DialogFragment
{
private NotifyChanged notifyChanged;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
notifyChanged = (NotifyChanged) getActivity();//This line
}
}
现在的问题是getActivity()
。此方法引用SecondTab,但是SecondTab在MainActivity中,我需要引用MainAcitvity。
MyError:
java.lang.ClassCastException: com.example.user.classmanager.MainActivity cannot be cast to com.example.user.classmanager.FragmentEditClass$NotifyChanged
at com.example.user.classmanager.FragmentEditClass.onCreate(FragmentEditClass.java:91)