【发布时间】:2013-08-06 13:25:41
【问题描述】:
在我的主布局中(在创建时设置)我有一些包含的布局,其中有一个imageview。在我的代码中的某些地方,我需要为这些 imageViews 设置位图。我的方法是这样的:
(ImageView) ((findViewById(R.id.first_app + (i - 1)).findViewById(R.id.app_image)));
其中我是循环变量。 问题是我的 findViewById(R.id.first_app + (i - 1) 为空,但这些布局存在于主布局中。
请帮我找出这个错误的原因。我真的很困惑,我快截止日期了。 这是我的主要布局 xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="3"
tools:context=".MoreAppsActivity" >
<RelativeLayout
android:id="@+id/more_app_title"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center"
android:background="#EA7026">
<ImageView
android:id="@+id/more_app_title_img"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@android:color/transparent"
android:src="@drawable/more_app_title_image"/>
</RelativeLayout>
<include
android:id="@+id/first_app"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
layout="@layout/single_app_layout" />
<include
android:id="@+id/second_app"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
layout="@layout/single_app_layout" />
<include
android:id="@+id/third_app"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
layout="@layout/single_app_layout" />
<RelativeLayout
android:id="@+id/more_app_subtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="60dp"
android:gravity="center"
android:background="#DBBE00">
<ImageButton
android:id="@+id/more_app_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:src="@drawable/more_app_threecircle"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:layout_below="@id/more_app_button"
android:layout_marginTop="5dp"
android:text="?????? ??? ?????"/>
</RelativeLayout>
</LinearLayout>
并包含布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
>
<ImageView
android:id="@+id/app_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"/>
<LinearLayout
android:id="@+id/app_text_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
android:weightSum="100">
<TextView
android:id="@+id/app_text"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="30"
android:background="#88000000"
android:text="aligholi salavat"
android:textSize="22sp"
android:gravity="center"
android:textColor="@android:color/white"/>
</LinearLayout>
</FrameLayout>
【问题讨论】:
-
也许我不知道你知道的东西,但你想用 findViewById(R.id.xyz+(i-1)) 做什么?
-
R.id 是一个整数。由于我包含的布局是连续的,它们的 id 是连续的两个。 (区别是一)。我在循环中使用它来防止代码冗余。
-
@alireza : R.id 可能会解析为整数,但
R.id.first_app(例如)实际上只是R.java类文件中static final字段的名称。你不能把它当作一个字符串来处理,而只是附加数字。 -
@Squonk:实际上我几天前在我的应用程序很小的时候得到了正确的结果。我用框架布局替换了包含的布局,并为它们提供了唯一的 id,但我仍然得到空异常。似乎主布局没有正确创建。
-
@alireza :我的意思是......不要这样做。 R.java 文件是自动生成的,分配给任何资源 id 的整数可以随时更改。你不能相信你尝试使用的代码会得到你期望的资源。