如何从 Mainactivity 编辑 Fragment 中的 TextView
Posted
技术标签:
【中文标题】如何从 Mainactivity 编辑 Fragment 中的 TextView【英文标题】:How to edit TextView in Fragment from Mainactivity 【发布时间】:2021-12-24 14:13:35 【问题描述】:我搜索了很多文档,并在下面编写了完整的代码, 但是每次我运行它时,它都说 view is null ?
请帮帮我
片段文件
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 文件
LoginFrag loginFrag = new LoginFrag();
goToFrag(loginFrag);
String text= "test set Text";
loginFrag.setTextView(R.id.fg,text);
函数 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
<TextView
android:id="@+id/fg"
android:layout_
android:layout_
android:text="" />
【问题讨论】:
【参考方案1】:请尝试以下方法
(1)单向
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)第二种方式
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)第三条路
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)
【讨论】:
在片段构造函数中传递参数不是一个好方法 - best practice for instantiating a fragment 在上面的答案中刚刚分享了3种方法来解决@bao ol根据这里的问题所面临的实际问题......希望它可以帮助以上是关于如何从 Mainactivity 编辑 Fragment 中的 TextView的主要内容,如果未能解决你的问题,请参考以下文章
如何从 WebView Cordova AngularJS 控制器访问/发送数据到 MainActivity?
如何每次从 Launcher Icon 启动 MainActivity?
如何从扩展 JobService 的类中调用将视图作为参数的 MainActivity 方法?