我正在从输入流加载位图并将其放在 ImageView 上。然后我有一个按钮应该在位图上绘制一个圆圈。错误是在 onClickListener 中找不到 imageview(称为“touch”)...我该如何解决这个问题?当我按下按钮时什么也没有发生。 (注:ZoomInZoomOut类是Imageview的扩展)
try {
java.io.FileInputStream in = this.openFileInput(path);
Bitmap bitmap = BitmapFactory.decodeStream(in);
ZoomInZoomOut touch = (ZoomInZoomOut)findViewById(R.id.IMAGEID);
touch = arrangeImageView(touch);
touch.setImageBitmap(bitmap);
in.close();
Button draw = (Button) findViewById(R.id.draw);
draw.setOnClickListener(new View.OnClickListener(){
public void onClick(View v)
{
Bitmap imageBitmap = Bitmap.createBitmap(200,
200, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(imageBitmap);
Paint p = new Paint();
p.setAntiAlias(true);
p.setColor(Color.BLUE);
canvas.drawCircle(60, 50, 25, p);
/////////////////////Error is on the next line:
touch.setImageBitmap(imageBitmap);
}
});
} catch (Exception e) {
e.printStackTrace();
}
这是 XML:
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:background="@drawable/background"
tools:context="com.commonsware.android.test1.ImageDisplayActivity"
>
<com.commonsware.android.test1.ZoomInZoomOut
android:id="@+id/IMAGEID"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="o"
android:id="@+id/draw"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
最佳答案
touch 变量是在另一个作用域中定义的。如果您想了解更多说明,请查看此 link .
要解决您的问题,请更改您的代码,如下所示:
final ZoomInZoomOut touch = (ZoomInZoomOut)findViewById(R.id.IMAGEID);
或者如果您稍后需要从另一个作用域访问它,您也可以在类中创建一个变量:
private ZoomInZoomOut touch;
并像这样实例化:
touch = (ZoomInZoomOut)findViewById(R.id.IMAGEID);
关于java - Android:找不到这个imageView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29108728/