CEN*_*EDE 28 java xml android android-imageview
这是我的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>
Run Code Online (Sandbox Code Playgroud)
这是我的java代码:
@Override
public void onClick(View click) {
if (click == profileBtn) {
whoamiwith.setBackgroundResource(R.drawable.image_i_wanna_put);
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试更改图像视图的图像源.没有语法错误但是当我单击按钮时,模拟器正在给我一个强制关闭,并且在logcat上它说:
显示java.lang.NullPointerException
它指向这条线:
whoamiwith.setBackgroundResource(R.drawable.loginbtn);
Run Code Online (Sandbox Code Playgroud)
Bha*_*rma 60
whoamiwith.setImageResource(R.drawable.loginbtn);
Run Code Online (Sandbox Code Playgroud)
小智 9
ImageView whoamiwith = (ImageView)findViewById(R.id.whoamiwith)
Drawable new_image= getResources().getDrawable(R.drawable.loginbtn);
whoamiwith.setBackgroundDrawable(new_image);
Run Code Online (Sandbox Code Playgroud)
你试一试
ImageView whoamiwith = (ImageView)findViewById(R.id.whoamiwith)
whoamiwith.setImageResource(R.id.new_image);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
73806 次 |
| 最近记录: |