片段无理由地失去对活动的引用

时间:2018-05-15 11:36:12

标签: android android-fragments android-context

在我的一个片段中,我有时会说我的Context为空。

我现在无法重新创建问题,因为我尝试以多种不同的方式访问崩溃的代码而没有一致的结果。

以下是我得到的崩溃日志:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: eu.side.aurora, PID: 12940
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
        at android.widget.Toast.<init>(Toast.java:101)
        at android.widget.Toast.makeText(Toast.java:258)
        at eu.side.aurora.views.fragments.StepStock.lambda$onCreateView$2$StepStock(StepStock.java:161)

代码如果引用

    InterventionsViewModel interventionsViewmodel = ViewmodelProviders.of(getActivity()).get(InterventionsViewModel)
    interventionsViewmodel.getExceptions().observe(getActivity(), exception ->{
        if(exception instanceof QuantityOverflowException){
            Toast.makeText(getActivity(), "No you just can't add more stock than there is available!", Toast.LENGTH_SHORT).show();
        }
        if(exception instanceof ScannedArticleNotFoundException){
            Toast.makeText(getActivity(), "This article doesn't even exist! Get your shit together please!²", Toast.LENGTH_SHORT).show();
        }
     });

奇怪的是,我在片段的onCreateView中几乎无处不在地引用了getActivty(),但它在那里崩溃了。

要使用以下视图访问代码,我只需按 + - 按钮即可添加或删除列表中的文章。当它达到最大值时,它会发送Toast以通知用户。

Screen that causes the crash

点击加号减号按钮时,会通知ViewModel,这会更改我之前观察过的可观察对象。

有谁知道这是从哪里来的?我认为它来自我的片段与我的Activity分离,但我无法弄清楚它是如何做到这一点的,因为我在创建之后再也没有触摸过我的片段。

提前致谢, 马修

编辑:根据要求,这是完整片段的代码     包eu.side.aurora.views.fragments;

public class StepStock extends Fragment {
    private INextStep nextStep;

    @BindView(R.id.articles_autocomplete)
    AutoCompleteTextView articlesAutocomplete;
    @BindView(R.id.articles_recycler)
    RecyclerView articleRecycler;


    private AccountManager accountManager;
    private Account account;
    private ArrayList<DetailedArticle> articles;
    private ArrayList<Article> stockedArticles;
    private StockRecyclerAdapter stockAdapter;
    private InterventionsViewModel interventionViewModel;

    public StepStock() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment StepStock.
     */
    // TODO: Rename and change types and number of parameters
    public static StepStock newInstance() {
        return new StepStock();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.fragment_step_stock, container, false);
        ButterKnife.bind(this, view);

        interventionViewModel = ViewModelProviders.of(getActivity()).get(InterventionsViewModel.class);

        /* UI Intialization */
        articleRecycler.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));


        /**
         * Stock recycler view
         * Used to keep track of stock used in an intervention
         * OnPlus -> Add one from article
         * OnMinus -> Remove one from article
         * */
        stockedArticles = new ArrayList<>();
        stockAdapter = new StockRecyclerAdapter(stockedArticles, new StockRecyclerAdapter.OnArticleClickListener() {
            @Override
            public void onArticleClicked(int position) {
                //new intent
            }

            @Override
            public void onPlusClicked(int position) {
                interventionViewModel.addOneToStockAtArticle(stockAdapter.getData().get(position));
            }

            @Override
            public void onMinusClicked(int position) {
                interventionViewModel.minusOneOrRemoveAtArticle(stockAdapter.getData().get(position));
            }
        });

        interventionViewModel.getCurrentIntervention().observe(getActivity(), intervention -> {
            if(intervention!= null && intervention.getArticles() != null)
                stockAdapter.putData(intervention.getArticles());
        });

        interventionViewModel.getExceptions().observe(getActivity(), exception ->{
            if(exception instanceof QuantityOverflowException){
                Toast.makeText(getActivity(), "No you just can't add more stock than there is available!", Toast.LENGTH_SHORT).show();
            }
            if(exception instanceof ScannedArticleNotFoundException){
                Toast.makeText(getActivity(), "This article doesn't even exist! Get your shit together please!²", Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if(context instanceof INextStep){
            nextStep = (INextStep) context;
        }
        else{
            throw new  RuntimeException(context.toString() + " must implement INextStep");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        nextStep = null;
    }
}

2 个答案:

答案 0 :(得分:1)

我会删除此代码

Supplier

并覆盖onactivitycreated方法并将其放在那里。然后,您可以确保RetailProfileDataBuilder() { super(() -> { RetailProfile rp = new RetailProfile(); rp.setDomain("RetailBuffer"); return rp; }); } public static RetailProfileDataBuilder of() { return new RetailProfileDataBuilder(); } 不会返回null。另请注意,在observe方法中,我传递的是片段的实例,而不是活动,因此观察者会尊重片段的生命周期。

答案 1 :(得分:1)

您的代码崩溃是因为您的&#34;例外观察员&#34;忽略了片段的生命周期。

可以在许多不同的场景中附加/分离片段,例如配置更改或将应用程序移动到后台等。当分离片段时,getActivity()方法返回null,这是你的原因崩溃。

另一方面,对象interventionViewModel.getExceptions()的生命周期是完全独立的。每次对象改变时,观察者都会被调用,而不管片段处于什么状态。因此,如果片段已分离,则应用程序将崩溃。

要解决此问题 - 请在分离片段时不要使用上下文。当片段与活动分离时,通常您不想听模型更改。如果您取消注册onDetach中的观察者,则会解决您的问题。

祝你好运!