Android: 没有找到这个imageView?
Android: This imageView is not found?
我正在从输入流加载位图并将其放在图像视图中。然后我有一个按钮应该在位图上画一个圆圈。错误是在 onClickListener 中找不到图像视图(称为 'touch')...我该如何解决这个问题?当我按下按钮时,什么也没有发生。 (注:ZoomInZoomOut class是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>
触摸变量在另一个范围内定义。如果您需要更多说明,请查看此 link.
要解决您的问题,请像这样更改您的代码:
final ZoomInZoomOut touch = (ZoomInZoomOut)findViewById(R.id.IMAGEID);
或 如果以后需要从另一个范围访问它,您还可以在 class 中创建一个变量:
private ZoomInZoomOut touch;
并像这样实例化:
touch = (ZoomInZoomOut)findViewById(R.id.IMAGEID);
我正在从输入流加载位图并将其放在图像视图中。然后我有一个按钮应该在位图上画一个圆圈。错误是在 onClickListener 中找不到图像视图(称为 'touch')...我该如何解决这个问题?当我按下按钮时,什么也没有发生。 (注:ZoomInZoomOut class是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>
触摸变量在另一个范围内定义。如果您需要更多说明,请查看此 link.
要解决您的问题,请像这样更改您的代码:
final ZoomInZoomOut touch = (ZoomInZoomOut)findViewById(R.id.IMAGEID);
或 如果以后需要从另一个范围访问它,您还可以在 class 中创建一个变量:
private ZoomInZoomOut touch;
并像这样实例化:
touch = (ZoomInZoomOut)findViewById(R.id.IMAGEID);