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.