我創建了自定義EditTextView,並且我想在自定義EditTextView聚焦時調用片段。如何從自定義視圖調用片段
我寫了這樣的代碼,但在調用片段時出現錯誤。
如果(有人有一個好主意,從定製的視圖調用片段){教我();}
這是我的代碼:
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
}
}
謝謝!你的答案真的很有幫助! – 2014-09-29 14:10:00
樂於幫助。我看到你對StackOverflow來說比較新。如果答案是有幫助的,則對其進行投票。如果答案正確,請將其標記爲已接受(綠色勾號)。 – 2014-09-29 22:10:54