您的位置:首页 > 移动开发 > Android开发

[Android]新版的sdk中新建一个android应用,增加的PlaceholderFragment这个静态类发生的事情

2014-05-26 22:57 417 查看
1,首先发生的是有两个布局xml,一个activity_main.xml,一个是fragment_main.xml一开始没在意,后来仔细看了原来是新功能的fragment概念等于多个场景在这个activity中切换,

按照原来的理念就是

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

latitudetext = (TextView) findViewById(R.id.latitudetext);
longtitudetext = (TextView) findViewById(R.id.longtitudetext);
altitudetext = (TextView) findViewById(R.id.altitudetext);
speedtext = (TextView) findViewById(R.id.speedtext);
button1 = (Button) findViewById(R.id.button1);
buttonlisten mybuttonlisten = new buttonlisten();
button1.setOnClickListener(mybuttonlisten);//错误在这里
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}

}


在原本的activity中添加这些对xml控件操作的一些代码,后来发现运行时候报错,错误在12行。

仔细搜索了一下,各种百度一下,发现是因为我这些控件都是在fragment的xml中,原本的activity直接就加入了fragment的xml场景,等于默认的activity_main.xml并没有运作,直接加载了另一个xml,而我把控件代码依旧放在setcontenview后面,意味着并不能在activity中找到这些控件,所以绑定监听器的操作不能执行,我应该把控件代码放到fragment的create的方法中去。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐