Java无法识别Android XML元素的ID

各位程序员好

我正在做一个教程,但遇到了一个我无法解决的障碍。当我像这样设置XML元素到Java的链接时:

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

它无法识别id,即使它与XML中的id完全相同。ivImage1、ivImage2和ivImage3都不起作用,但类中的其余元素起作用。我做错了什么

这是我的活动代码:

包com.frostbytedev.addsub;
导入java.io.IOException;
导入android.app.Activity;
导入android.content.Intent;
导入android.graphics.Bitmap;
导入android.os.Bundle;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.ImageButton;
导入android.widget.ImageView;
公共类照片扩展活动实现OnClickListener{
位图bmp;
图像按钮ib;
按钮b;
ImageView iv、Image1、Image2、Image3、Image4;
意图一;
最终静态int cameraData=0;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.photo);
初始化();
}
私有void初始化(){
//TODO自动生成的方法存根
iv=(ImageView)findViewById(R.id.ivReturnedPic);
b=(按钮)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(此);
ib.setOnClickListener(本);
}
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
开关(v.getId()){
案例R.id.B网球:
试一试{
getApplicationContext().setWallpaper(bmp);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
打破
案例R.id.ibTakePic:
i=新意图(android.provider.MediaStore.ACTION\u IMAGE\u CAPTURE);
startActivityForResult(一,cameraData);
打破
}//闭合开关
}//关闭一次单击
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
//TODO自动生成的方法存根
super.onActivityResult(请求代码、结果代码、数据);
if(resultCode==RESULT\u OK){
Bundle extras=data.getExtras();
bmp=(位图)附加获取(“数据”);
iv.设置图像位图(bmp);
}
}//停课

photo.xml

xml version=“1.0”encoding=“utf-8”>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout\u width=“匹配父项”
android:layout\u height=“match\u parent”
android:orientation=“vertical”>
<图像视图
android:id=“@+id/ivReturnedPic”
android:layout\u width=“fill\u parent”
android:layout_height=“250dp”
android:layout_gravity=“center”
android:src=”https://stackoverflow.com/questions/16389364/@可拉伸/白色“/>
<线性布局
android:layout\u width=“fill\u parent”
android:layout_height=“60dp”
android:orientation=“水平”
android:weightSum=“100”>
<图像按钮
android:id=“@+id/ibTakePic”
android:layout\u width=“fill\u parent”
android:layout\u height=“fill\u parent”
android:layout_weight=“78”
android:background=“@drawable/camera”/>
<按钮
android:id=“@+id/bSetWall”
android:layout\u width=“fill\u parent”
android:layout\u height=“fill\u parent”
android:layout_weight=“22”
android:text=“设为壁纸”/>
</LinearLayout>
<水平滚动视图
android:layout_width=“200dp”
android:layout\u height=“包装内容”
android:layout_gravity=“center”>
<线性布局
android:layout\u width=“fill\u parent”
android:layout\u height=“包装内容”
android:orientation=“horizontal”>
<图像视图
android:id=“@+id/ivImage1”
android:layout_width=“125dp”
android:layout_height=“125dp”
android:padding=“15dp”
android:src=”https://stackoverflow.com/questions/16389364/@可拉拔/史蒂文鲁尔兹”/>
<图像视图
android:id=“@+id/ivImage2”
android:layout_width=“125dp”
android:layout_height=“125dp”
android:padding=“15dp”
android:src=”https://stackoverflow.com/questions/16389364/@可牵引/cat”/>
<图像视图
android:id=“@+id/ivImage3”
android:layout_width=“125dp”
android:layout_height=“125dp”
android:padding=“15dp”
android:src=”https://stackoverflow.com/questions/16389364/@可抽出式/蜂式”/>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>

谢谢你的帮助

尝试从project->Clean中清理项目,然后再次生成它

发表评论