问题
Automatically, in OnCreateView, there's:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
I want to initialize my variable as in the same way like OnCreateView in AsyncTask. I was able to initialize these variable :
LayoutInflater inflater;
ViewGroup container;
inflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rootView = inflater.inflate(R.layout.fragment_pager_list,
container, false);
However, i get the error that container isn't initialized! so how can i initialize it? Thanks
回答1:
It's complaining because you have not initialized the value of the variable container
- you have only declared it. Initialization means you assign it a value, such as ViewGroup container = null;
.
For your purposes, you can omit that entirely and simply pass null to the call to inflate
.
LayoutInflater inflater = (LayoutInflater) ctx.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rootView = inflater.inflate(R.layout.fragment_pager_list, null);
来源:https://stackoverflow.com/questions/18038163/how-to-initialize-containerviewgroup