5

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)
StuStirling
  • 15,601
  • 23
  • 93
  • 150

5 Answers5

3

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.

K-ballo
  • 80,396
  • 20
  • 159
  • 169
  • thanks for your help. it is the way I'm referencing the fragment, I must be doing it wrong – StuStirling Jun 25 '12 at 16:58
  • try using if((getSupportFragmentManager.findFragmentBy[Id|Tag]!=null) before attempting to reference the fragment. **getSupportFragmentManager if it's using the compatability lib and getFragmentManager if not and targeting >3.0 – Zachary Moshansky Nov 22 '12 at 00:18
2

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.

edthethird
  • 6,263
  • 2
  • 24
  • 34
2

If someone is trying to acces any view from fragment's layout in Fragment's class outside onCreateView, then you can make the inflating view declared at class level and link it after inflating statement and use that view reference to find view anywhere in Fragment class: for example...........

 public class FragmentA extends Fragment implements onClickListener{

//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();
    // or from a button click 
          Button button = view.findViewById(R.id.button);
          button.setOnClickListener(this)

    return view;

}

private void setTextViewNow() {

    //use 'view' to find the id of textview using findviewById();   
     if(view !=null){
    TextView myView = (TextView)view.findViewById(R.id.textViewMyView);
    myView.setText("THis is set from outside oncreateView");           
        }
   }

    @Override
    public void  onClick(View view){
       setTextViewNow();
     }
 }
Murli
  • 524
  • 6
  • 11
  • 2
    This works only because `setTextViewNow()` is called from `onCreateView()`. If `setTextViewNow()` were called from, say, a listener, then the `view` class member would be null. – Al Lelopath Apr 14 '15 at 19:40
1

I was having a similar problem, turns out I was overriding the getItem(int) method of the Fragment and returning a new instance of Fragment every time, in your case I do not know if you also are overriding it, but, you should check HistoryFragment.onListItemClick to see if you are calling the getItem method and returning a new instance of Fragment which is not the same as the one you are showing on screen.

Tim
  • 35,413
  • 11
  • 95
  • 121
Lalo
  • 11
  • 1
-1

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

xunien
  • 185
  • 1
  • 9