2016-11-30 86 views
0

我創建了OnClickListener並將其作爲參數傳遞給我的setOnBackButtonClickListener方法。當我第一次啓動應用程序時,它工作得很好,但是當我第二次打開它時,我得到一個異常。我認爲這可能與片段LifeCycle有關,因爲我在onViewCreated內調用方法。無法綁定片段內的視圖

這裏是例外:

Process: com., PID: 12660 
java.lang.RuntimeException: Unable to bind views for com.presenter.view.fragment.ForgotPasswordFragment 
at butterknife.ButterKnife.bind(ButterKnife.java:322) 
at butterknife.ButterKnife.bind(ButterKnife.java:279) 

這裏是我的片段:

@Bind(R.id.action_bar) ActionBar actionBar; 

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


     DaggerForgotPasswordComponent.builder() 
       .applicationComponent(
         ((AndroidApplication) getActivity().getApplication()).getApplicationComponent()) 
       .forgotPasswordModule(new ForgotPasswordModule()) 
       .build() 
       .inject(this); 

    } 

    View.OnClickListener cl = new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      navigateTo(LoginActivity.class); 
     } 
    }; 

    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 
     this.actionBar.setOnBackButtonClickListener(cl); 

    } 
+1

它不應該被'@ BindView'? –

+0

不是,語法在版本8.0中進行了更改。 @Murat K. – Adnan

+0

在8.4.0版本(最新版本)中,它是BindView ... *飛走* – Jaythaking

回答

2

儘量覆蓋onCreateView並結合類似與ButterKnife

@BindView(R.id.action_bar) ActionBar actionBar; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    View fragmentView = inflater.inflate(R.layout.fragment_layout, container, false); 
    ButterKnife.bind(this, fragmentView); 

    return fragmentView; 
} 
+0

謝謝,但沒有幫助我。當我第一次打開應用程序,一切都很好。當我試圖打開它秒,我得到例外。 @wojdor – Adnan