【问题标题】:Android: This imageView is not found?Android:找不到这个imageView?
【发布时间】:2015-03-17 20:07:17
【问题描述】:

我正在从输入流中加载位图并将其放在图像视图上。然后我有一个按钮,应该在位图上画一个圆圈。错误是在 onClickListener 中找不到图像视图(称为“触摸”)......我该如何解决这个问题?当我按下按钮时,没有任何反应。 (注: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>

【问题讨论】:

  • 你也可以发布你的xml吗?
  • 请查看更新后的帖子。

标签: java android button imageview onclicklistener


【解决方案1】:

touch 变量是在另一个作用域中定义的。如果你想了解更多,请查看link

要解决您的问题,请像这样更改您的代码:

final ZoomInZoomOut touch = (ZoomInZoomOut)findViewById(R.id.IMAGEID);

或者如果您以后需要从另一个范围访问它,您也可以在您的类中创建一个变量:

private ZoomInZoomOut touch;

并像这样实例化:

touch = (ZoomInZoomOut)findViewById(R.id.IMAGEID);

【讨论】:

  • 第一种方式比较好,除非变量实际需要类作用域
  • 第一个比较好,但是不允许他做下面的那一行:touch=arrangeImageView(touch);
  • @GaryBak 是的,错过了这一点,但可能有更好的方法来做任何试图在那里做的事情。我假设添加params 或类似
  • 谢谢...但是我现在当圆圈被绘制到屏幕上时,它并没有被添加到图像上,它只是删除了那里的位图并创建了一个大圆圈。如何使圆圈出现在原始图像视图上?
猜你喜欢
  • 1970-01-01
  • 2020-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多