Android填充listView与自定义ArrayAdapter

时间:2016-04-12 12:13:54

标签: java listview android-arrayadapter

我正在尝试用我创建的自定义数组适配器填充listView。我想要的结果是左侧有一个图像,右侧有两个标签。

当我到达应该显示listView的片段时,应用程序崩溃但我无法找到原因。错误为java.lang.IllegalStateException: System services not available to Activities before onCreate()

这是我的代码:

片段:

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    getActivity().setTitle("Inbox");

    myView = inflater.inflate(R.layout.inbox_layout, container, false);

    ListView list = (ListView) myView.findViewById(R.id.listViewMyAccount);
    inboxCellElement[] elts = new inboxCellElement[] {new inboxCellElement(R.drawable.ic_menu_camera, "Val", "Hi !")};
    list.setAdapter(new inboxCellAdapter(new NavDrawer(), R.layout.inbox_cell_layout, elts, (NavDrawer) getActivity()));

    return myView; 
}

包含要传递给listView的数据的对象:

public class inboxCellElement {
  private String name = "", message = "";
  private int imgId = 0;

  public inboxCellElement(int imgId, String name, String message){
      this.imgId = imgId;
      this.name = name;
      this.message = message;
  }

  public int getImgId() { return imgId; }
  public String getName() { return name; }
  public String getMessage() { return message; }
}

自定义适配器:

public class inboxCellAdapter extends ArrayAdapter {
  inboxCellElement[] elts;
  NavDrawer navDrawer;

  // constructor
  public inboxCellAdapter(Context context, int resource, Object[] objects, NavDrawer navDrawer) {
      super(context, resource, objects);
      elts = (inboxCellElement[]) objects;
      this.navDrawer = navDrawer;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
      MyHolder holder = null;
      LayoutInflater inflater = new NavDrawer().getLayoutInflater();
      if (convertView == null) {
          convertView = inflater.inflate(R.layout.inbox_cell_layout, null, false);
          holder = new MyHolder(convertView);
          convertView.setTag(holder);
      }
      else {
          holder = (MyHolder) convertView.getTag();
      }
      holder.getUpperText().setText(elts[position].getName());
      holder.getLowerText().setText(elts[position].getMessage());
      holder.getImage().setImageResource(elts[position].getImgId());

      return convertView;
  }

  private class MyHolder {
      private View row;
      private TextView upperText = null, lowerText = null;
      private ImageView img = null;

      public MyHolder(View row) {
          this.row = row;
      }

      public TextView getUpperText() {
          if (this.upperText == null) {
              this.upperText = (TextView) navDrawer.findViewById(R.id.inboxCellName);
          }
          return this.upperText;
      }

      public TextView getLowerText() {
          if (this.lowerText == null) {
              this.lowerText = (TextView) navDrawer.findViewById(R.id.inboxCellMessage);
          }
          return this.lowerText;
      }

      public ImageView getImage() {
          if (this.img == null) {
              this.img = (ImageView) navDrawer.findViewById(R.id.inboxCellImg);
          }
          return this.img;
      }
   }
}

似乎错误来自几个navDrawer.findViewById(...)调用,但我不知道如何解决它。任何人都可以确认错误发生在这里,为什么并帮我修复它?感谢

编辑:添加堆栈跟踪

---------崩溃的开始

04-12 14:32:58.816 2128-2128/t2g.com.travel2gather E/AndroidRuntime: FATAL EXCEPTION: main
                                                                     Process: t2g.com.travel2gather, PID: 2128
                                                                     java.lang.IllegalStateException: System services not available to Activities before onCreate()
                                                                         at android.app.Activity.getSystemService(Activity.java:5253)
                                                                         at android.view.LayoutInflater.from(LayoutInflater.java:229)
                                                                         at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:178)
                                                                         at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:137)
                                                                         at t2g.com.travel2gather.adapters.inboxCellAdapter.<init>(inboxCellAdapter.java:0)
                                                                         at t2g.com.travel2gather.inboxFragment.onCreateView(inboxFragment.java:30)
                                                                         at android.app.Fragment.performCreateView(Fragment.java:2220)
                                                                         at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:973)
                                                                         at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
                                                                         at android.app.BackStackRecord.run(BackStackRecord.java:793)
                                                                         at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535)
                                                                         at android.app.FragmentManagerImpl$1.run(FragmentManager.java:482)
                                                                         at android.os.Handler.handleCallback(Handler.java:739)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                         at android.os.Looper.loop(Looper.java:148)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

1 个答案:

答案 0 :(得分:2)

您无法使用Activity的构造函数来提供context(正如您在评论NavDrawer中所说的那样是您的MainActivity)。所以改变这个onCreateView就像这样。

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    getActivity().setTitle("Inbox");

    myView = inflater.inflate(R.layout.inbox_layout, container, false);

    ListView list = (ListView) myView.findViewById(R.id.listViewMyAccount);
    inboxCellElement[] elts = new inboxCellElement[] {new inboxCellElement(R.drawable.ic_menu_camera, "Val", "Hi !")};
    list.setAdapter(new inboxCellAdapter(getActivity(), R.layout.inbox_cell_layout, elts, (NavDrawer) getActivity()));

    return myView; 
}

同样在你的适配器中:

public class inboxCellAdapter extends ArrayAdapter {
  inboxCellElement[] elts;
  NavDrawer navDrawer;
  Activity activity;

  // constructor
  public inboxCellAdapter(Activity activity, int resource, Object[] objects, NavDrawer navDrawer) {
      super(context, resource, objects);
      this.activity = activity;
      elts = (inboxCellElement[]) objects;
      this.navDrawer = navDrawer;
  }

因此getView将会像:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
   MyHolder holder = null;
   LayoutInflater inflater = activity.getLayoutInflater();