Java 不识别 Android XML 元素的 ID

标签 java android xml

各位程序员大家好!

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

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

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

这是我的 Activity 代码:

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

<?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>

感谢您的帮助!

最佳答案

尝试从 Project -> Clean 中清理项目并再次build

关于Java 不识别 Android XML 元素的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16389364/

相关文章:

xml - 谷歌通讯录 API : Unauthorized 401 Unknown authorization header

java - Google App Engine Java 应用找不到 javac?

java - 是否可以编写一个新的 "phone" Activity ,如果可以,那么如何编写?

android - 如何在android屏幕(webview)上适应网页?

c# - 我可以将 BaseUri 分配给 XDocument 吗?

javascript - VAST错误: on VASTClient.

java - 多线程、可调用、FutureObject

java - 在 Java 中如何附加到文本文件而不是覆盖它?

java - 如何检测 Java 字符串中的日语文本?

android - 在智能手机和个人电脑/嵌入式设备之间同步时间