Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Fragments View from Outside OnCreateView Method

Have searched around for this but everyone of them is for the onCreateView() method where you can access the Fragments view via the inflater.

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

What I want to know is how to do this dynamically. For example say I am using an activity to show a fragment and call a method in the fragment such as:

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

Just because the fragment is already created and it would be a way of dynamically changing it.

Any help would be grand. I may doing 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)
like image 570
StuStirling Avatar asked Jun 25 '12 16:06

StuStirling


People also ask

Which method is used to access the fragmented view?

Interaction with fragments is done through FragmentManager , which can be obtained via Activity. getFragmentManager() and Fragment. getFragmentManager() .

What are the differences between onCreate () onCreateView () and onActivityCreated () in fragments and what would they each be used for?

The onCreate() is called first, for doing any non-graphical initialisations. Next, you can assign and declare any View variables you want to use in onCreateView() . Afterwards, use onActivityCreated() to do any final initialisations you want to do once everything has completed.

What is difference between onCreate and onCreateView in fragment?

onCreate is called on initial creation of the fragment. You do your non graphical initializations here. It finishes even before the layout is inflated and the fragment is visible. onCreateView is called to inflate the layout of the fragment i.e graphical initialization usually takes place here.

Which function should you use to inflate the view of a fragment?

Fragment CREATED and View INITIALIZED In most cases, you can use the fragment constructors that take a @LayoutId , which automatically inflates the view at the appropriate time. You can also override onCreateView() to programmatically inflate or create your fragment's view.


2 Answers

The way you are doing it should just work. The crash log shows us that the NPE is unrelated to the way the fragment updates its widgets.

like image 71
K-ballo Avatar answered Oct 20 '22 08:10

K-ballo


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 this approach does not lazy-load the TextView reference-- it loads it when the view is inflated as opposed to when it is needed.

like image 23
edthethird Avatar answered Oct 20 '22 06:10

edthethird