476rick
476rick

Reputation: 2795

findViewById in Fragment and not in (On)CreateView

I have an Activity where I do fill a grid using an adapter. When a item in the grid is pressed I want to change the text of a TextView in "NavbarFragment".

CategoryActivity.java:

 GridView grid = (GridView) findViewById(R.id.gridlayout_grid);
 grid.setAdapter(new GridAdapter(tiles));

CategoryActivity.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:orientation="vertical"
tools:context=".Activity.CategoryActivity">

<fragment android:name="app_a_tize.expressme.Fragment.NavbarFragment"
    android:id="@+id/fragNavbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<GridView
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:layout_margin="5dp"
    android:verticalSpacing="10dp"
    android:gravity="center"
    android:horizontalSpacing="10dp"
    android:layout_gravity="center"
    android:id="@+id/gridlayout_grid"/>

<fragment android:name="app_a_tize.expressme.Fragment.ConversationbarFragment"
    android:id="@+id/fragConversation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
</LinearLayout>

The onClickListener in the GridAdapter.java:

view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            TextView textView = (TextView) v.findViewById(R.id.item_name);
            String tileName = textView.getText().toString();
            System.out.println("tileName" + tileName);
            ConversationBarManager.getInstance().addWord(tileName);
            NavbarFragment navbar = new NavbarFragment();
            navbar.changeConversationBarString();
        }
   });

Function in NavbarFragment.java:

public void changeConversationBarString(){
    TextView conversationBar = (TextView) ?????.findViewById(R.id.conversationBar);
  conversationBar.setText(ConversationBarManager.getInstance().getConversationBarString());
}

In this function ^ I want to change the text of TextView conversationBar in the fragment. But I can't get the TextView because whatever I try, I get a NullPointer. Because the findViewById doesnt work.

All the questions about findViewById say you can use: getView() in the onCreateView() method.

My question is: how can change the text property of my TextView in the NavbarFragment when a item in the grid is clicked.

Upvotes: 1

Views: 962

Answers (3)

malmling
malmling

Reputation: 2518

What you probably want to do is to initialise the TextView earlier. In the #onCreateView()

like so:

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.my_fragment, container, false);

    mMyTextView = (TextView) view.findViewById(R.id.my_text_view);
}

and then in your public method:

public void changeConversationBarString(){
  mMyTextView.setText(ConversationBarManager.getInstance().getConversationBarString());
}

Upvotes: 1

Joseph Earl
Joseph Earl

Reputation: 23432

Because you have not added the Fragment to the layout using FragmentManager, the onCreateView method of the Fragment will not have been called.

Since attaching a fragment is not immediate, it would be better to call changeConversationBarString from the onViewCreated method of your fragment.

In your activity make sure you attach your fragment:

FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragment_content, new NavbarFragment());
ft.commit();

In NavbarFragment:

public void onViewCreated() {
  changeConversationBarString();
}

public void changeConversationBarString(){
  TextView conversationBar = (TextView) getView().findViewById(R.id.conversationBar);
  conversationBar.setText(ConversationBarManager.getInstance().getConversationBarString());
}

Upvotes: 0

Rodriquez
Rodriquez

Reputation: 1001

Best way is use the Butterknife Butterknife is powerfull tool

on dependencies just make:

 compile 'com.jakewharton:butterknife:7.0.1'

and on code u bind:

public class MainActivity extends ActionBarActivity {

private static final int TAB_REMOTE_POSITION = 0;
private static final int TAB_SOCIAL_POSITION = 1;

private RemoteFragment remoteFragment;
private ChatFragment chatFragment;
private BluetoothSPP bt;
private boolean bBluetooth = true;
private WiFiP2pService wiFiP2pService;

@Bind(R.id.view_pager)
ViewPager viewPager;
@Bind(R.id.tab_layout)
TabLayout tabLayout;

on fragment u initialize like(oncreate):

  ButterKnife.bind(this, v);

activity:

ButterKnife.bind(this);

Upvotes: 0

Related Questions