简体   繁体   中英

How to inflate a layout in method in fragment

actually I have a fragment.`

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View promptView = inflater.inflate(R.layout.fragment_messages, container, false);

    sendRestoreRequest2();

    return promptView;
}


public void sendRestoreRequest2() {

     final TextView df=(TextView)promptView.findViewById(R.id.df);//this will give an error. I dont know how to inflate this one because this is a fragment.
    }

How to fix this? Because in activity we can just final TextView df=(TextView)findViewById(R.id.df) in method and it will work.

You have to create a global View promptView variable in your fragment.

Your code should look something like this:

private View promptView = null;

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
Bundle savedInstanceState) {
    if(promptView == null){
       promptView = inflater.inflate(R.layout.fragment_messages, container, false);

       sendRestoreRequest2();
    }
    return promptView;
}


public void sendRestoreRequest2() {
    final TextView df=(TextView)promptView.findViewById(R.id.df);
}

Try this:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
   View promptView = inflater.inflate(R.layout.fragment_messages, container, false);
   sendRestoreRequest2(promptView);
   return promptView;
}

public void sendRestoreRequest2(View promptView) {
   final TextView df = (TextView) promptView.findViewById(R.id.df);
}

YOU are not having the scope of the inflated PromptView in the method which is out side of the ONCREATEVIEW's scope

one thing that you can do is like this

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View promptView = inflater.inflate(R.layout.fragment_messages, container, false);

    sendRestoreRequest2(promptView);

    return promptView;
}


public void sendRestoreRequest2(View incomingPromptView) {
     // here now you have that promptView in your other method's scope and this should register the TextView in your Fragment
     final TextView df=(TextView)incomingPromptView.findViewById(R.id.df);
    }

Solution number Two is like this

You can do findViewbyid in a fragment class by the useage of simple getActivty().findViewbyId by overRiding the method of OnActivityCreated in your Fragment class (but it is a passive approach though)

Sorry everyone I am a newbie in fragment.I just discovered the solution

Put this inoncreateview. Change from final TextView df=(TextView)promptView.findViewById(R.id.df); to df = view.findViewById(R.id.df);

And it works.

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