Android 小发现:xml里定义的组件取出始终为null
2012-02-24 10:53
267 查看
先看下列xml代码:
<?xml
version="1.0"
encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/wordBgRL"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/dictionaryButton"
android:background="@drawable/dictionary_btn_bg"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
style="@style/WrapContentWidgetStyle"
/>
</RelativeLayout>
在java代码里取出wordBgRL 始终为null;
wordBgRL = (RelativeLayout)findViewById(R.id.wordBgRL); // wordBgRL alwasys == null
dictionaryButton = (Button)findViewById(R.id.dictionaryButton); // dictionaryButton != null
后来发现你必须自己另写一个RelativeLayout,可能因为 xmlns:android="http://schemas.android.com/apk/res/android" 它存在的原因吧。
<?xml
version="1.0"
encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:id="@+id/wordBgRL"
style="@style/FillParentWidgetStyle"
>
<Button
android:id="@+id/dictionaryButton"
android:background="@drawable/dictionary_btn_bg"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
style="@style/WrapContentWidgetStyle"
/>
</RelativeLayout>
</RelativeLayout>
<?xml
version="1.0"
encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/wordBgRL"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/dictionaryButton"
android:background="@drawable/dictionary_btn_bg"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
style="@style/WrapContentWidgetStyle"
/>
</RelativeLayout>
在java代码里取出wordBgRL 始终为null;
wordBgRL = (RelativeLayout)findViewById(R.id.wordBgRL); // wordBgRL alwasys == null
dictionaryButton = (Button)findViewById(R.id.dictionaryButton); // dictionaryButton != null
后来发现你必须自己另写一个RelativeLayout,可能因为 xmlns:android="http://schemas.android.com/apk/res/android" 它存在的原因吧。
<?xml
version="1.0"
encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:id="@+id/wordBgRL"
style="@style/FillParentWidgetStyle"
>
<Button
android:id="@+id/dictionaryButton"
android:background="@drawable/dictionary_btn_bg"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
style="@style/WrapContentWidgetStyle"
/>
</RelativeLayout>
</RelativeLayout>
相关文章推荐
- Android 中使用xml定义各种Drawable
- android 使用xml定义自己的View
- 在做android发现""和null竟然不一样。。靠。。 记住啊啊啊啊
- Android中发现的方向键旋转定义
- Android自己定义组件系列【3】——自己定义ViewGroup实现側滑
- 【实训】Android中xml动画定义
- android 自定义属性的使用一、在res/values文件下定义一个attrs.xml文件,代码如下
- Android 用xml定义带阴影的图形
- 在xml中定义EditText出现“EditText No label views point to this text field with an android:labelFor”
- Android工程clean之后发现不能生产R.java,那么应该是main.xml之类的地方出错了。修改后重新编译即可
- Android中实现双缓冲(画板应用)和XML文件定义菜单
- 使用XML定义组件样式
- Android 使用xml定义Drawable--第一篇:概述以和Shape Drawable示例
- Android中通过xml资源文件定义数组
- Android strings.xml文件定义字符串中的标签
- android通过style.xml文件定义相同控件的通用属性
- 【android】在xml文件中定义drawable数组、id数组等
- Activity no title 直接在AndroidManifest.xml定义
- 关于android的animation的xml定义中的android:interpolator属性的含义
- Android XML文件定义4种动画