简体   繁体   中英

During what part of activity life cycle various android annotations are called?

Currently I am moving my android project to android-annotations. I am having difficulty in understanding the sequence in which annotations are called. Due to wrong sequence I am getting nullPointerExceptions.

As an example usecase, I am trying to use @ViewById in Fragment. However I am getting nullPointerException in onCreateView() and onResume().

Documentation explaining about at what time in activity life cycle, which annotation will be triggered will be very helpful.

Following is small section of my code :-

@EFragment
public class DiscoverFragment extends Fragment {
    @ViewById(R.id.dishList)
    PullToRefreshListView mPullToRefreshView;

    @ViewById
    LinearLayout noDataAvailable;

    @ViewById
    LinearLayout dataAvailable;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.d(TAG, "onCreateView");
        View v = inflater.inflate(R.layout.fragment_discover, container, false);

        if (cursorStringDistance == null) {
            mPullToRefreshView.setMode(Mode.PULL_FROM_START);
            mPullToRefreshView.setOnRefreshListener(oneRefresher);
        } else {
            mPullToRefreshView.setMode(Mode.BOTH);
            mPullToRefreshView.setOnRefreshListener(bothRefresher);
        }

        ListView actualListView = mPullToRefreshView.getRefreshableView();
        actualListView.setAdapter(mAdapter);
        return v;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (dishListDistance.size() == 0) {
            noDataAvailable.setVisibility(View.VISIBLE);
            dataAvailable.setVisibility(View.GONE);
        } else {
            noDataAvailable.setVisibility(View.GONE);
            dataAvailable.setVisibility(View.VISIBLE);
        }
        startTime = System.currentTimeMillis();
    }
}

I am getting nullPointerException for line mPullToRefreshView.setMode(Mode.PULL_FROM_START); and noDataAvailable.setVisibility(View.VISIBLE); .

You have to add an @AfterViews annotated method, the injected View fields will be available there. In case of Fragment s AA injects the View fields in the onViewCreated() method. If you want to understand the lifecycle, inspect the generated class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM