2014-12-08 46 views
-1

TextView的我有以下代碼:空指針異常是發生在初始化使用Butterknife

public static class PlaceholderFragment extends Fragment { 

    private static final String ARG_SECTION_NUMBER = "section_number"; 

    @InjectView(R.id.testTv) 
    TextView textView;    

    public static PlaceholderFragment newInstance(int sectionNumber) { 
     PlaceholderFragment fragment = new PlaceholderFragment(); 
     Bundle args = new Bundle(); 
     args.putInt(ARG_SECTION_NUMBER, sectionNumber); 
     args.putString("product_url"); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    public PlaceholderFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_userprofile_myaccount, container, false); 

     ButterKnife.inject(getActivity(), rootView); 

     textView.setText("Hello"); 

     return rootView; 
    } 

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

} 

錯誤正在發生上的線與textview.setText("Hello");

+0

可能重複[什麼是空指針異常,並且我怎麼修復它?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – 2014-12-08 09:42:19

回答

4

你應該閱讀document仔細:)

不是:

ButterKnife.inject(getActivity(), rootView); 

變化:的

ButterKnife.inject(this, rootView); 
+0

謝謝..問題解決.. – 2014-12-09 10:41:39

+0

而不是@@@@@@@全部 – Jerome 2014-12-09 10:45:54