问题描述
- fragement中控件空指针
-
public class FoundNew extends Fragment{private EditText ed_found_id; private Button btn_ok,btn_cancel; @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { // TODO Auto-generated method stub super.onActivityCreated(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreateView(inflater, container, savedInstanceState); View founNewView = inflater.inflate(R.layout.found_new, container,false); ed_found_id = (EditText)founNewView.findViewById(R.id.ed_found_id); btn_cancel = (Button)founNewView.findViewById(R.id.btn_fundNewCancel); btn_ok = (Button)founNewView.findViewById(R.id.btn_fundNewOk); btn_cancel.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub String sttt = ed_found_id.getText().toString(); ed_found_id.setText(""); } }); return founNewView; }
}
ed_found_id.setText("");执行不过
04-13 06:31:47.860: D/AndroidRuntime(941): Shutting down VM
04-13 06:31:47.860: W/dalvikvm(941): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
04-13 06:31:47.880: E/AndroidRuntime(941): FATAL EXCEPTION: main
04-13 06:31:47.880: E/AndroidRuntime(941): java.lang.NullPointerException
04-13 06:31:47.880: E/AndroidRuntime(941): at com.example.foundmanager.FoundNew$1.onClick(FoundNew.java:77)
04-13 06:31:47.880: E/AndroidRuntime(941): at android.view.View.performClick(View.java:3511)
04-13 06:31:47.880: E/AndroidRuntime(941): at android.view.View$PerformClick.run(View.java:14105)
04-13 06:31:47.880: E/AndroidRuntime(941): at android.os.Handler.handleCallback(Handler.java:605)
04-13 06:31:47.880: E/AndroidRuntime(941): at android.os.Handler.dispatchMessage(Handler.java:92)
04-13 06:31:47.880: E/AndroidRuntime(941): at android.os.Looper.loop(Looper.java:137)
04-13 06:31:47.880: E/AndroidRuntime(941): at android.app.ActivityThread.main(ActivityThread.java:4424)
04-13 06:31:47.880: E/AndroidRuntime(941): at java.lang.reflect.Method.invokeNative(Native Method)
04-13 06:31:47.880: E/AndroidRuntime(941): at java.lang.reflect.Method.invoke(Method.java:511)
04-13 06:31:47.880: E/AndroidRuntime(941): atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-13 06:31:47.880: E/AndroidRuntime(941): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-13 06:31:47.880: E/AndroidRuntime(941): at dalvik.system.NativeStart.main(Native Method)好像ed_found_id是空指针 大神帮忙呀
解决方案
ed_found_id.getText() 都没有出错,说明 ed_found_id 不为空 。
ed_found_id.setText(""); 却不行,难道是参数的问题?在双引号中间增加一个空格试试吧。
解决方案二:
没见过get没问题,但set有问题的
如果没有跟踪过,跟踪一下看看,是不是findviewbyid的时候就是没有获取到或者获取后在fragment其他地方又重写置null了。