Java 更改片段';从主活动中选择文本视图
我一直在学习如何用三个片段实现底部导航栏的教程。现在,当我需要更新我的个人资料片段的文本视图时,我被卡住了。主要活动代码:Java 更改片段';从主活动中选择文本视图,java,android,android-fragments,Java,Android,Android Fragments,我一直在学习如何用三个片段实现底部导航栏的教程。现在,当我需要更新我的个人资料片段的文本视图时,我被卡住了。主要活动代码: public class MainActivity extends AppCompatActivity { String activeUser = "", response="myresponse"; ProgressDialog progDailog; @Override protected void onCreate(Bundle savedInsta
public class MainActivity extends AppCompatActivity {
String activeUser = "", response="myresponse";
ProgressDialog progDailog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activeUser = getIntent().getStringExtra("currentUser");
setupNavigationView();
new getProfileData().execute("myurl");
getSupportActionBar().setTitle("Home");
}
private void setupNavigationView() {
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
if (bottomNavigationView != null) {
// Select first menu item by default and show Fragment accordingly.
Menu menu = bottomNavigationView.getMenu();
selectFragment(menu.getItem(1));
// Set action to perform when any menu-item is selected.
bottomNavigationView.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
selectFragment(item);
return false;
}
});
}
}
/**
* Perform action when any item is selected.
*
* @param item Item that is selected.
*/
protected void selectFragment(MenuItem item) {
item.setChecked(true);
switch (item.getItemId()) {
case R.id.menu_home:
// Action to perform when Home Menu item is selected.
pushFragment(new HomeFragment());
break;
case R.id.menu_news:
// Action to perform when News Menu item is selected.
pushFragment(new NewsFragment());
break;
case R.id.menu_profile:
// Action to perform when Profile Menu item is selected.
pushFragment(new ProfileFragment());
break;
}
}
/**
* Method to push any fragment into given id.
*
* @param fragment An instance of Fragment to show into the given id.
*/
protected void pushFragment(Fragment fragment) {
if (fragment == null)
return;
FragmentManager fragmentManager = getFragmentManager();
if (fragmentManager != null) {
FragmentTransaction ft = fragmentManager.beginTransaction();
if (ft != null) {
ft.replace(R.id.main_container, fragment);
ft.commit();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.three_dots_items, menu);
return true;
}
ProfileFragment代码:
public class ProfileFragment extends Fragment {
TextView profile;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.profile_fragment, container, false);
profile = (TextView) view.findViewById(profileData);
return view;
}
public void updateTextView(String text){
profile.setText(text);
}
My profilefragment.xml有一个id为profileData的textview。在我的ProfileFragment java类中,我引入了一个方法updateTextView来更新概要文件布局,但我不知道在mainactivity.java中在何处或如何调用该方法。我已经搜索了几个关于这个主题的问题,并尝试了stackoverflow上提出的不同解决方案,但每当我试图在mainactivity中调用该方法时,我总是收到一个空指针异常错误。有人能帮我吗?您需要在
MainActivity
中进行以下更改:
使用以下方法通过标记获取片段(即ProfileFragment
):
Profile Fragment fragment = (ProfileFragment)getSupportFragmentManager().findFragmentByTag("tag");
if(fragment != null)
fragment.updateTextView();
这里的标签是一个字符串常量,您需要在将片段替换为时添加它
下:
在代码中进行以下更改
FragmentTransaction ft = fragmentManager.beginTransaction();
if (ft != null) {
ft.replace(R.id.main_container, fragment,"tag");
在Fragment
public static ProfileFragment mProfileFragment =null;
public static ProfileFragment getInstance(){
if(mProfileFragment == null){
mProfileFragment = new ProfileFragment();
}
return mProfileFragment;
}
当您试图设置文本值时,请在活动
类中检查片段
的第一个实例是否为null。如果不为空,则设置文本值
if(ProfileFragment.getInstance() != null){
textview.SetText("Your Text Value here");
}
用
片段
替换容器的内容时,始终可以通过调用getSupportFragmentManaget().findFragmentById(…)
来获取当前显示的片段
。然后可以检查当前片段是否为ProfileFragment
,如果是,则进行更新:
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.main_container);
if (fragment instanceof ProfileFragment) {
((ProfileFragment) fragment).updateTextView(text);
}
您在哪里调用了updateTextView(字符串文本)方法?