我创建了片段,需要调用我从开发人员教程中引用的活动。但是以下代码:
public void onAttach(Activity activity)
{
super.onAttach(activity);
try
{
interfaceDataCommunicator = (InterfaceDataCommunicator) activity;
}
catch (ClassCastException e)
{
throw new ClassCastException(activity.toString()
+ " must implement InterfaceDataCommunicator");
}
}
即使我在mainactivity中实现了这个接口,也会抛出classcastexception。我无法弄清楚原因。任何帮助都将非常感激。这就是我的主要活动:
public class TestActivity extends Activity implements InterfaceDataCommunicator
{
public InterfaceDataCommunicatorFromActivity interfaceDataCommunicatorFromActivity;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
}
public void updateData(String data)
{
//interfaceDataCommunicatorFromActivity.updateData(data);
this.data=data;
}