bao ol
bao ol

Reputation: 11

How to edit TextView in Fragment from Mainactivity

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="" />

Upvotes: 1

Views: 94

Answers (1)

gpuser
gpuser

Reputation: 1183

Please try the below way

(1)one way

public class LoginFrag extends Fragment {
View view;
String text;
public LoginFrag(String text{
  this.text = text;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
   
 view =  inflater.inflate(R.layout.fragment_login, container, false);
 setTextView(R.id.fg,text); 
 return view;
}

public void setTextView(int layout, String content){    
    TextView textView = view.findViewById(layout);
    textView.setText(content);
}

}

MainActivity{
  String text= "test set Text";
  LoginFrag loginFrag = new LoginFrag(text.toString() 
  goToFrag(loginFrag);
}

(2)second way

public class LoginFrag extends Fragment {
View view;
MainActivity mainActivty;
public LoginFrag(MainActivity mainActivty{
  this.mainActivty = mainActivty
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
   
 view =  inflater.inflate(R.layout.fragment_login, container, false); 
 return view;
}

@Override
void onStart(){
  super.onStart();
  mainActivty.refresh(this) 
}

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{
  String text= "test set Text"; 
  LoginFrag loginFrag = new       LoginFrag(text.toString() 
  goToFrag(loginFrag);

 public void onRefresh(Fragment fragment){
   if(fragment instanceOf LoginFrag){
     ((LoginFrag)fragment).setTextView(R.id.fg,text);
   }elseif(fragment instanceOf Signup){
     ((Signup)fragment).setTextView(R.id.fg,text);   
  
   }  
 }
}

(3)Third way

 LoginFragment{
       public boolean setTextView(int layout, String content){
        if(view == null){
         //Always return view=null ???
            Log.e("view_null","yes");
            return false;
        }
        TextView textView = view.findViewById(layout);
        textView.setText(content);
        return true;
      }
    }

MainActivity{

 LoginFrag loginFrag = new LoginFrag();
goToFrag(loginFrag);

  String text= "test set Text";
  new Handler().postDelayed(new Runnable(){

    boolean isSuccess = loginFrag.setTextView(R.id.fg,text); 
     if(isSuccess){
       handler.removeCallbacks(this)
       handler.postDelayed(1000);
     }      
  },1000)
  
}

Upvotes: 1

Related Questions