2015-07-01 72 views
-2

顯示我有以下類:DialogFragment不上按一下按鈕

public class MyAlertDialogFragment extends DialogFragment { 


    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     return new AlertDialog.Builder(getActivity()) 
       // Set Dialog Icon 
       .setIcon(R.drawable.ic_launcher) 
       // Set Dialog Title 
       .setTitle("Alert DialogFragment") 
       // Set Dialog Message 
       .setMessage("Alert DialogFragment Tutorial") 

       // Positive button 
       .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         // Do something else 
        } 
       }) 

       // Negative Button 
       .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         // Do something else 
        } 
       }).create(); 
    } 
} 

,並從中我打電話這個類的其他類。

public class ExpandableListAdapter extends BaseExpandableListAdapter { 

    private Context _context; 
    private List<?> _listDataHeader; // header titles 
    // child data in format of header title, child title 
    private HashMap<String, List<String>> _listDataChild; 

    public ExpandableListAdapter(Context context, List<?> listDataHeader, 
      HashMap<String, List<String>> listChildData) { 
     this._context = context; 
     this._listDataHeader = listDataHeader; 
     this._listDataChild = listChildData; 
    } 

    @Override 
    public Object getChild(int groupPosition, int childPosititon) { 
     return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
       .get(childPosititon); 
    } 

    @Override 
    public long getChildId(int groupPosition, int childPosition) { 
     return childPosition; 
    } 

    @Override 
    public View getChildView(int groupPosition, final int childPosition, 
      boolean isLastChild, View convertView, ViewGroup parent) { 
       .... 
    } 

    .... 
     .... 

    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, 
      View convertView, ViewGroup parent) { 


     final Button btn_login = (Button)convertView.findViewById(R.id.btn_login); 

     btn_print.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       new LineupFragment(); 


      } 
     }); 

     return convertView; 
    } 


    public class LineupFragment extends FragmentActivity{ 

     public LineupFragment() { 

       MyAlertDialogFragment alert = new MyAlertDialogFragment(); 
       alert.show(getSupportFragmentManager(), "Alert_Dialog"); 
     } 

     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 


     } 

    } 
} 

當我點擊按鈕,然後它給出了強制關閉錯誤。

FATAL EXCEPTION: main 
Process: com.novum.ideal, PID: 9408 
    java.lang.IllegalStateException: Activity has been destroyed 
    at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1387) 
    at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:636) 
    at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:615) 
    at android.support.v4.app.DialogFragment.show(DialogFragment.java:138) 
    at com.novum.ideal.ExpandableListAdapter$LineupFragment.<init>(ExpandableListAdapter.java:149) 
    at com.novum.ideal.ExpandableListAdapter$1.onClick(ExpandableListAdapter.java:126) 
    at android.view.View.performClick(View.java:4463) 
    at android.view.View$PerformClick.run(View.java:18770) 
    at android.os.Handler.handleCallback(Handler.java:808) 
    at android.os.Handler.dispatchMessage(Handler.java:103) 
    at android.os.Looper.loop(Looper.java:193) 

我想只顯示來自這個類的對話框。

我該如何解決它?

+0

你不需要我有'oncreate'加入LineupFragment – Eoin

回答

0

你不能在構造函數中顯示它。如果要在活動啓動時顯示對話框,請在onCreate方法內執行。看看Activity lifecycle

+0

但它現在仍然顯示對話框。 – deepak

+0

請參閱@Blackbelt回答。 – MightySeal

3

您不能在Activity的子類上使用new運算符。活動本身必須經歷其所有生命週期,然後才能將其用作有效的Context,對於您的DialogFragment。假設您正在從FragmentActivity的子類中實例化ExpandableListAdapter,而不是保留對Context的引用,則可以參考您正在使用的FragmentActivity/Activity,並使用這個參數來檢索您需要顯示的supportFragmentManger的實例您的DialogFragment

如果您找到通往Activity事件發生的途徑,那麼將會有更好的辦法,因此需要顯示DialogFragment

E.g

,而不必

private Context _context; 
public ExpandableListAdapter(Context context 

你可以有

private FragmentActivity mActivity; 
public ExpandableListAdapter(FragmentActivity activity 

和的onClick

@Override 
public void onClick(View v) { 
    if (!mActivity.isFinishing()) { 
     new MyAlertDialogFragment().show(mActivity.getSupportFragmentManager(), "Alert_Dialog"); 
    } 
} 
+0

你能通過任何例子來解釋嗎?我很困惑。 – deepak

+0

哪一部分讓你感到困惑? – Blackbelt

+0

如何從onClick事件中調用DialogFragment。 – deepak