我有两个班级;
ProfileFragment.class
public class ProfileFragment extends Fragment implements OnClickListener {
Fragment fragment = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_profile, container,false);
return rootView;
}
}
LastCheckins.class
public class LastCheckins extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView sv = (ScrollView) findViewById(R.id.scrollView1);
LinearLayout ll = (LinearLayout) findViewById(R.id.LinearLayout1);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
for (int i = 0; i < 20; i++) {
CheckBox cb = new CheckBox(this);
cb.setText("I'm dynamic!");
ll.addView(cb);
}
this.setContentView(sv);
}
}
如何在程序创建ProfileFragment类时调用Activity类?
非常感谢。
答案 0 :(得分:2)
使用getActivity()。
您可以将其投射到LastChekins,以便您可以调用其metods并访问其可见字段:
((LastCheckins)getActivity()).methodX();
答案 1 :(得分:0)
getActivity()
返回此片段当前与之关联的Activity。