我一直在关注有关如何使用三个 fragment 实现底部导航栏的教程。当我需要更新我的个人资料 fragment 的 TextView 时,我现在陷入困境。主要 Activity 代码:

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); 
} 

我的 profilefragment.xml 有一个 ID 为 profileData 的 TextView 。在我的 ProfileFragment java 类中,我引入了一个方法 updateTextView 来更新配置文件布局,但我不知道在哪里或如何在我的 mainactivity.java 中调用此方法。我搜索了有关此主题的几个问题,并尝试了 stackoverflow 上提出的不同解决方案,但每当我尝试在 mainactivity 中调用该方法时,我都会收到空指针异常错误。请问有人可以帮助我吗?

请您参考如下方法:

您需要在 MainActivity 中进行以下更改:

使用方法通过标记获取 fragment (即 ProfileFragment):

Profile Fragment fragment =  (ProfileFragment)getSupportFragmentManager().findFragmentByTag("tag"); 
 if(fragment != null) 
 fragment.updateTextView(); 

这里标记是一个字符串常量,您需要在将 fragment 替换为时添加它 如下:

在您的代码中进行以下更改

FragmentTransaction ft = fragmentManager.beginTransaction(); 
    if (ft != null) { 
        ft.replace(R.id.main_container, fragment,"tag"); 


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!