2015-12-11 22 views
24

Używam Butterknife po raz pierwszy, ale coś musi być nie tak. Mam fragment i Listview oraz TextView tylko do testowania, ale Butterknife nie będzie wiązał moich zmiennych:Scyzoryk Android - wiązanie w fragmencie

public class MyFragment extends Fragment { 

    @Bind(R.id.resultListView) ListView resultList; 

    @Bind(R.id.textView1) TextView test; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_my, container, false); 
     ButterKnife.bind(this, view); 
     System.out.println(resultList); //null 
     System.out.println(view.findViewById(R.id.resultListView)); //works 
     System.out.println(test); //null 
     System.out.println(view.findViewById(R.id.textView1)); //works 
     return view; 
    } 

} 

Bez wyjątku lub czegokolwiek. Ręczne wiązanie działa, więc moje Widoki muszą tam być.

+0

Co twoi zależności wyglądać? –

+0

Właśnie mam w sobie słoik Butterknife. – breakline

+0

ahh, więc używasz zaćmienia? –

Odpowiedz

11

Pod względem kodowania, wygląda dobrze. Tak więc w oparciu o komentarze, wygląda na to trzeba skonfigurować przetwarzania adnotacji w Eclipse: http://jakewharton.github.io/butterknife/ide-eclipse.html

+0

Dzięki, zrobiono to, ale co ciekawe wygenerowany folder jest pusty. Nie widziałem tego, ale myślę, że to ma coś wspólnego z tym? – breakline

+0

Co zrobić, jeśli robisz czystą kompilację? –

+0

To samo. Zakładam, że procesor adnotacji nigdy nie zostanie wywołany? – breakline

24

To praca dla mnie:

Gradle

compile 'com.jakewharton:butterknife:8.6.0' 
annotationProcessor 'com.jakewharton:butterknife-compiler:8.6.0' 

Kod

. 
... 

@BindView(R.id.text_input) 
TextView text_input; 

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

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

    text_input.setText("Lorem Ipsum"); 
... 
. 
+2

dzięki za moją pracę dla mnie –

4

także pamiętaj o zwolnieniu, gdy skończysz:

private Unbinder unbinder; 

...

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.finalisation_step_fragment, container, false); 
     unbinder = ButterKnife.bind(this, v); 
     //initialize your UI 

     return v; 
    } 

...

@Override public void onDestroyView() { 
     super.onDestroyView(); 
     unbinder.unbind(); 
    } 
Powiązane problemy