Reputation: 18130
I'm trying to toast when a particular editText is hit in my fragment.
My fragment activity:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
EditText ed2 = (EditText) getView().findViewById(R.id.editText2);
ed2.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(MotionEvent.ACTION_UP == event.getAction()){
Toast.makeText(getActivity(), "Hello", Toast.LENGTH_LONG).show();
}
return false;
}
});
}
My application crashes when this fragment is added. I'm getting a null pointer at ed2.setOnTouchListener...
.
Any ideas?
Upvotes: 0
Views: 2205
Reputation: 176
EditText is null. if your contentview is not cotain edittext , edittext can't created. OnTouchListener is not null. first check EditText .
Upvotes: 1
Reputation: 13731
use onCreateView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.your_view, container, false);
EditText = ed2 = (EditText) getView().findViewById(R.id.editText2);
ed2.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(MotionEvent.ACTION_UP == event.getAction()){
Toast.makeText(getActivity(), "Hello", Toast.LENGTH_LONG).show();
}
return false;
}
});
return view;
}
Upvotes: 1