2017-01-17 1 views
0

Google 앱에 대한 여러 가지 오류 보고서가 있습니다.Context.getResources() NPE

이 버그는 Socorro 인터페이스에서 제기되었으며 보고 bp-d1b4cdb8-fd3f-457b-9a29-df9192170112입니다. 소코로 - 서명 :

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)

자바 스택 추적 :

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

그것은 여러 장치에 무슨 일이 있었 :

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 내 코드입니다.

6.0.1, 삼성-SM-G935A 6.0.1, 삼성-SM-G890A 6.0, Alco의 6.0.1, 모토로라, XT1585

그와 같은 경우 나도 몰라 https://code.google.com/p/android/issues/detail?id=56296

또한 getContext 위치와 관련이 있는지 궁금합니다. 나는 그것을 Fragment.onViewCreated에서 호출했다.

그러나 이것은 소스 코드에서 다음과 같이 말합니다. 단편의보기 계층은이 시점에서 상위에 연결되어 있지 않습니다.

+0

에? –

+0

또한 view.getcontext()를 사용할 수 있습니다. –

+0

@ ρяσѕρєяK @ keyur9779 감사합니다. 여러분은'getActivity','getContext'와'view.getContext'의 차이점을 여기에서 지적 해 주시겠습니까? –

답변

1

변경

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

왜`getActivity()`를 사용하지

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

전자가 예외를 던지는 이유를 설명해 주시겠습니까? –

+0

이전에'getContext()'가 NPE를 리턴 할 때까지 읽었습니다. 개발 중에는 getContext() 대신에'getActivity()'를 사용해야합니다. – TruongHieu

+0

어디서 읽었는지 기억하니? 또는 getContext()에 대한 자료를 NPE를 반환 할 수 있습니까? 나는 그런 일이 일어날 것을 결코 알지 못했다. –