2012-07-08 91 views
8

我用新的片段替换现有的片段,我能够看到我的视图,但在按钮上设置点击侦听器时,它返回null。我收到以下例外情况:findViewById在片段中返回空值

?:??: 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) 

我不知道发生了什么?

上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; 

我还引进com.big.xxxxxxx.R

在此先感谢您的帮助

+0

我已经清理并重新生成R.java。 – Preethi 2012-07-08 18:16:27

+0

PLZ在方法onCreateView()中添加代码,在您的xxxxx.java类的第34行中添加代码,然后为您的代码片段添加xml布局 – Houcine 2012-07-08 18:19:01

+0

感谢您的回复。我已经夸大了片段的OnCreateView视图,这就是为什么我能够查看片段,但无法设置任何类型的监听器,因为我的按钮似乎为空(即使我可以看到按钮) – Preethi 2012-07-08 18:27:58

回答

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; 

你必须调用findViewById上你的观点 - 而不是你的活动。

+1

是否有一个原因,你使用findviewbyid在活动中的活动,但在片段的视图? – user1549672 2013-08-06 20:35:54

0

原因是在onCreateView的片段视图尚未创建,所以它返回null。尝试在onResume中完成,它会返回给您视图:

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