nnnnnnitters
nnnnnnitters

Reputation: 65

Android: This imageView is not found?

I'm loading a bitmap from the input stream and putting it on an imageview. THen I have a button that should draw a circle on the bitmap. The error is that the imageview (called 'touch') is not found inside the onClickListener...how do I fix this? When I press the button nothing happens. (Note: ZoomInZoomOut class is an extension of 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();
    }

Here is the 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>

Upvotes: 0

Views: 100

Answers (1)

calimbak
calimbak

Reputation: 866

The touch variable is defined in another scope. If you want more explication, check this link.

To fix your problem change your code like this:

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

Or you can also create a variable in your class if you need to access it from another scope later:

private ZoomInZoomOut touch;

and instantiate like this:

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

Upvotes: 3

Related Questions