本文介绍了ConvertView是ArrayAdapter空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
公共类DSongListAdapter扩展ArrayAdapter<宋> { 上下文mContext;
清单<宋> mObjects; 公共DSongListAdapter(上下文的背景下,列表与LT;宋>的SongList){
超(背景下,R.layout.list_item,的SongList);
this.mContext =背景;
this.mObjects =的SongList;
} @覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){ 查看ItemView控件= convertView;
如果(ItemView控件!= NULL){
ItemView控件=((活动)mContext).getLayoutInflater()膨胀(R.layout.list_item,父母,假的)。 TextView的titleLabel =(TextView中)itemView.findViewById(R.id.titleLabel);
}
返回convertView;
} }公共类SongFragment扩展片段{
私人DSongListAdapter适配器;
私人的ArrayList<宋>的SongList;
私人查看rootView;
私人的ListView ListView的; @覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
rootView = inflater.inflate(R.layout.fragment_song,集装箱,FALSE);
ListView控件=(ListView控件)rootView.findViewById(R.id.song_listview); 如果(ListView的== NULL)
返回rootView; 的SongList =新的ArrayList<宋>();
songList.add(新曲());
适配器=新DSongListAdapter(getActivity()的SongList);
listView.setAdapter(适配器); //现在崩溃! 返回rootView;
}
我的应用总是崩溃,因为 convertView
总是空!我能做些什么,以显示我的项目?
LogCat中
> 06-19 13:33:17.314:E / AndroidRuntime(26651):致命异常:主要
> 06-19 13:33:17.314:E / AndroidRuntime(26651):
>显示java.lang.NullPointerException 06-19 13:33:17.314:
> E / AndroidRuntime(26651):在
> android.widget.AbsListView.obtainView(AbsListView.java:2621)06-19
> 13:33:17.314:E / AndroidRuntime(26651):在
> android.widget.ListView.measureHeightOfChildren(ListView.java:1253)
> 06-19 13:33:17.314:E / AndroidRuntime(26651):在
> android.widget.ListView.onMeasure(ListView.java:1165)06-19
> 13:33:17.314:E / AndroidRuntime(26651):在
> android.view.View.measure(View.java:16142)06-19 13:33:17.314:
> E / AndroidRuntime(26651):在
> android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4927)
> 06-19 13:33:17.314:E / AndroidRuntime(26651):在
> android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
> 06-19 13:33:17.314:E / AndroidRuntime(26651):在
> android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
> 06-19 13:33:17.314:E / AndroidRuntime(26651):在
> android.widget.LinearLayout.onMeasure(LinearLayout.java:588)06-19
> 13:33:17.314:E / AndroidRuntime(26651):在
> android.view.View.measure(View.java:16142)06-19 13:33:17.314:
> E / AndroidRuntime(26651):在
> android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4927)
> 06-19 13:33:17.314:E / AndroidRuntime(26651):在
> android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
> 06-19 13:33:17.314:E / AndroidRuntime(26651):在
> android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
> 06-19 13:33:17.314:E / AndroidRuntime(26651):在
> android.widget.LinearLayout.onMeasure(LinearLayout.java:588)06-19
> 13:33:17.314:E / AndroidRuntime(26651):在
> android.view.View.measure(View.java:16142)06-19 13:33:17.314:
> E / AndroidRuntime(26651):在
> android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4927)
> 06-19 13:33:17.314:E / AndroidRuntime(26651):在
> android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
> 06-19 13:33:17.314:E / AndroidRuntime(26651):在
> android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
> 06-19 13:33:17.314:E / AndroidRuntime(26651):在
> android.widget.LinearLayout.onMeasure(LinearLayout.java:588)06-19
> 13:33:17.314:E / AndroidRuntime(26651):在
> android.view.View.measure(View.java:16142)06-19 13:33:17.314:
> E / AndroidRuntime(26651):在
> android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4927)
> 06-19 13:33:17.314:E / AndroidRuntime(26651):在
> android.widget.FrameLayout.onMeasure(FrameLayout.java:310)06-19
> 13:33:17.314:E / AndroidRuntime(26651):在
> android.view.View.measure(View.java:16142)06-19 13:33:17.314:
> E / AndroidRuntime(26651):在
> android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
> 06-19 13:33:17.314:E / AndroidRuntime(26651):在
> android.widget.LinearLayout.onMeasure(LinearLayout.java:588)06-19
> 13:33:17.314:E / AndroidRuntime(26651):在
> android.view.View.measure(View.java:16142)06-19 13:33:17.314:
> E / AndroidRuntime(26651):在
> android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4927)
> 06-19 13:33:17.314:E / AndroidRuntime(26651):在
> android.widget.FrameLayout.onMeasure(FrameLayout.java:310)06-19
> 13:33:17.314:E / AndroidRuntime(26651):在
> com.android.internal.policy.impl.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2471)
> 06-19 13:33:17.314:E / AndroidRuntime(26651):在
> android.view.View.measure(View.java:16142)06-19 13:33:17.314:
> E / AndroidRuntime(26651):在
> android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2167)06-19
> 13:33:17.314:E / AndroidRuntime(26651):在
> android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1315)
> 06-19 13:33:17.314:E / AndroidRuntime(26651):在
> android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1526)
> 06-19 13:33:17.314:E / AndroidRuntime(26651):在
> android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1212)06-19
> 13:33:17.314:E / AndroidRuntime(26651):在
> android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:4981)
> 06-19 13:33:17.314:E / AndroidRuntime(26651):在
> android.view.Choreographer $ CallbackRecord.run(Choreographer.java:791)
> 06-19 13:33:17.314:E / AndroidRuntime(26651):在
> android.view.Choreographer.doCallbacks(Choreographer.java:591)06-19
> 13:33:17.314:E / AndroidRuntime(26651):在
> android.view.Choreographer.doFrame(Choreographer.java:561)06-19
> 13:33:17.314:E / AndroidRuntime(26651):在
> android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:777)
> 06-19 13:33:17.314:E / AndroidRuntime(26651):在
> android.os.Handler.handleCallback(Handler.java:725)06-19
> 13:33:17.314:E / AndroidRuntime(26651):在
> android.os.Handler.dispatchMessage(Handler.java:92)06-19
> 13:33:17.314:E / AndroidRuntime(26651):在
> android.os.Looper.loop(Looper.java:137)06-19 13:33:17.314:
> E / AndroidRuntime(26651):在
> android.app.ActivityThread.main(ActivityThread.java:5306)06-19
> 13:33:17.314:E / AndroidRuntime(26651):在
> java.lang.reflect.Method.invokeNative(本机方法)06-19
> 13:33:17.314:E / AndroidRuntime(26651):在
> java.lang.reflect.Method.invoke(Method.java:511)06-19 13:33:17.314:
> E / AndroidRuntime(26651):在
> com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1102)
> 06-19 13:33:17.314:E / AndroidRuntime(26651):在
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)06-19
> 13:33:17.314:E / AndroidRuntime(26651):在
> dalvik.system.NativeStart.main(本机方法)
解决方案
尝试用它来取代您的适配器code。公共类DSongListAdapter扩展ArrayAdapter<宋> { 上下文mContext;
清单<宋> mObjects; 公共DSongListAdapter(上下文的背景下,列表与LT;宋>的SongList){
超(背景下,R.layout.list_item,的SongList);
this.mContext =背景;
this.mObjects =的SongList;
}
@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){ ViewHolder持有人;
如果(convertView == NULL){
持有人=新ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item,父母,假);
holder.titleLabel =(TextView中)convertView.findViewById(R.id.titleLabel);
convertView.setTag(保持器);
}其他{
支架=(ViewHolder)convertView.getTag();
}
holder.titleLabel.setText(mObjects.get(位置).getTitle());
返回convertView;
} 类ViewHolder {
TextView的titleLabel;
} }
public class DSongListAdapter extends ArrayAdapter<Song> {
Context mContext;
List<Song> mObjects;
public DSongListAdapter(Context context, List<Song> songList ) {
super(context, R.layout.list_item, songList );
this.mContext = context;
this.mObjects = songList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if( itemView != null ){
itemView = ((Activity) mContext).getLayoutInflater().inflate(R.layout.list_item, parent, false);
TextView titleLabel = (TextView) itemView.findViewById(R.id.titleLabel);
}
return convertView;
}
}
public class SongFragment extends Fragment {
private DSongListAdapter adapter;
private ArrayList<Song> songList;
private View rootView;
private ListView listView;
@Override
public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ){
rootView = inflater.inflate(R.layout.fragment_song, container, false);
listView = (ListView) rootView.findViewById( R.id.song_listview );
if( listView == null )
return rootView;
songList = new ArrayList<Song>();
songList.add( new Song() );
adapter = new DSongListAdapter( getActivity(), songList);
listView.setAdapter(adapter); //Crashes now!
return rootView;
}
My App always crashes because the convertView
is always null! What can I do to display my items?
LogCat
> 06-19 13:33:17.314: E/AndroidRuntime(26651): FATAL EXCEPTION: main
> 06-19 13:33:17.314: E/AndroidRuntime(26651):
> java.lang.NullPointerException 06-19 13:33:17.314:
> E/AndroidRuntime(26651): at
> android.widget.AbsListView.obtainView(AbsListView.java:2621) 06-19
> 13:33:17.314: E/AndroidRuntime(26651): at
> android.widget.ListView.measureHeightOfChildren(ListView.java:1253)
> 06-19 13:33:17.314: E/AndroidRuntime(26651): at
> android.widget.ListView.onMeasure(ListView.java:1165) 06-19
> 13:33:17.314: E/AndroidRuntime(26651): at
> android.view.View.measure(View.java:16142) 06-19 13:33:17.314:
> E/AndroidRuntime(26651): at
> android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4927)
> 06-19 13:33:17.314: E/AndroidRuntime(26651): at
> android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
> 06-19 13:33:17.314: E/AndroidRuntime(26651): at
> android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
> 06-19 13:33:17.314: E/AndroidRuntime(26651): at
> android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 06-19
> 13:33:17.314: E/AndroidRuntime(26651): at
> android.view.View.measure(View.java:16142) 06-19 13:33:17.314:
> E/AndroidRuntime(26651): at
> android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4927)
> 06-19 13:33:17.314: E/AndroidRuntime(26651): at
> android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
> 06-19 13:33:17.314: E/AndroidRuntime(26651): at
> android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
> 06-19 13:33:17.314: E/AndroidRuntime(26651): at
> android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 06-19
> 13:33:17.314: E/AndroidRuntime(26651): at
> android.view.View.measure(View.java:16142) 06-19 13:33:17.314:
> E/AndroidRuntime(26651): at
> android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4927)
> 06-19 13:33:17.314: E/AndroidRuntime(26651): at
> android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
> 06-19 13:33:17.314: E/AndroidRuntime(26651): at
> android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
> 06-19 13:33:17.314: E/AndroidRuntime(26651): at
> android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 06-19
> 13:33:17.314: E/AndroidRuntime(26651): at
> android.view.View.measure(View.java:16142) 06-19 13:33:17.314:
> E/AndroidRuntime(26651): at
> android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4927)
> 06-19 13:33:17.314: E/AndroidRuntime(26651): at
> android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 06-19
> 13:33:17.314: E/AndroidRuntime(26651): at
> android.view.View.measure(View.java:16142) 06-19 13:33:17.314:
> E/AndroidRuntime(26651): at
> android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
> 06-19 13:33:17.314: E/AndroidRuntime(26651): at
> android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 06-19
> 13:33:17.314: E/AndroidRuntime(26651): at
> android.view.View.measure(View.java:16142) 06-19 13:33:17.314:
> E/AndroidRuntime(26651): at
> android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4927)
> 06-19 13:33:17.314: E/AndroidRuntime(26651): at
> android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 06-19
> 13:33:17.314: E/AndroidRuntime(26651): at
> com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2471)
> 06-19 13:33:17.314: E/AndroidRuntime(26651): at
> android.view.View.measure(View.java:16142) 06-19 13:33:17.314:
> E/AndroidRuntime(26651): at
> android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2167) 06-19
> 13:33:17.314: E/AndroidRuntime(26651): at
> android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1315)
> 06-19 13:33:17.314: E/AndroidRuntime(26651): at
> android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1526)
> 06-19 13:33:17.314: E/AndroidRuntime(26651): at
> android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1212) 06-19
> 13:33:17.314: E/AndroidRuntime(26651): at
> android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4981)
> 06-19 13:33:17.314: E/AndroidRuntime(26651): at
> android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
> 06-19 13:33:17.314: E/AndroidRuntime(26651): at
> android.view.Choreographer.doCallbacks(Choreographer.java:591) 06-19
> 13:33:17.314: E/AndroidRuntime(26651): at
> android.view.Choreographer.doFrame(Choreographer.java:561) 06-19
> 13:33:17.314: E/AndroidRuntime(26651): at
> android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
> 06-19 13:33:17.314: E/AndroidRuntime(26651): at
> android.os.Handler.handleCallback(Handler.java:725) 06-19
> 13:33:17.314: E/AndroidRuntime(26651): at
> android.os.Handler.dispatchMessage(Handler.java:92) 06-19
> 13:33:17.314: E/AndroidRuntime(26651): at
> android.os.Looper.loop(Looper.java:137) 06-19 13:33:17.314:
> E/AndroidRuntime(26651): at
> android.app.ActivityThread.main(ActivityThread.java:5306) 06-19
> 13:33:17.314: E/AndroidRuntime(26651): at
> java.lang.reflect.Method.invokeNative(Native Method) 06-19
> 13:33:17.314: E/AndroidRuntime(26651): at
> java.lang.reflect.Method.invoke(Method.java:511) 06-19 13:33:17.314:
> E/AndroidRuntime(26651): at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
> 06-19 13:33:17.314: E/AndroidRuntime(26651): at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 06-19
> 13:33:17.314: E/AndroidRuntime(26651): at
> dalvik.system.NativeStart.main(Native Method)
解决方案
Try to replace your adapter code with this.
public class DSongListAdapter extends ArrayAdapter<Song> {
Context mContext;
List<Song> mObjects;
public DSongListAdapter(Context context, List<Song> songList ) {
super(context, R.layout.list_item, songList );
this.mContext = context;
this.mObjects = songList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if( convertView == null ){
holder = new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);
holder.titleLabel = (TextView) convertView.findViewById(R.id.titleLabel);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.titleLabel.setText(mObjects.get(position).getTitle());
return convertView;
}
class ViewHolder {
TextView titleLabel;
}
}
这篇关于ConvertView是ArrayAdapter空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!