请问动态加载是不是不能在MainActivity中操控fragment里的组件啊
public class MainActivity extends AppCompatActivity {
private Button bt;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyFragment myFragment=new MyFragment();
FragmentManager fragmentManager=getFragmentManager();
FragmentTransaction beginTransaction=fragmentManager.beginTransaction();
beginTransaction.add(R.id.rl,myFragment);
beginTransaction.commit();
/* bt=(Button)findViewById(R.id.button);
tv=(TextView)findViewById(R.id.textView);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tv.setText("Fragment里的按钮在活动中起作用了");
}
});*/
}
}
如果不把注释加上,程序就会崩溃,,,静态加载就可以不用加注释