这是我的 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);
原文由 jofftiquez 发布,翻译遵循 CC BY-SA 4.0 许可协议