2016-08-18 67 views
0

我有一個片段類,它具有操作欄圖標。它是這樣的 -從一個片段Android啓動對話框片段

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setHasOptionsMenu(true); 
    } 

    @Override 
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 
     inflater.inflate(R.menu.action_bar_call, menu); 
     super.onCreateOptionsMenu(menu,inflater); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case R.id.call: 
      //Call Dialog fragment from here 
       CallDialogFragment dialog = CallDialogFragment .instantiate(getActivity(), "fragmentTAG"); 
      dialog.show(getFragmentManager(), "dialog"); 
       return false; 

     } 
     return false; 
    } 

編輯:

public class CallDialogFragment extends DialogFragment { 

    public CallDialogFragment() { 

    } 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) 
    { 
     View view = getActivity().getLayoutInflater().inflate(R.layout.call_dialog_fragment, new LinearLayout(getActivity()), false); 



     // Build dialog 
     Dialog builder = new Dialog(getActivity()); 
     builder.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     builder.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT)); 
     builder.setContentView(view); 
     return builder; 
    } 
} 

TheDialogFragment類看起來是這樣的。我在我的片段說法不兼容的類型

如何調用對話片段與片段

+0

http://android-developers.blogspot.com/2012/05/using-dialogfragments.html?m=1 – Mariusz

+0

我不知道爲什麼下跌投票...仍然沒有令人信服的答案 –

+0

「不兼容的類型所需:xyzCallDialog找到:android.app.Fragment」tl; dr改變「CallDialogFragment對話框= CallDialogFragment .instantiate(getActivity(),」fragmentTAG「); ''到對話框DialogFragment =新CallDialogFragment();' – Mariusz

回答

0

打電話給你的對話片段像下面

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
     case R.id.call: 
       //Call Dialog fragment from here 
       CallDialogFragment dialog = new CallDialogFragment(); 
       dialog.show(getFragmentManager(), "dialog"); 

       return true; 

     } 
     return false; 
} 
+0

你能否詳細解釋我 –

+0

我已經更新了答案。遠到顯示DialogFragment? – fluffyBatman

+0

請參閱我編輯的問題,請...它給我錯誤爲不兼容的類型。必需的CallDialogFragment找到:片段 –

0

使用dialogFragment.show(getFragmentManager(), "TAG");其中dialogFragment是你DialogFragment類的實例,"TAG"得到錯誤是字符串標籤識別片段的對話框經理。

根據您的封裝片段是支持庫還是標準片段,getFragmentManager()將返回支持或標準片段管理器。

如果需要取得該片段的實例,將先前連接,使用findFragmentByTag()方法和投射到您的對話片段類,例如:

((MyDialogFragment)getFragmentManager().findFragmentByTag(TAG))... 

由以前使用附加的標籤搜索/顯示對話框片段。

+0

我不明白 –