Android : How to call a Fragment from CustomView

on Monday, September 29, 2014


I created a Custom EditTextView and I want to call a Fragment when Custom EditTextView has focused.


I write a code like this, but I have got a error when call fragment.


if (someone has a good idea to call Fragment from custom View){please teach me();}


enter image description here


this is my code:


public class OriginalEditText extends EditText {



public OriginalEditText(Context context) {
super(context);
// TODO Auto-generated constructor stub
}

public OriginalEditText(Context context, AttributeSet attrs) {
super(context, attrs);
setText("");
setHint("it is original");

setOnFocusChangeListener(new OnFocusChangeListener() {

@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
// ////////////////////////////////////////////
// /I want to Call Fragment here. ////////////
// //////////////////////////////////////////
Fragment fgm = null;
testFragment testFragment=new testFragment();
FragmentManager manager=fgm.getFragmentManager();//null pointerException occurred here
FragmentTransaction transaction=manager.beginTransaction();

transaction.add(R.id.originalEditText1,testFragment, "test" );
transaction.commit();
// ///////////////////////////////////////////////////////////////////////////////
// /but above code not work.....How to call Fragment from CustomView ////////////
// /////////////////////////////////////////////////////////////////////////////


}

}
});

}

public OriginalEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}


}


0 comments:

Post a Comment