3

ビットマップ オブジェクトをイメージビューに設定しようとすると、次のエラーが発生します。行の「imageView2を変数に解決できません」: mImg = (ImageView) findViewById(R.id.(imageView2));

コード :

package com.example.ocr01;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //SETTING UP BUTTON AND LISTENER
    Button button = (Button)findViewById(R.id.button1);
    button.setOnClickListener((OnClickListener) this);
}

public void onClick(View v) {
      // do something when the button is clicked
    }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}




//CONVERTING IMAGE TO BITMAP

/*public static Bitmap getBitmapFromURL(String xxx) {
    try {
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}*/

    void create_bitmap(){
        //creating bitmap
        Bitmap source = BitmapFactory.decodeResource(getResources(),
        R.drawable.image1);
        //calling doGreyScale
        doGreyscale(source);
    }

    public static void doGreyscale(Bitmap src) {
        // constant factors
        final double GS_RED = 0.299;
        final double GS_GREEN = 0.587;
        final double GS_BLUE = 0.114;

        // create output bitmap
        Bitmap bmOut = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
        // pixel information
        int A, R, G, B;
        int pixel;

        // get image size
        int width = src.getWidth();
        int height = src.getHeight();

        // scan through every single pixel
        for(int x = 0; x < width; ++x) {
            for(int y = 0; y < height; ++y) {
                // get one pixel color
                pixel = src.getPixel(x, y);
                // retrieve color of all channels
                A = Color.alpha(pixel);
                R = Color.red(pixel);
                G = Color.green(pixel);
                B = Color.blue(pixel);
                // take conversion up to one single value
                R = G = B = (int)(GS_RED * R + GS_GREEN * G + GS_BLUE * B);
                // set new pixel color to output bitmap
                bmOut.setPixel(x, y, Color.argb(A, R, G, B));
            }
        }

        //converting bitmap object to show in imageview2
        ImageView mImg;
        mImg = (ImageView) findViewById(R.id.(imageView2));
        mImg.setImageBitmap(bmOut);

    }

}

同じの 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"
tools:context=".MainActivity" >


<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:src="@drawable/image1" />

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/imageView1"
    android:src="@drawable/ic_launcher" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Button" />

</RelativeLayout>

NB : 私は学期プロジェクトを試す初めての開発者です。どんな種類の助けも大歓迎です。

4

5 に答える 5

8

これを試して:

    }

    //converting bitmap object to show in imageview2
    ImageView mImg;
    mImg = (ImageView) findViewById(R.id.imageView2);
    mImg.setImageBitmap(bmOut);

}

これの代わりに:

    }

    //converting bitmap object to show in imageview2
    ImageView mImg;
    mImg = (ImageView) findViewById(R.id.(imageView2));
    mImg.setImageBitmap(bmOut);

}
于 2013-02-07T06:15:01.123 に答える
4

findViewById(R.id.(imageView2))する必要がありますfindViewById(R.id.imageView2)

imageView2idクラスのメソッドではなく、クラスのフィールドidです。

于 2013-02-07T06:08:31.700 に答える
0

キンガムジックの言う通り

findViewById(R.id.imageView2) である必要があります。これは、「imageView2」として宣言して名前を付けた Resources(R) の id(id) を使用してビューを見つけることを意味します

于 2013-02-07T06:13:39.440 に答える
0

このように試してください ImageView mImg = (ImageView) findViewById(R.id.imageView2);この行はcreateメソッドに書き込みます

 ImageView mImg = (ImageView) findViewById(R.id.imageView2);
 Bitmap bitmapOrg = MainActivity.mImg ;     
 statusimage.setImageBitmap(bitmapOrg);

編集:

 Drawable drawable = mImg.getDrawable();
   if (drawable instanceof BitmapDrawable) {
       BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
       bitmap = bitmapDrawable.getBitmap();
    }
于 2013-02-07T06:14:25.327 に答える