2014-10-19 38 views
1

当我从活动延伸,我修改fragment_main和找到这样的控制:从ActionBarActivity扩展时绑定到XML的位置?

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.fragment_main); 

    MyButton = (Button) findViewById(R.id.button1); 
} 

,但是当我这样做从ActionBarActivity延伸时,给我一个错误,所以做工作,我要评论:

/*if (savedInstanceState == null) { 
     getSupportFragmentManager().beginTransaction() 
       .add(R.id.container, new PlaceholderFragment()).commit(); 
}*/ 

,并设置activity_main到fragment_main

setContentView(R.layout.fragment_main); 

,但我不相信这是正确的...

+0

在哪个布局文件中是R.id.button1按钮? – joao2fast4u 2014-10-19 00:05:07

+0

在fragment_main中,我使用了来自eclipse的Blank活动 – 2014-10-19 00:50:28

+0

为什么你想让你的Activity扩展ActionBarActivity? – joao2fast4u 2014-10-19 00:55:05

回答

0

你不应该得到你的活动的按钮视图,但它在一个片段中。你可以像你在你的活动中那样得到你的片段,例如PlaceholderFragment。如果需要,您应该知道如何在ActivityFragment之间进行交互。链接http://developer.android.com/guide/components/fragments.html可能会帮助你。