初始化变量的NullPointerException?

时间:2015-03-13 21:06:50

标签: java android nullpointerexception

我在This question中看到了问题,但我相信我正确调用了我的布局,然后与Activity / Fragment的观点进行了互动。

我的问题是当我在构造函数中初始化变量时,我测试了它的null状态,结果很好......直到app继续进入另一个方法,然后它再次神奇地变为null ... 也许还有一些其他的Android技术性问题让我的程序崩溃了,我不知道呢?

ServiceStatusFragment (是程序的入口点,从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显示我在构造函数中初始化的serviceStatus变量在调用适配器的getCount()方法时为null,为什么会这样?

logcat的 (在这里没有正确显示)GIST HERE

0 个答案:

没有答案