Context.getResources()NPE

时间:2017-01-17 02:52:12

标签: android android-fragments

我收到了几个关于我们应用的崩溃报告。

此错误是从Socorro界面提交的,并且是 报告bp-d1b4cdb8-fd3f-457b-9a29-df9192170112。 Socorro-Signature:

java.lang.NullPointerException: Attempt to invoke virtual method ''android.content.res.Resources android.content.Context.getResources()'' on a null object reference: at dnu.afterTextChanged(CommentOperatingFragment.java)

Java堆栈跟踪:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
     at dnu.afterTextChanged(CommentOperatingFragment.java:252)
     at android.widget.TextView.sendAfterTextChanged(TextView.java:7929)
     at android.widget.TextView.setText(TextView.java:4084)
     at android.widget.TextView.setText(TextView.java:3933)
     at android.widget.EditText.setText(EditText.java:85)
     at android.widget.TextView.setText(TextView.java:3908)
     at dnv.a(CommentOperatingFragment.java:269)
     at dpz.a(SoftKeyboardStateHelper.java:84)
     at dpz.onGlobalLayout(SoftKeyboardStateHelper.java:47)
     at android.view.ViewTreeObserver.dispatchOnGlobalLayout(ViewTreeObserver.java:912)
     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1899)
     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1079)
     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5915)
     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:773)
     at android.view.Choreographer.doCallbacks(Choreographer.java:586)
     at android.view.Choreographer.doFrame(Choreographer.java:556)
     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:759)
     at android.os.Handler.handleCallback(Handler.java:739)
     at android.os.Handler.dispatchMessage(Handler.java:95)
     at android.os.Looper.loop(Looper.java:135)

这是我在CommentOperatingFragment中的代码:

@Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        …...
        mCommentEdt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence,
                                          int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence,
                                      int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (mCommentEdt.getText().toString().isEmpty()) {
                    mPostTxt.setClickable(false);
                    mPostTxt.setTextColor(ResourcesCompat.getColor(getContext().getResources(),// Line 252
                            R.color.article_comment_area_post_color_without_text, null));
                } else {
                    mPostTxt.setClickable(true);
                    mPostTxt.setTextColor(ResourcesCompat.getColor(getContext().getResources(),
                            R.color.article_comment_area_post_color_with_text, null));
                }
            }
        });
}

它发生在几个设备上。

6.0.1,SAMSUNG-SM-G935A 6.0.1,SAMSUNG-SM-G890A 6.0,Alco 6.0.1,摩托罗拉,XT1585

我不知道它是否与https://code.google.com/p/android/issues/detail?id=56296

相同

我也想知道它是否与getContext位置有关。我在Fragment.onViewCreated中调用它。

但它在源代码中说:此时片段的视图层次结构不会附加到其父级。

1 个答案:

答案 0 :(得分:1)

更改

ResourcesCompat.getColor(getContext().getResources(),
                            R.color.article_comment_area_post_color_with_text, null)

ContextCompat.getColor(getActivity(), R.color.article_comment_area_post_color_with_text)