Sundus
Sundus

Reputation: 468

Getting Id of a Textview in a fragment OnCreate() method

I am trying to get the Id of a text view in a fragment's onCreate() method. It returns null everytime.

I want to display my contacts in a fragment. For that at the end of FetchContact() method I assign the contact list to TextView.

Since my code is returning null when I find the TextView my application gives NullPointerException. Anyone who could tell me how to get the Id of TextView of a Fragment in onCreate() method.

I tried making an object of activity class to get the TextView, still the code returned null. No luck.

public class ContactsFragment extends Fragment {

public TextView outputtext;
Context context;

class ActivityObj extends Activity{
    public TextView text;
    Context objContext;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        text =(TextView)findViewById(R.id.textContact);
       objContext = getApplicationContext();
       super.onCreate(savedInstanceState);
    }
}


@Override
public void onCreate(Bundle savedInstanceState) {
    ActivityObj obj = new ActivityObj();
    super.onCreate(savedInstanceState);
    outputtext =(TextView) getView().findViewById(R.id.textContact);
 //  outputtext= obj.text;
    context=getActivity().getApplicationContext();

    //fetchContacts();

}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.contacts_layout, container, false);
    return rootView;
}

Upvotes: 0

Views: 1227

Answers (4)

Zar E Ahmer
Zar E Ahmer

Reputation: 34380

You cannot initialize any view using findViewById before onCreateView method calls.

So you must have to wait for the callback to run. And there are two ways to get it. And it will work on onActivityCreated because it called after onCreateView

  1. As described by ρяσѕρєя K using the View param came from onViewCreated.
  2. You can call getView() in any callback(delegate) which call after onCreateView()

Like

TextView tv = (TextView) getView().findViewById(R.id.tv_id);

Upvotes: 0

Kashif Anwaar
Kashif Anwaar

Reputation: 798

You should use like this on onActivityCreated method

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        TextView tv = (TextView) getActivity().findViewById(R.id.textView1);

        }
    }

Or use callbacks

Upvotes: 0

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132992

find the TextView my application gives NullPointerException.

Because in Fragment lifecycle onCreate method called before onCreateView method.

override onViewCreated which call after onCreateView method and use use first parameter of onViewCreated method for accessing views from Fragment layout:

@Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        TextView text =(TextView)view.findViewById(R.id.textContact);
         /// your code here....
}

Upvotes: 4

Huzefa Gadi
Huzefa Gadi

Reputation: 1149

use

rootView.findViewById(R.id....) in your oncreateView there is where you should do the UI related work

Upvotes: 2

Related Questions