我的列表视图有一个自定义适配器(扩展了 ArrayAdapter)。该列表加载正常。但是,当我点击我的列表中的一个项目,即列表中的第5个项目时,getView中的位置总是0而不是5。它崩溃了,因为convertView
不再null
是0,如果它是0,则header
应该将变量初始化为R.id.listHeaderTv
TextView ,但它找不到它并最终为空。
我如何解决position
问题并且convertView
不再为空?
编辑:检查 onItemClick 方法后,位置正确。唯一的问题是,在 onItemClick 方法之后,getView 被调用并在那里崩溃,因为视图没有被膨胀。
日志猫:
02-27 16:35:25.280: E/AndroidRuntime(6330): FATAL EXCEPTION: main
02-27 16:35:25.280: E/AndroidRuntime(6330): java.lang.NullPointerException
02-27 16:35:25.280: E/AndroidRuntime(6330): at moflow.adapters.CatalogListAdapter.getView(CatalogListAdapter.java:66)
02-27 16:35:25.280: E/AndroidRuntime(6330): at android.widget.AbsListView.obtainView(AbsListView.java:1294)
02-27 16:35:25.280: E/AndroidRuntime(6330): at android.widget.ListView.makeAndAddView(ListView.java:1727)
02-27 16:35:25.280: E/AndroidRuntime(6330): at android.widget.ListView.fillSpecific(ListView.java:1272)
02-27 16:35:25.280: E/AndroidRuntime(6330): at android.widget.ListView.layoutChildren(ListView.java:1558)
02-27 16:35:25.280: E/AndroidRuntime(6330): at android.widget.AbsListView.onLayout(AbsListView.java:1147)
02-27 16:35:25.280: E/AndroidRuntime(6330): at android.view.View.layout(View.java:7035)
02-27 16:35:25.280: E/AndroidRuntime(6330): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:909)
02-27 16:35:25.280: E/AndroidRuntime(6330): at android.view.View.layout(View.java:7035)
02-27 16:35:25.280: E/AndroidRuntime(6330): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
02-27 16:35:25.280: E/AndroidRuntime(6330): at android.view.View.layout(View.java:7035)
02-27 16:35:25.280: E/AndroidRuntime(6330): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
02-27 16:35:25.280: E/AndroidRuntime(6330): at android.view.View.layout(View.java:7035)
02-27 16:35:25.280: E/AndroidRuntime(6330): at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
02-27 16:35:25.280: E/AndroidRuntime(6330): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
02-27 16:35:25.280: E/AndroidRuntime(6330): at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 16:35:25.280: E/AndroidRuntime(6330): at android.os.Looper.loop(Looper.java:123)
02-27 16:35:25.280: E/AndroidRuntime(6330): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-27 16:35:25.280: E/AndroidRuntime(6330): at java.lang.reflect.Method.invokeNative(Native Method)
02-27 16:35:25.280: E/AndroidRuntime(6330): at java.lang.reflect.Method.invoke(Method.java:521)
02-27 16:35:25.280: E/AndroidRuntime(6330): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-27 16:35:25.280: E/AndroidRuntime(6330): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-27 16:35:25.280: E/AndroidRuntime(6330): at dalvik.system.NativeStart.main(Native Method)
适配器:
@Override
public int getCount() {
return catalog.size();
}
@Override
public View getView( int position, View convertView, ViewGroup parent ) {
View v = convertView;
CatalogItem item = getItem( position );
if ( v == null ) {
LayoutInflater inflater = ( LayoutInflater ) mContext.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
if ( item.header )
v = inflater.inflate( R.layout.sec_header, null );
else
v = inflater.inflate( R.layout.sec_item, null );
}
if ( item.header ) {
TextView header = ( TextView ) v.findViewById( R.id.listHeaderTV );
header.setText( item.name );
header.setEnabled( false );
} else {
TextView sectionItem = ( TextView ) v.findViewById( R.id.listCreatureNameTV );
sectionItem.setText( item.name );
}
return v;
}