Java get view方法在生成对象之前崩溃
每次我运行应用程序时,下面脚本中的get view方法都会崩溃(错误指向该方法)。这是错误:Java get view方法在生成对象之前崩溃,java,android,android-arrayadapter,Java,Android,Android Arrayadapter,每次我运行应用程序时,下面脚本中的get view方法都会崩溃(错误指向该方法)。这是错误: 07-11 17:25:01.147 8512-8512/com.example.android.quakereport E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.android.q
07-11 17:25:01.147 8512-8512/com.example.android.quakereport E/AndroidRuntime:
FATAL EXCEPTION: main
Process: com.example.android.quakereport, PID: 8512
android.content.res.Resources$NotFoundException: Resource ID #0x0
这是getview方法代码中的ArrayAdapter:
public class EarthQuakeAdapter extends ArrayAdapter<Earthquake> {
public EarthQuakeAdapter(Activity context, ArrayList<Earthquake> Earthquakes) {
// Here, we initialize the ArrayAdapter's internal storage for the context and the list.
// the second argument is used when the ArrayAdapter is populating a single TextView.
// Because this is a custom adapter for two TextViews and an ImageView, the adapter is not
// going to use this second argument, so it can be any value. Here, we used 0.
super(context, 0, Earthquakes);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
return super.getView(position, convertView, parent);
}
Earthquake currentEarthquake = getItem(position);
TextView magnitude = (TextView) listItemView.findViewById(R.id.magnitude);
// Get the version name from the current AndroidFlavor object and
// set this text on the name TextView
magnitude.setText(Earthquake.getmMagnitude());
// Find the TextView in the list_item.xml layout with the ID version_number
TextView place = (TextView) listItemView.findViewById(R.id.Place); // Get the version number from the current AndroidFlavor object and
// set this text on the number TextView
place.setText(Earthquake.getMplace());
// Find the ImageView in the list_item.xml layout with the ID list_item_icon
TextView date = (TextView) listItemView.findViewById(R.id.Date);
date.setText(Earthquake.getmDate());
// Get the image resource ID from the current AndroidFlavor object and
// set the image to iconView
// Return the whole list item layout (containing 2 TextViews and an ImageView)
// so that it can be shown in the ListView
return listItemView;
}
公共类地震适配器扩展ArrayAdapter{
公共地震适配器(活动背景、阵列列表地震){
//这里,我们为上下文和列表初始化ArrayAdapter的内部存储。
//第二个参数在ArrayAdapter填充单个TextView时使用。
//因为这是两个TextView和一个ImageView的自定义适配器,所以适配器不是
//我们将使用第二个参数,所以它可以是任何值。
超级(上下文,0,地震);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
View listItemView=convertView;
如果(listItemView==null){
listItemView=LayoutFlater.from(getContext()).inflate(
R.layout.list_项,父项,false);
返回super.getView(position、convertView、parent);
}
地震电流地震=getItem(位置);
TextView幅值=(TextView)listItemView.findViewById(R.id.magnity);
//从当前AndroidFlavor对象获取版本名,然后
//在名称TextView上设置此文本
震级.setxt(地震.getmMagnitude());
//在list_item.xml布局中查找ID版本号为的TextView
TextView位置=(TextView)listItemView.findViewById(R.id.place);//从当前AndroidFlavor对象获取版本号,然后
//在数字文本视图上设置此文本
place.setText(地震.getMplace());
//在list_item.xml布局中使用ID list_item_图标查找ImageView
TextView日期=(TextView)listItemView.findViewById(R.id.date);
date.setText(地震.getmDate());
//从当前AndroidFlavor对象获取图像资源ID,然后
//将图像设置为iconView
//返回整个列表项布局(包含两个TextView和一个ImageView)
//以便可以在ListView中显示它
返回listItemView;
}
如果需要,我可以添加任何代码或ressource,只要在注释中告诉我即可。您不需要在重写的
getView
函数中调用super.getView(…)
,并且您正在传入无效的资源ID。只需删除该行,因为您没有使用它的返回值:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
...
}
您永远不会真正返回您充气的ViewHolder 当视图为空时,不应调用
super
。应展开视图,获取子视图,填充它们,最后返回展开的根视图
请看一看,了解有关此过程的更多信息
尝试按如下方式编写适配器:
public class EarthQuakeAdapter extends ArrayAdapter<Earthquake> {
// ...
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}
Earthquake currentEarthquake = getItem(position);
TextView magnitude = (TextView) listItemView.findViewById(R.id.magnitude);
// Get the version name from the current AndroidFlavor object and
// set this text on the name TextView
magnitude.setText(Earthquake.getmMagnitude());
// Find the TextView in the list_item.xml layout with the ID version_number
TextView place = (TextView) listItemView.findViewById(R.id.Place); // Get the version number from the current AndroidFlavor object and
// set this text on the number TextView
place.setText(Earthquake.getMplace());
// Find the ImageView in the list_item.xml layout with the ID list_item_icon
TextView date = (TextView) listItemView.findViewById(R.id.Date);
date.setText(Earthquake.getmDate());
// Get the image resource ID from the current AndroidFlavor object and
// set the image to iconView
// Return the whole list item layout (containing 2 TextViews and an ImageView)
// so that it can be shown in the ListView
return listItemView;
}
}
公共类地震适配器扩展ArrayAdapter{
// ...
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
View listItemView=convertView;
如果(listItemView==null){
listItemView=LayoutInflater.from(getContext()).flate(R.layout.list_项,父项,false);
}
地震电流地震=getItem(位置);
TextView幅值=(TextView)listItemView.findViewById(R.id.magnity);
//从当前AndroidFlavor对象获取版本名,然后
//在名称TextView上设置此文本
震级.setxt(地震.getmMagnitude());
//在list_item.xml布局中查找ID版本号为的TextView
TextView位置=(TextView)listItemView.findViewById(R.id.place);//从当前AndroidFlavor对象获取版本号,然后
//在数字文本视图上设置此文本
place.setText(地震.getMplace());
//在list_item.xml布局中使用ID list_item_图标查找ImageView
TextView日期=(TextView)listItemView.findViewById(R.id.date);
date.setText(地震.getmDate());
//从当前AndroidFlavor对象获取图像资源ID,然后
//将图像设置为iconView
//返回整个列表项布局(包含两个TextView和一个ImageView)
//以便可以在ListView中显示它
返回listItemView;
}
}
您可以阅读有关平滑滚动的更多信息。
作为一种选择,您可以切换到RecyclerView。哪一行导致了崩溃?可能重复我不理解您的评论@Bucket@OussamaHassini阅读我在上面评论中提供的链接上的问题和答案。这个问题以前被问过。你的代码非常相似。如果你尝试它,我们肯定会知道k谢谢你的帮助我只是想知道我是应该删除地震适配器方法构造函数还是应该删除Known不,你保留构造函数,我只是为了简单起见删除了它