I searched quite a lot of documentation and I wrote the complete code below, But every time I run it, it says view is null ?
Please help me
Fragment file
public class LoginFrag extends Fragment {
View view;
public LoginFrag(){
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_login, container, false);
return view;
}
public void setTextView(int layout, String content){
if(view == null){
//Always return view=null ???
Log.e("view_null","yes");
return;
}
TextView textView = view.findViewById(layout);
textView.setText(content);
}
}
MainActivity file
LoginFrag loginFrag = new LoginFrag();
goToFrag(loginFrag);
String text= "test set Text";
loginFrag.setTextView(R.id.fg,text);
funtion goToFrag
private void goToFrag(Fragment fragment){
fragment.setArguments(getIntent().getExtras());
FragmentManager fragManager = getSupportFragmentManager();
FragmentTransaction transaction = fragManager.beginTransaction();
transaction.add(R.id.fragment_container_start, fragment);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.commit();
}
TextView in Fragment layout
<TextView
android:id="@+id/fg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="" />