2013-02-07 23 views
1

我收到以下錯誤,當我嘗試將位圖對象設置爲imageview。 「imageView2 can not be resolved to a variable」for the line:mImg =(ImageView)findViewById(R.id.(imageView2));在android中的位圖imageview在android

CODE:

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:我是第一次開發者,希望我的學期項目。歡迎任何形式的幫助。

+1

爲什麼R.id.(imageView2)? – smk

回答

1

findViewById(R.id.(imageView2))應該是findViewById(R.id.imageView2)

imageView2是類id上的字段而不是類id上的方法。

+0

刪除了括號,但仍然收到錯誤「R的原始類型int沒有字段ID」@Kingamajick –

+0

如果您在輸入R.id後在eclipse中進行開發。按下Ctrl +空格它將列出所有可能的Id's – Vins

+0

自從添加了imageView2以來,您是否重新構建了R文件? – Kingamajick

0

至於說通過Kingamjick

應當findViewById(R.id.imageView2)這意味着通過使用資源(R)標識(ID)找到該視圖,您聲明並命名爲「imageView2」

+0

刪除括號但仍收到錯誤「R的原始類型int沒有字段ID」 @vins –

+0

可能是它沒有獲取資源文件,請確保在您的eclipse項目中有gen文件夾後和R.java文件。如果仍然存在問題,那麼請嘗試清理一次項目 – Vins

+0

已經清理過的項目和是的,有一個R.java文件的gen文件夾。仍然存在錯誤。 –

0

嘗試這樣ImageView mImg = (ImageView) findViewById(R.id.imageView2);這行寫上創建方法

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(); 
    } 
+0

編寫行 ImageView mImg =(ImageView)findViewById(R.id.imageView2);在onCreate中的 確實有助於消除錯誤,但我爲什麼使用MySipDetails.mImg? 不會mImg.setImageBitmap(bmOut);做這份工作? –

+0

請參閱'MySipDetails'編輯代碼使用您的活動名稱'MainActivity' – NagarjunaReddy

+0

無法將圖像轉換爲位圖bitmapOrg –

4

試試這個:

} 

    //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); 

}