Accessing fragments from an external OnCreateView method

Searched around this, but each one is for the onCreateView () method, where you can access the presentation of fragments through an inflatable element.

public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved) { super.onCreateView(inflater, group, saved); View view = inflater.inflate(R.layout.photosfrag, group, false); 

I want to know how to do this dynamically. For example, I use an operation to display a fragment and call a method in a fragment, for example:

 public void setTitleText(String title) { TextView nameView = (TextView)getView().findViewById(R.id.titleTxtView); nameView.setText(title); } 

Just because the fragment has already been created, and this will be a way to dynamically change it.

Any help would be great. I can do it all wrong.

EDIT

Here is the crash log

 06-25 17:31:37.343: D/AndroidRuntime(1009): Shutting down VM 06-25 17:31:37.353: W/dalvikvm(1009): threadid=1: thread exiting with uncaught exception (group=0x409c01f8) 06-25 17:31:37.383: E/AndroidRuntime(1009): FATAL EXCEPTION: main 06-25 17:31:37.383: E/AndroidRuntime(1009): java.lang.NullPointerException 06-25 17:31:37.383: E/AndroidRuntime(1009): at com.corecoders.stuart.MainActivity.onTrackSelected(MainActivity.java:81) 06-25 17:31:37.383: E/AndroidRuntime(1009): at com.corecoders.stuart.HistoryFragment.onListItemClick(HistoryFragment.java:51) 06-25 17:31:37.383: E/AndroidRuntime(1009): at android.app.ListFragment$2.onItemClick(ListFragment.java:160) 06-25 17:31:37.383: E/AndroidRuntime(1009): at android.widget.AdapterView.performItemClick(AdapterView.java:292) 06-25 17:31:37.383: E/AndroidRuntime(1009): at android.widget.AbsListView.performItemClick(AbsListView.java:1058) 06-25 17:31:37.383: E/AndroidRuntime(1009): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514) 06-25 17:31:37.383: E/AndroidRuntime(1009): at android.widget.AbsListView$1.run(AbsListView.java:3168) 06-25 17:31:37.383: E/AndroidRuntime(1009): at android.os.Handler.handleCallback(Handler.java:605) 06-25 17:31:37.383: E/AndroidRuntime(1009): at android.os.Handler.dispatchMessage(Handler.java:92) 06-25 17:31:37.383: E/AndroidRuntime(1009): at android.os.Looper.loop(Looper.java:137) 06-25 17:31:37.383: E/AndroidRuntime(1009): at android.app.ActivityThread.main(ActivityThread.java:4424) 06-25 17:31:37.383: E/AndroidRuntime(1009): at java.lang.reflect.Method.invokeNative(Native Method) 06-25 17:31:37.383: E/AndroidRuntime(1009): at java.lang.reflect.Method.invoke(Method.java:511) 06-25 17:31:37.383: E/AndroidRuntime(1009): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 06-25 17:31:37.383: E/AndroidRuntime(1009): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 06-25 17:31:37.383: E/AndroidRuntime(1009): at dalvik.system.NativeStart.main(Native Method) 
+4
source share
5 answers

The way you do this should just work. The crash log shows us that the NPE is not related to how the fragment updates its widgets.

+3
source

Do this instead:

 //Define reference globally TextView nameView; public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved){ super.onCreateView(inflater, group, saved); View view = inflater.inflate(R.layout.photosfrag, group, false); //do this here! nameView = view.findViewByID(R.id.titleTxtView); ... } public void setTitle(String t){ nameView.setText(t); } 

The difference is that this approach does not lazily load the TextView link - it loads it when the view is inflated, and not when it is needed.

+1
source

I had a similar problem, it turned out that I redefined the getItem(int) method for Fragment and returned a new Fragment instance each time, in your case I don't know if you redefined it, but you should check HistoryFragment.onListItemClick to find out if you call the getItem method and return a new instance of the fragment that does not match the one you are showing on the screen.

+1
source

If someone is trying to access any view from the fragment layout in the Fragment class outside of onCreateView, you can make the inflation declaration declared at the class level and link it after the inflation statement and use this link to search anywhere in the Fragment class: for example ... ........

 public class FragmentA extends Fragment { //This will hold the inflated fragment layout private View view; public FragmentA() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragmenta, container, false); this.view= view;// link this 'view' to our 'view' which we declared in class setTextViewNow(); return view; } private void setTextViewNow() { //use 'view' to find the id of textview using findviewById(); TextView myView = (TextView)view.findViewById(R.id.textViewMyView); myView.setText("THis is set from outside oncreateView"); } 

}

0
source

Or use the getActivity method. Example from the document: View listView = getActivity().findViewById(R.id.list);

-1
source

All Articles