[Android]新版的sdk中新建一个android应用,增加的PlaceholderFragment这个静态类发生的事情
2014-05-26 22:57
417 查看
1,首先发生的是有两个布局xml,一个activity_main.xml,一个是fragment_main.xml一开始没在意,后来仔细看了原来是新功能的fragment概念等于多个场景在这个activity中切换,
按照原来的理念就是
在原本的activity中添加这些对xml控件操作的一些代码,后来发现运行时候报错,错误在12行。
仔细搜索了一下,各种百度一下,发现是因为我这些控件都是在fragment的xml中,原本的activity直接就加入了fragment的xml场景,等于默认的activity_main.xml并没有运作,直接加载了另一个xml,而我把控件代码依旧放在setcontenview后面,意味着并不能在activity中找到这些控件,所以绑定监听器的操作不能执行,我应该把控件代码放到fragment的create的方法中去。
按照原来的理念就是
@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的方法中去。
相关文章推荐
- 如何解决:新建Android程序的时候发生了找不到/sdk/tools/lib/proguard.cfg文件的错误
- android学习笔记(1)---eclipse中新建一个Android工程如何选择sdk
- 在项目中用到datatables比较多,但是当datatables加载超过1000多的时候就会出现有点缓慢的的样子,这个时候如果继续增加数据量让datatables去处理,那会是一个让人抓狂的事情,幸
- Android ADT,SDK 更新后一个问题:新建项目layout和src文件夹为空
- 请教一个问题,Android应用运行时在调用某个API(比如相机)时,系统如何判断这个应用是否具备相应的权限?
- 如何解决:新建Android程序的时候发生了找不到 /android-sdk-windows/tools/lib/proguard.cfg文件 的错误
- 如何解决:新建Android程序的时候发生了找不到/sdk/tools/lib/proguard.cfg文件的错误
- UNIX网络编程卷1:应用进程写数据到一个TCP套接字中发生的事情
- android7.1增加一个开机自启动的bin应用遇到的权限问题
- 如何解决:新建Android程序的时候发生了找不到 /android-sdk-windows/tools/lib/proguard.cfg文件 的错误
- 如何解决:新建Android程序的时候发生了找不到 \android-sdk-windows\tools\lib\proguard.cfg文件 的错误
- 请教一个问题,Android应用运行时在调用某个API(比如相机)时,系统如何判断这个应用是否具备相应的权限?
- 请教一个问题,Android应用运行时在调用某个API(比如相机)时,系统如何判断这个应用是否具备相应的权限?
- 如何解决:新建Android程序的时候发生了找不到 \android-sdk-windows\tools\lib\proguard.cfg文件 的错误
- 如何解决:新建Android程序的时候发生了找不到 \android-sdk-windows\tools\lib\proguard.cfg文件 的错误
- 如何解决:新建Android程序的时候发生了找不到 /android-sdk-windows/tools/lib/proguard.cfg文件 的错误
- Android高手进阶教程(十二)之----Android 在一个应用中如何启动另外一个已安装的应用!
- Android - 如何将两个/多个应用放到一个进程中去?
- Android 在一个应用中如何启动另外一个已安装的应用