findviewbyid() 在 Eclipse 中的自定义适配器中返回 null
欢迎来到另一个 findviewbyid() returns null
问题!
我编写了一个适配器类,用于将数组显示到 ListView 中。猜猜 findviewbyid() 返回 null 是什么。 (我检查了有关此主题的所有其他问题,但它们与我的问题无关)。
这是我的适配器类:
package de.kosmowski.discogs.wants;
import java.util.ArrayList;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import de.kosmowski.discogs.model.Want;
public class WantAdapter extends ArrayAdapter<Want> {
private ArrayList<Want> items;
public WantAdapter(Context context, int textViewResourceId, ArrayList<Want> items) {
super(context, textViewResourceId, items);
this.items = items;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
Want w = items.get(position);
Log.d("WantAdapter", "want:" + w);
if (v == null) {
LayoutInflater vi = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(w != null && w.getType()!= null && (w.getType().equals(Want.TYPE_RELEASE) || w.getType().equals(Want.TYPE_MASTER)))
{
Log.d(this.getClass().getCanonicalName(), "Choosing Release List Item");
v = vi.inflate(R.layout.releaselistitem, null);
}
else
{
v = vi.inflate(R.layout.lplistitem, null);
}
}
if (w != null) {
ImageView icon = (ImageView) v.findViewById(R.id.typeicon); //Returns NULL at this line
TextView tt = (TextView) v.findViewById(R.id.text1);
if(w.getType() != null)
{
if(w.getType().equals(Want.TYPE_ARTIST))
{
icon.setImageResource(R.drawable.resulttype_artist);
}
else if(w.getType().equals(Want.TYPE_RELEASE))
{
icon.setImageResource(R.drawable.resulttype_lp);
}
else if(w.getType().equals(Want.TYPE_MASTER))
{
icon.setImageResource(R.drawable.resulttype_lp_master);
}
else
{
icon.setImageResource(R.drawable.defaultresult);
}
}
else
{
icon.setImageResource(R.drawable.defaultresult);
}
if (tt != null) {
tt.setText(w.getName()); }
}
return v;
}
}
我标记了返回 null 的行。
这是膨胀的 xml 文件(lplistitem 和releaselistitem 目前完全相同):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<ImageView android:scaleType="center" android:layout_gravity="center_vertical" android:src="@drawable/defaultresult" android:id="@+id/typeicon" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:textSize="16px"
android:minHeight="?android:attr/listPreferredItemHeight"
/>
</LinearLayout>
您能看到任何明显的错误吗?对我来说一切似乎都是对的。
Welcome to another findviewbyid() returns null
question!
I wrote an adapter class for displaying an array into a ListView. And gues what findviewbyid() returns null. (I checked all the other questions on this topic but they're not related to my problem).
So here is my Adapter Class:
package de.kosmowski.discogs.wants;
import java.util.ArrayList;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import de.kosmowski.discogs.model.Want;
public class WantAdapter extends ArrayAdapter<Want> {
private ArrayList<Want> items;
public WantAdapter(Context context, int textViewResourceId, ArrayList<Want> items) {
super(context, textViewResourceId, items);
this.items = items;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
Want w = items.get(position);
Log.d("WantAdapter", "want:" + w);
if (v == null) {
LayoutInflater vi = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(w != null && w.getType()!= null && (w.getType().equals(Want.TYPE_RELEASE) || w.getType().equals(Want.TYPE_MASTER)))
{
Log.d(this.getClass().getCanonicalName(), "Choosing Release List Item");
v = vi.inflate(R.layout.releaselistitem, null);
}
else
{
v = vi.inflate(R.layout.lplistitem, null);
}
}
if (w != null) {
ImageView icon = (ImageView) v.findViewById(R.id.typeicon); //Returns NULL at this line
TextView tt = (TextView) v.findViewById(R.id.text1);
if(w.getType() != null)
{
if(w.getType().equals(Want.TYPE_ARTIST))
{
icon.setImageResource(R.drawable.resulttype_artist);
}
else if(w.getType().equals(Want.TYPE_RELEASE))
{
icon.setImageResource(R.drawable.resulttype_lp);
}
else if(w.getType().equals(Want.TYPE_MASTER))
{
icon.setImageResource(R.drawable.resulttype_lp_master);
}
else
{
icon.setImageResource(R.drawable.defaultresult);
}
}
else
{
icon.setImageResource(R.drawable.defaultresult);
}
if (tt != null) {
tt.setText(w.getName()); }
}
return v;
}
}
I marked the line where null is returned.
And here is the inflated xml file (both the lplistitem and the releaselistitem are currently exactly the same):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<ImageView android:scaleType="center" android:layout_gravity="center_vertical" android:src="@drawable/defaultresult" android:id="@+id/typeicon" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:textSize="16px"
android:minHeight="?android:attr/listPreferredItemHeight"
/>
</LinearLayout>
Can you see any obvious errors? Everything seems right to me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
所以这个问题确实与eclipse有关。在我做了你提到的所有事情并用谷歌搜索我的双手之后,我认为检查 Eclipse 是否运行正常可能是个好主意。
因此,由于某种原因,eclipse向我显示的布局文件不是正在编译的文件。因此,由于这个原因,日食中的一切看起来都很好。
然后我直接在文件系统中查看了同一文件,这仍然是旧文件。 所以我刚刚刷新了我的 Eclipse 项目,瞧,旧文件出现在 Eclipse 中,我再次编辑了它。 我不知道为什么会发生这种情况,这让我害怕 Eclipse 可能会出现更多不透明的问题。
几天前我遇到了一个与此类似的问题。
请参阅 RuntimeException with Android OptionsMenu
问题出在哪里,R.java 文件没有自动重新编译添加新的 id 资源后。
我现在要清理我的 Eclipse 安装并从头开始安装所有内容,以确保在安装过程中没有发生重大错误。
感谢您的回答!
So the problem was really related to eclipse. After i did everything you mentioned and googled my hands to the bone i thought it might be a good idea to check if eclipse is doing right.
So for some reason the layout file eclipse displayed to me was not the file which was getting compiled. So for that reason everything looked fine in eclipse.
I then had a look at the same file directly in the File System and this was still the old one. So i just refreshed my eclipse project and voila the old file showed up in eclipse and i edited it again. That was ist. I don't know why this happend and this scares my there could be more of those untransparent problems with eclipse.
I had a problem a few days ago that was kind of similar to it.
see RuntimeException with Android OptionsMenu
Where the problem was, that the R.java file was not recompiled automaticly after adding a new id resource.
I'm going to clean up my eclipse installation now and install everything from the scratch just to make sure there was no major mistake happened during my installation.
Thanks for your answers!
尝试使用 /> 终止 ImageView XML 条目而不是...>< /ImageView>
如果不是这样,那么尝试一一删除每个属性(如layout_gravity等)
祝你好运。
Try terminating the ImageView XML entry with /> instead of ...>< / ImageView>
If not that, then try removing each of the attributes one-by-one (like layout_gravity, etc)
Good luck.
我不想尝试使用 LayoutInflater 变量,而是尝试在一行中膨胀布局。另外,我很想将上下文存储为私有变量,以确保使用正确的上下文。因此,这就是我将尝试在代码中做不同的事情:
我将保持这些行下面的其余代码相同。尝试一下!
I would be tempted to not try and have a LayoutInflater variable but try to inflate the layout in one line. Also i would be tempted to store the Context as a private variable to ensure the right context is being used. So heres what i would try doing differently in your code:
I would keep the rest of the code below these lines the same. Give that a try!