将对象加载到ListView时出现NullPointerException

时间:2014-03-18 03:00:57

标签: java android android-fragments android-listview

我试图对我正在构建的应用程序的UI进行一些模拟。我使用导航抽屉作为根布局,并且我使用嵌套片段作为我的一个菜单项,以便使用带有标签指示符的ViewPager。当我只使用一些基本文本填充每个片段时,该部分工作正常,但当我用一个ListView替换其中一个子片段(其中一个选项卡视图)中的基本文本时;我在我的片段类中得到了一个java.lang.NullPointerException。

public class FragmentVenues extends ListFragment {

private static final String ARGUMENT_KEY_PROPERTIES = "venue";


public static FragmentVenues newInstance(ArrayList<Venue> venues) {
    final Bundle args = new Bundle();
    args.putParcelableArrayList(ARGUMENT_KEY_PROPERTIES, venues);

    final FragmentVenues f = new FragmentVenues();
    f.setArguments(args);
    return f;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    final List<Venue> venues =      getArguments().getParcelableArrayList(ARGUMENT_KEY_PROPERTIES);//error here
    setListAdapter(new VenueListAdapter(getActivity(), venues));
}

这是访问填充类的ListAdapter创建要加载到ListView中的对象。

public class VenueListAdapter extends ArrayAdapter<Venue>{
private final LayoutInflater mInflater;

public VenueListAdapter(Context context, List<Venue> objects) {
    super(context, -1, objects);
    mInflater = LayoutInflater.from(context);

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final Venue venue = getItem(position);

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.fragmentvenues, parent, false);
    }

    // This will be made more efficient later
    TextView tv = (TextView) convertView.findViewById(R.id.city);
    tv.setText(venue.getCity() + ", " + venue.getState());
    tv = (TextView) convertView.findViewById(R.id.venuename);
    tv.setText(venue.getVenueName());
    tv = (TextView) convertView.findViewById(R.id.street);
    tv.setText(venue.getStreetAddress());
    tv = (TextView) convertView.findViewById(R.id.number);
    tv.setText(venue.getPhoneNumber());

    return convertView;

}
}

我想弄清楚我做错了什么和/或是否有我失踪的东西?当我使用Action Bar Tabs时,同样的代码工作,但当我意识到他们并不打算使用导航抽屉时,我放弃了它们。下面是堆栈跟踪:

 03-17 23:45:39.345: E/AndroidRuntime(2184): FATAL EXCEPTION: main
03-17 23:45:39.345: E/AndroidRuntime(2184): java.lang.NullPointerException
03-17 23:45:39.345: E/AndroidRuntime(2184):     at com.testapp.slidingmenu.FragmentVenues.onActivityCreated(FragmentVenues.java:47)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1508)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.view.View.measure(View.java:15848)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.view.View.measure(View.java:15848)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.view.View.measure(View.java:15848)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.view.View.measure(View.java:15848)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:651)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.view.View.measure(View.java:15848)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.view.View.measure(View.java:15848)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.view.View.measure(View.java:15848)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.view.View.measure(View.java:15848)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.view.Choreographer.doFrame(Choreographer.java:532)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.os.Handler.handleCallback(Handler.java:730)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.os.Looper.loop(Looper.java:137)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at android.app.ActivityThread.main(ActivityThread.java:5103)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at java.lang.reflect.Method.invokeNative(Native Method)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at java.lang.reflect.Method.invoke(Method.java:525)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-17 23:45:39.345: E/AndroidRuntime(2184):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

试试这个,用这个替换你的课程

public class VenueListAdapter extends ArrayAdapter<Venue>{
private final LayoutInflater mInflater;

private List<Venue> venue;

public VenueListAdapter(Context context, List<Venue> objects) {

super(context, -1, objects);

 mInflater = LayoutInflater.from(context);

venue=objects;

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

final Venue venue = getItem(position);

if (convertView == null) {
      convertView = mInflater.inflate(R.layout.fragmentvenues, parent, false);
    }

// This will be made more efficient later
TextView tv = (TextView) convertView.findViewById(R.id.city);
tv.setText(venue.getCity() + ", " + venue.getState());
tv = (TextView) convertView.findViewById(R.id.venuename);
tv.setText(venue.getVenueName());
tv = (TextView) convertView.findViewById(R.id.street);
tv.setText(venue.getStreetAddress());
tv = (TextView) convertView.findViewById(R.id.number);
tv.setText(venue.getPhoneNumber());

return convertView;

}
}

答案 1 :(得分:0)

在Venue Class和Fragment上实现Parcelable。

public class Venue implements Parcelable{

@Override
public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    // TODO Auto-generated method stub

}

}