活动访问onAttachFragment中的片段的onCreateView小部件:NullPointerException [英] Activity accessing fragment's onCreateView's widget in onAttachFragment: NullPointerException

查看:113
本文介绍了活动访问onAttachFragment中的片段的onCreateView小部件:NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的片段中,我有一个带有汉堡菜单的搜索栏.单击后者时,必须打开导航抽屉.但这是在活动中定义的.

In my fragment, I have a search bar with a burger menu. When the latter is clicked, the navigation drawer must be opened. But it is defined in the activity.

要从我的片段访问活动抽屉,请执行以下操作:

To access the activity's drawer from my fragment:

  1. 我创建了一个包含方法 clickOnBurgerMenu

在我的活动中,我已经通过打开抽屉的方法实现了此接口

In my activity, I have implemented this interface with a method that opens its drawer

在我的活动中,我实现了方法 onAttachFragment ,在这里我将该片段的方法称为 setUpMaterialSearchBar

In my activity, I have implemented the method onAttachFragment and here I call the fragment's method setUpMaterialSearchBar

在我的片段中:在方法 setUpMaterialSearchBar(final ClickOnBurgerMenu活动)中:我调用 activity.clickOnBurgerMenu();

In my fragment: in the method setUpMaterialSearchBar(final ClickOnBurgerMenu activity): I call activity.clickOnBurgerMenu();

但是问题在于,在片段的方法 setUpMaterialSearchBar 中,对 activity.clickOnBurgerMenu(); 的调用是在搜索栏上设置的侦听器中执行的.后者在此片段的 onCreateView 方法中定义.

But the problem is that in the fragment's method setUpMaterialSearchBar, the call to activity.clickOnBurgerMenu(); is executed in a listener set on the search bar. The latter being defined in the method onCreateView of this fragment.

因此,在附加片段时,活动将执行其 setUpMaterialSearchBar 方法,以便使用活动的抽屉设置侦听器,但此时物料搜索栏不存在:侦听器为在 null 引用上设置,并抛出 NullPointerException .换句话说,更确切地说: material_search_bar 仅在 onCreateView 中定义,在活动的 onAttachFragment 之后调用-这就是问题所在.

So when the fragment is attached, the activity executes its setUpMaterialSearchBar method in order to set the listener with the activity's drawer, but the material search bar doesn't exist at this time: the listener is set on a null reference and a NullPointerException is thrown. In other words, more concretly: material_search_bar is defined only in onCreateView, called after the activity's onAttachFragment - that's the problem.

我该如何解决这个问题?

How could I solve this problem?

来源

public interface ClickOnMaterialSearchBarBurgerMenu {
    void clickOnMaterialSearchBarBurgerMenu();
}

活动(实现此接口)

@Override
public void onAttachFragment(Fragment fragment) {
    if (fragment instanceof HomeFragment) {
        HomeFragment home_fragment = (HomeFragment) fragment;
        home_fragment.setUpMaterialSearchBar(this);
    }
}

@Override
public void clickOnMaterialSearchBarBurgerMenu() {
    drawer_layout.openDrawer(Gravity.START);
}

片段(例如, material_search_bar 仅在 onCreateView 中定义,在活动的 onAttachFragment 之后调用-这就是问题所在)

The fragment (material_search_bar for example is defined only in onCreateView, called after the activity's onAttachFragment - that's the problem)

public void setUpMaterialSearchBar(final ClickOnMaterialSearchBarBurgerMenu activity) {
    material_search_bar.setOnSearchActionListener(new SimpleOnSearchActionListener() {
        @Override
        public void onButtonClicked(int buttonCode) {
            switch (buttonCode){
                case MaterialSearchBar.BUTTON_NAVIGATION:
                    activity.clickOnMaterialSearchBarBurgerMenu();
                    break;
                case MaterialSearchBar.BUTTON_BACK:
                etc. etc. etc.

推荐答案

为了阐明您的逻辑,您可以引入

In order to untangle your logic, you can introduce a

私有SimpleOnSearchActionListener sosActionListener; HomeFragment 中.

更改 setUpMaterialSearchBar()如下:

public void setUpMaterialSearchBar(final ClickOnMaterialSearchBarBurgerMenu activity) {
    sosActionListener = new SimpleOnSearchActionListener() {
        @Override
        public void onButtonClicked(int buttonCode) {
            switch (buttonCode){
                case MaterialSearchBar.BUTTON_NAVIGATION:
                    activity.clickOnMaterialSearchBarBurgerMenu();
                    break;
                case MaterialSearchBar.BUTTON_BACK:
                // etc. etc. etc.
            }
        }
    };
    if (material_search_bar != null){
        material_search_bar.setOnSearchActionListener(sosActionListener);
    }
}

初始化 material_search_bar

material_search_bar.setOnSearchActionListener(sosActionListener);

这篇关于活动访问onAttachFragment中的片段的onCreateView小部件:NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆