1
我在Android 4.4.4/4.4.2/5.0.2中使用了findViewById()。findViewById在Android 4.4.4中只返回null
但4.4.4只有错误'NullpointException'。
这是我在MainActivity中的代码。
FrameLayout fl = (FrameLayout) this.findViewById(Util.StringToResourceID(this, "id", "main_map_fl"));
所有的android都没有错误,但只在4.4.4中返回null。
然而,
Util.StringToResourceID(this, "id", "main_map_fl")
它返回值。非空。
我在该代码之前试过setContentView。
我也是第一次尝试过干净的项目。
我不知道为什么会引起错误。请帮帮我。
+)
这里是一个具有 'main_map_fl' 我的.XML代码。
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_rl"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<include
android:id="@+id/main_tbt_ll"
android:layout_width="200dp"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
layout="@layout/layout_sub_tbt"
android:visibility="visible" />
<FrameLayout
android:id="@+id/main_map_fl"
android:layout_width="fill_parent"
android:layout_height="fill_parent
android:layout_toLeftOf="@+id/main_tbt_ll"
android:background="#ffff0000" >
</FrameLayout>
<ImageView
android:id="@+id/main_tbt_shadow_iv"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_toLeftOf="@+id/main_tbt_ll"
android:background="@drawable/shadow_w" />
<LinearLayout
android:id="@+id/main_action_ll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<TextView
android:id="@+id/main_action_set_admin_tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:gravity="center"
android:background="#b2333333"
style="@style/font_31p_255_255_255"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/bottom_bar_bg" >
<ImageButton
android:background="@drawable/btn_main_actionbar_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/main_action_back_ib"
android:layout_gravity="center_vertical"/> ......
</LinearLayout>
</LinearLayout>
++)
Util.StringToResourceID
它是包名和类的字符串为int,我想。
public static int StringToResourceID(Context context, String _class, String _field) {
int rval = 0;
String a = context.getPackageName() + ".R$" + _class;
try {
Class Ra = Class.forName(a);
try {
rval = Ra.getField(_field).getInt(Ra);
} catch (IllegalArgumentException var7) {
...........
return rval;
}
什么是... Util.StringToResourceID –
我编辑了我的quenstion! @MartinMarconcini – parfum
那么,返回什么号码?看看R.java并找到它 –