0

I am not able to get the reference of TextView(defined in Fragment's xml) from Activity class. Can anyone please look into it what am I missing here.

My activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
/>

And fragment_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:background="#FFEBCC">
<TextView android:id="@+id/dateArea" android:layout_height="wrap_content" android:layout_width="wrap_content" 
    android:text="@string/app_name"/>

And below Activity class Toasts NULL value of dateArea TextVew:

public class MainActivity extends ActionBarActivity {
    ....
    ....
    public static class PlaceholderFragment extends Fragment {
        public PlaceholderFragment() {}

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }
    ....
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
        dateArea = (TextView)findViewById(R.id.dateArea);
        Toast.makeText(this, dateArea.toString(),Toast.LENGTH_LONG).show(); //dateArea is NULL !!
    }
}

Thanks for help.

ravi tiwari
  • 521
  • 1
  • 8
  • 24
  • `dateArea.toString()` can't actually toast "NULL" - if `dateArea` is null, `dateArea.toString()` will crash. I suspect this isn't the actual code. – Ryan M May 16 '23 at 06:55

2 Answers2

0

Try this..

Your TextView belongs to fragment_main.xml so you have ti initialize it in PlaceholderFragment and also you have to get the text before displaying it in Toast

public static class PlaceholderFragment extends Fragment {
        public PlaceholderFragment() {}
        TextView dateArea;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            dateArea = (TextView) rootView.findViewById(R.id.dateArea);
            Toast.makeText(getActivity(), dateArea.getText().toString(),Toast.LENGTH_LONG).show();
            return rootView;
        }
    }
Hariharan
  • 24,741
  • 6
  • 50
  • 54
  • No Hariharan... Of course I have already defined dateArea. It is defined at the top (I have ellipsis'd that portion).. Here it is: private static TextView dateArea; I am just printing the Object. Its coming as NULL instead of some reference code.. – ravi tiwari Apr 17 '14 at 10:40
  • @ravitiwari i said you have to initialize `TextView` `onCreateView` not `onCreate`. see my ans clearly. – Hariharan Apr 17 '14 at 10:42
  • No sir. You can try at your end and this doesn't work. Try below steps: 1) Define dateArea as global variable (i.e. outside PlaceholderFragment). 2) Initialize it inside onCreateView in PlaceholderFragment as you already have done. 3) Toast it inside Activity class (i.e. inside onCreate function). It will produce NULL. – ravi tiwari Apr 17 '14 at 10:55
  • @ravitiwari give the toast inside `onCreateView`. Because `onCreate` will execute before `onCreateView`. So that you are getting null value. – Hariharan Apr 17 '14 at 10:56
  • then you are saying we can not refer fragment's UI controls from inside the main Activity?? We have to refer only from within Fragment class?? – ravi tiwari Apr 17 '14 at 11:15
  • @ravitiwari yes exactly. – Hariharan Apr 17 '14 at 11:16
0

You have to obtain Fragment's View instance in your Activity if you want to reference fragment's UI elements.
fragment.getView().findViewByID(id);
Because fragments and activitys have thier "own" findViewByID method that refers thier inflated layout

user3455363
  • 408
  • 1
  • 5
  • 13