这是我的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp">
<LinearLayout
android:id="@+id/line111"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<ImageView
android:id="@+id/book_image_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/book1_pic" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.8"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="标题:" />
<EditText
android:id="@+id/book_name_edit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="1dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="作者:" />
<EditText
android:id="@+id/book_author_edit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="1dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="出版社:" />
<EditText
android:id="@+id/book_press_edit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="1dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="出版时间:" />
<EditText
android:id="@+id/book_presstime_edit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="1dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="ISBN:" />
<EditText
android:id="@+id/book_isbn_edit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="1dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
//初始化控件 onCreate(){ //在onCreate()内
setContentview(上面那个布局文件); //略写
ImageView book_pic_edit=(ImageView)findViewById(R.id.book_image_edit);
EditText book_name=(EditText) findViewById(R.id.book_name_edit);
EditText author=(EditText)findViewById(R.id.book_author_edit);
EditText press=(EditText) findViewById(R.id.book_press_edit);
EditText press_time=(EditText)findViewById(R.id.book_presstime_edit);
EditText isbn=(EditText) findViewById(R.id.book_isbn_edit);
Spinner reading_state=(Spinner) findViewById(R.id.spinner_readingstate);
Spinner bookshelf=(Spinner) findViewById(R.id.spinner_bookshelf_edit);
EditText book_source=(EditText) findViewById(R.id.book_source_edit);
}
这里一个问题就是, ImageView book_pic_edit=(ImageView)findViewById(R.id.book_image_edit); book_pic_edit为null??? 为什么会这样 其他的都有值
请大神搭救!!!
android:id="@+id/line111"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
:layout_width="0dp"???????
1.清除缓存,
2.把这个imageview删除重新添加一个
3.换别的id名字试一下,看看会不会返回null,
如果都不行,把页面删除重新添加试试^_^
android:layout_width="match_parent"
android:layout_height="200dp"> -----这里面没有android:orientation
<LinearLayout
android:id="@+id/line111"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<ImageView
android:id="@+id/book_image_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/book1_pic" />
</LinearLayout>
代码不全我只能帮你分析下:
1、setContentView中是否是你当前的layout布局
2、book_pic_edit当前这个ID被你以前的布局所占用导致的,你可以clean下代码在试试
3、把ID重新命名一个试试
setContentView没有找到对应的布局的id
首先确定你的setContentView的xml是这个(我看到你的很多id并没有在布局中有)
然后修改imageview的id再试试能不能find到
如果都不能那么清除as缓存:菜单栏>file>invalid cache and restart
你这个问题主要是R资源类没有编译成功imageview的id,你去R类中找找看,找不到就说明没有编译上,把项目重新build一下。
尝试在build菜单下先clean project,然后reBuild project
我也是这个问题,很奇怪,而且后面我添加了一个button控件测试,发现不为空,但是imageView的返回还是null