【发布时间】:2013-11-01 08:50:20
【问题描述】:
这是我的 xml,它位于我的活动中出现的片段内。
<FrameLayout
android:id="@+id/frame1"
android:layout_width="wrap_content"
android:layout_height="115dp"
android:layout_margin="2dp"
android:layout_weight="0.33">
<ImageView
android:id="@+id/whoamiwith"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:src="@drawable/default_image" />
</FrameLayout>
这是我的java代码:
@Override
public void onClick(View click) {
if (click == profileBtn) {
whoamiwith.setBackgroundResource(R.drawable.image_i_wanna_put);
}
}
我正在尝试更改图像视图的图像源。没有语法错误,但是当我单击按钮时,模拟器会强制关闭,并且在 logcat 上显示:
java.lang.NullPointerException
它指向线:
whoamiwith.setBackgroundResource(R.drawable.loginbtn);
【问题讨论】:
-
您需要像 click.getId() == profileBtn 一样检查。并分享您的代码如何初始化您的 whoamiwith ?
-
你初始化你的
whoamiwith了吗? -
你的初始化有问题。检查您的图像视图 ID。
-
ImageView whereami = (ImageView) findViewById(R.id.whereami);伙计们,我是这样理解的..
-
你是如何处理“whoamiwith”的 你只初始化了whoamiwith rite?哪里呢?并且您正在尝试通过初始化设置背景图像!
标签: java android xml android-imageview