1

I'm trying to fill my listView with a custom array adapter I created. The result I want is to have an image on the left hand side, and two labels on the right.

When I reach the fragment that should display the listView, the app crashes but I can't really find out why. The error being java.lang.IllegalStateException: System services not available to Activities before onCreate().

Here is my code:

The fragment:

@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; 
}

The Object containing the data to pass to the 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; }
}

The custom Adapter:

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;
      }
   }
}

It seems that the error comes from the several navDrawer.findViewById(...) calls, but I have no idea how to fix it. Can anyone confirm the error happens here, why and help me fixing it ? Thanks

EDIT: Adding the stacktrace

--------- beginning of crash

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)
10
  • can you post full stack trace? Commented Apr 12, 2016 at 12:16
  • I added it at the end of the post Commented Apr 12, 2016 at 12:35
  • Are you anywhere calling getSystemService(Context.CONNECTIVITY_SERVICE);? Commented Apr 12, 2016 at 12:43
  • What is this NavDrawer? Commented Apr 12, 2016 at 12:46
  • No, Could this help getting the current context ? And the navDrawer is my main activity that contains a NavigationDrawer, hence the use of fragments. Commented Apr 12, 2016 at 12:46

1 Answer 1

2

You can't use Activity's constructor to provide context (as you said in comments NavDrawer is your MainActivity). So change this onCreateView like this.

@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; 
}

Similarly in you adapter:

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;
  }

Hence getView will be like:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
   MyHolder holder = null;
   LayoutInflater inflater = activity.getLayoutInflater();
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your help. I think I understand why this happens. I think you made a mistake by calling super(context, resource, objects); since we removed the variable context. However, I replaced it by activity, but I still have a crash: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at t2g.com.travel2gather.adapters.inboxCellAdapter.getView(inboxCellAdapter.java:42). Why is holder.getUpperText() null?
EDIT: I found the error: In the holder i was using activity.findViewById() instead of row.findViewById() Thanks for the help !
Great! Happy coding. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.