0

I saw the question asked in This question but I believe I correctly called my layouts in order then interacted with the Activity/Fragment's views.

My issue is when I initialized a variable inside a constructor, I tested it for it's null status and it turns out fine... until the app continues into another method, then it magically becomes null again... Maybe there's some other Android technicality that is crashing my program that I don't know about?

ServiceStatusFragment (is the entry point of the program, gets loaded from MainActivity)

public class ServiceStatusFragment extends Fragment {
private final String LOG_TAG = getTag();
private static final String ARG_SECTION_NUMBER = "section_number";

private View rootView;
private ListView listView;

private ServiceStatus serviceStatus;

private PopulateList populateList;

public static ServiceStatusFragment newInstance(int sectionNumber) { //gets called when app starts
    ServiceStatusFragment fragment = new ServiceStatusFragment();
    Bundle args = new Bundle();
    args.putInt(ARG_SECTION_NUMBER, sectionNumber);
    fragment.setArguments(args);
    return fragment;
}

public ServiceStatusFragment() { }

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.fragment_main, container, false);

    listView = (ListView)rootView.findViewById(R.id.status_list);

    listView.setAdapter(new StatusListAdapter(rootView.getContext())); //sets up adapter, pass in context
    return rootView;
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    ((MainActivity) activity).onSectionAttached(
            getArguments().getInt(ARG_SECTION_NUMBER));
}

StatusListAdapter

public class StatusListAdapter extends BaseAdapter
{
private final String LOG_TAG = StatusListAdapter.class.getSimpleName();

private Context c;
private ServiceStatus serviceStatus = new ServiceStatus();

public StatusListAdapter(Context c){
    this.c = c;
    Serializer serializer = new Persister();
    try {
        //deserialize XML as per Simple XML java library
        serviceStatus = serializer.read(ServiceStatus.class, c.getResources().openRawResource(R.raw.servicestatus));
    } catch (Exception e) {
        e.printStackTrace();
    }

    //DEBUG STUFF, (none of this gets displayed to logcat)
    if(serviceStatus == null)
        System.out.println("NULL BITCH!!");
    if(c.getResources().openRawResource(R.raw.servicestatus) == null)
        System.out.println("FILE  NULL HOE!!");
}

@Override
public int getCount()
{
    return serviceStatus.getSubways().size(); //Logcat shows NullPointerException!?!??
}

@Override
public Object getItem(int position)
{
    return serviceStatus.getSubways().get(position);
}

@Override
public long getItemId(int position)
{
    return 0;
}

static class StatusRowItemHolder {
    TextView lineText;
    TextView statusText;
    TextView dateTimeText;
}

//TODO: Check efficiency, doesn't seem right in teh logcatz. Getting Tags, then inflating views?
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    StatusRowItemHolder srih;

    //some view initialization stuff...

    return convertView;
}

LogCat shows the serviceStatus variable I initialized in the constructor is null when the adapter's getCount() method is called, why is that?

Logcat (not showing correctly on here) GIST HERE

Community
  • 1
  • 1
mastrgamr
  • 631
  • 1
  • 11
  • 21
  • 6
    getSubways() is returning null – Blackbelt Mar 13 '15 at 21:09
  • Thanks @Blackbelt I found the issue. Apparently the library I was using (which ServiceStatus was utilizing) was returning errors, but app was not breaking when that error occurred it just threw a warning. The LogCat showed the warning but I didn't inherently see it, I just went straight to the red error crashing my app... I guess moral of the story is read your warnings? – mastrgamr Mar 14 '15 at 01:07

0 Answers0