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;
}
我的应用程序总是崩溃,因为convertView
始终为空!我该怎么做才能展示我的物品?
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)
答案 0 :(得分:3)
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;
}
}
答案 1 :(得分:1)
你必须从getView()回调中返回itemView。