2013-05-05 3 views
12

안녕하세요!Java가 Android XML 요소의 ID를 인식하지 못함

자습서를 진행하고 있지만 해결할 수없는 걸림돌이 발생했습니다. 나는이 같은 자바 XML 요소의 링크를 설정하는 경우 :는 XML의 정확한 한 경우에도

Image1 = (ImageView) findViewById(R.id.ivImage1); 

이 ID를 인식하지 못합니다. ivImage1 ivImage2 및 ivImage3 모두는 작동하지 않지만 클래스의 나머지 요소는 작동합니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까? 도움을

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <ImageView 
     android:id="@+id/ivReturnedPic" 
     android:layout_width="fill_parent" 
     android:layout_height="250dp" 
     android:layout_gravity="center" 
     android:src="@drawable/white" /> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="60dp" 
     android:orientation="horizontal" 
     android:weightSum="100"> 

     <ImageButton 
      android:id="@+id/ibTakePic" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="78" 
      android:background="@drawable/camera" /> 

     <Button 
      android:id="@+id/bSetWall" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="22" 
      android:text="Set as Wallpaper" /> 
    </LinearLayout> 

    <HorizontalScrollView 
     android:layout_width="200dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center"> 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 

      <ImageView 
       android:id="@+id/ivImage1" 
       android:layout_width="125dp" 
       android:layout_height="125dp" 
       android:padding="15dp" 
       android:src="@drawable/stevenrulz" /> 

      <ImageView 
       android:id="@+id/ivImage2" 
       android:layout_width="125dp" 
       android:layout_height="125dp" 
       android:padding="15dp" 
       android:src="@drawable/cat" /> 

      <ImageView 
       android:id="@+id/ivImage3" 
       android:layout_width="125dp" 
       android:layout_height="125dp" 
       android:padding="15dp" 
       android:src="@drawable/bee" /> 
     </LinearLayout> 
    </HorizontalScrollView> 
</LinearLayout> 

감사

package com.frostbytedev.addsub; 

import java.io.IOException; 
import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.ImageButton; 
import android.widget.ImageView; 

public class Photo extends Activity implements OnClickListener { 
    Bitmap bmp; 
    ImageButton ib; 
    Button b; 
    ImageView iv, Image1, Image2, Image3, Image4; 
    Intent i; 
    final static int cameraData = 0; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.photo); 
     initialize(); 
    } 

    private void initialize() { 

     // TODO Auto-generated method stub 
     iv = (ImageView) findViewById(R.id.ivReturnedPic); 
     b = (Button) findViewById(R.id.bSetWall); 
     ib = (ImageButton) findViewById(R.id.ibTakePic); 
     Image1 = (ImageView) findById(R.id.ivImage1); 
     Image2 = (ImageView) findById(R.id.ivImage2); 
     Image3 = (ImageView) findById(R.id.ivImage3); 
     Image4 = (ImageView) findById(R.id.ivImage4); 
     b.setOnClickListener(this); 
     ib.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     switch(v.getId()){ 
     case R.id.bSetWall: 
      try { 
       getApplicationContext().setWallpaper(bmp); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      break; 

     case R.id.ibTakePic: 
      i= new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      startActivityForResult(i, cameraData); 
      break; 




     }//Closes Switch 
    }//Closes OnClick 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     // TODO Auto-generated method stub 
     super.onActivityResult(requestCode, resultCode, data); 
     if(resultCode == RESULT_OK){ 
     Bundle extras = data.getExtras(); 
     bmp = (Bitmap) extras.get("data"); 
     iv.setImageBitmap(bmp); 
     } 
}//Closes Class 

photo.xml :

여기 내 활동 코드입니다!

답변

14

프로젝트를 Project -> Cleanbuild에서 다시 청소 해보십시오.

+0

와우 감사합니다. 일했다! – Bauss

+2

이것을 허용 된 대답으로 표시 할 수 있습니다. –

1

프로젝트를 정리하고 빌드하고 xml에 레이아웃이나 위젯을 추가 한 후 먼저 저장해야합니다.

관련 문제