android 以编程方式更改图像视图
android change imageview programmatically
我正在制作一个 android 应用程序,每次用户输入一个值时都会显示相同的图像视图。但我希望每次用户输入值时更改 imageview。我写了一个代码来执行此操作,但问题是在显示第一张图片后我遇到了黑屏,它把我带到了应用程序的第一页,它没有崩溃,只是显示黑屏。
这里是 xml 文件中的 imageview 代码:
<ImageView
android:id="@+id/ImageSuccess"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_marginBottom="20dip"
android:layout_marginTop="20dip"
android:scaleType="centerInside"
android:src="@drawable/image1" />
我在这里更改了 java 文件中的图像视图:
ImageView myImage= (ImageView) findViewById(R.id.ImageSuccess);
if(userVlue.equals("SCHOOL")){
myImage.setImageResource(R.drawable.image1);
}
else if(userVlue.equals("CAR")){
myImage.setImageResource(R.drawable.image2);
}
else if(userVlue.equals("TOY")){
myImage.setImageResource(R.drawable.image3);
}
myImage 或 userVlue 之一未正确设置,可能为空。
调试和共享日志。你的 partcular activity 可能因为 nullpointexception 而被破坏,因此在回溯中返回到 main activity。
我尝试了一个示例应用程序,您的逻辑运行良好。
您正在处理某些 UI 元素上的用户操作,当您执行 findViewById 时,cotext 就是那个 UI 元素,因此此方法在它的子元素中搜索 ImageSuccess。尝试将页面布局作为根布局并执行 rootlayout.findViewById("ImageSuccess");
终于找到解决办法了,出现黑屏是因为图片只存在于drawable文件中,所以我需要将图片添加到drawable-ldp、drawable-ldpi等所有drawbridge文件夹中.... , 这样该应用程序就可以在不同的 phone 大小下运行。
我正在制作一个 android 应用程序,每次用户输入一个值时都会显示相同的图像视图。但我希望每次用户输入值时更改 imageview。我写了一个代码来执行此操作,但问题是在显示第一张图片后我遇到了黑屏,它把我带到了应用程序的第一页,它没有崩溃,只是显示黑屏。
这里是 xml 文件中的 imageview 代码:
<ImageView
android:id="@+id/ImageSuccess"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_marginBottom="20dip"
android:layout_marginTop="20dip"
android:scaleType="centerInside"
android:src="@drawable/image1" />
我在这里更改了 java 文件中的图像视图:
ImageView myImage= (ImageView) findViewById(R.id.ImageSuccess);
if(userVlue.equals("SCHOOL")){
myImage.setImageResource(R.drawable.image1);
}
else if(userVlue.equals("CAR")){
myImage.setImageResource(R.drawable.image2);
}
else if(userVlue.equals("TOY")){
myImage.setImageResource(R.drawable.image3);
}
myImage 或 userVlue 之一未正确设置,可能为空。 调试和共享日志。你的 partcular activity 可能因为 nullpointexception 而被破坏,因此在回溯中返回到 main activity。
我尝试了一个示例应用程序,您的逻辑运行良好。
您正在处理某些 UI 元素上的用户操作,当您执行 findViewById 时,cotext 就是那个 UI 元素,因此此方法在它的子元素中搜索 ImageSuccess。尝试将页面布局作为根布局并执行 rootlayout.findViewById("ImageSuccess");
终于找到解决办法了,出现黑屏是因为图片只存在于drawable文件中,所以我需要将图片添加到drawable-ldp、drawable-ldpi等所有drawbridge文件夹中.... , 这样该应用程序就可以在不同的 phone 大小下运行。