2012-07-08 18 views
8

Zastępuję istniejący fragment nowym fragmentem i widzę mój widok, ale podczas ustawiania klikacza na przycisku zwraca on wartość null. Otrzymuję następujący wyjątek:findViewById zwracająca wartość null w fragmencie

?:??: W/?(?): java.lang.NullPointerException 
?:??: W/?(?): at com.biggu.shopsavvy.fragments.xxxxxxxx.onCreateView(xxxxxx.java:34) 
?:??: W/?(?): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:870) 
?:??: W/?(?): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080) 
?:??: W/?(?): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622) 
?:??: W/?(?): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416) 
?:??: W/?(?): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:420) 
?:??: W/?(?): at android.os.Handler.handleCallback(Handler.java:615) 
?:??: W/?(?): at android.os.Handler.dispatchMessage(Handler.java:92) 
?:??: W/?(?): at android.os.Looper.loop(Looper.java:137) 
?:??: W/?(?): at android.app.ActivityThread.main(ActivityThread.java:4745) 
?:??: W/?(?): at java.lang.reflect.Method.invokeNative(Native Method) 
?:??: W/?(?): at java.lang.reflect.Method.invoke(Method.java:511) 
?:??: W/?(?): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
?:??: W/?(?): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
?:??: W/?(?): at dalvik.system.NativeStart.main(Native Method) 

Nie mam pojęcia, co się dzieje?

Kod na OnCreateView:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.capture_card_phone_number, container, false); 
     mPhone = (AutoCompleteTextView) getActivity().findViewById(R.id.phone_number); 
     Button next = (Button) getActivity().findViewById(R.id.capture_phone_next); 
     next.setOnClickListener(this); 
     // next.setEnabled(false); 

     return view; 

Mam również importowane com.big.xxxxxxx.R

Z góry dzięki za pomoc

+0

Mam już oczyszczone i zregenerowane R.java. – Preethi

+0

plz dodaj kod w metodzie onCreateView(), w szczególności w linii 34 twojej klasy xxxxx.java, i dodaj także układ xml dla twojego fragmentu – Houcine

+0

Dzięki za odpowiedź. Napełniłem widok w OnCreateView fragmentu, dlatego jestem w stanie wyświetlić fragment, ale nie mogę ustawić żadnego rodzaju detektorów, ponieważ mój przycisk wydaje się być pusty (mimo że widzę przycisk) – Preethi

Odpowiedz

23
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle 

    savedInstanceState) { 
      View view = inflater.inflate(R.layout.capture_card_phone_number, container, false); 
      mPhone = (AutoCompleteTextView) getActivity().findViewById(R.id.phone_number); 
      Button next = (Button) view.findViewById(R.id.capture_phone_next); 
      next.setOnClickListener(this); 


      return view; 

Trzeba zadzwonić na findViewById twój widok - nie na twojej aktywności.

+1

czy istnieje powód, że używasz findviewbyid do działania w działaniu, ale do widoku w fragmencie? – user1549672

0

Powód jest taki, że w onCreateView widok fragmentu nie jest jeszcze utworzony, więc zwraca wartość null. Spróbuj to zrobić w onResume, a zwróci Ci widok:

@Override 
public void onResume() { 
    super.onResume(); 
    mPhone = (AutoCompleteTextView) getActivity().findViewById(R.id.phone_number); 
    Button next = (Button) getActivity().findViewById(R.id.capture_phone_next); 
    next.setOnClickListener(this); 
} 
Powiązane problemy