2014-09-29 32 views
0

我創建了自定義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 
    } 

} 

回答

0

您設置FGM爲NULL,上面兩行。顯然,當你編寫fgm.getFragmentManager()時你會得到一個空指針異常。我建議你在activity/fragment中設置你的自定義視圖的onFocusListener。

爲了提供活動和片段之間的溝通,您可以點擊此鏈接: link

0

我覺得你的問題的一部分可能來自你的FragmentManager理解。
在參考文檔中,FragmentManager被描述爲「與活動內部的片段對象交互的界面」。

在您的代碼,您提供您的OnFocusChangeListener作爲一個匿名內部類,並創建你設置爲null一個Fragment稱爲fgm。因此,當您調用fgm.getFragmentManager()時,您正在有效地嘗試獲取尚未分配給ActivityFragmentFragmentManager

您可以做的是確保您在創建自定義編輯文本的實例時通過Activity作爲Context。然後,您可以使用對該Activity的引用來獲取FragmentManager。

public class OriginalEditText extends EditText { 

    private Activity mActivity; 

    public OriginalEditText(Context context) { 
     super(context); 
     castContextAsActivity(context); 
    } 

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

     setOnFocusChangeListener(new OnFocusChangeListener() { 

      @Override 
      public void onFocusChange(View v, boolean hasFocus) { 
       if (hasFocus) { 
        if (mActivity != null) { 
         FragmentManager manager = mActivty.getFragmentManager(); 
         ... 
         // You now have your FragmentManager and can do what you want with it. 
        } 
       } 

      } 
     }); 

    } 

    private castContextAsActivity(Context context) { 
     try{ 
      // Try and cast the supplied context as an Activity 
      mActivity = (Activity) context; 
     } catch (ClassCastException e) { 
      // If we've caught an exception, then the supplied context was not an Activity. 
      Log.e("OriginalEditText", "Error: You must supply an Activity as the Context!"); 
     }   
    } 
    ... 
} 
+0

謝謝!你的答案真的很有幫助! – 2014-09-29 14:10:00

+0

樂於幫助。我看到你對StackOverflow來說比較新。如果答案是有幫助的,則對其進行投票。如果答案正確,請將其標記爲已接受(綠色勾號)。 – 2014-09-29 22:10:54

相關問題