我有以下课程:
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 :(得分:3)
您无法在new
的子类上使用Activity
运算符。活动本身必须经历其生命周期,然后才能将其用作Context
的有效DialogFragment
。假设您要从ExpandableListAdapter
的子类实例化FragmentActivity
,而不是保留对Context
的引用,您可以引用FragmentActivity
/ Activity
正在使用并使用此功能来检索您需要显示supportFragmentManger
DialogFragment
的实例。
如果您找到Activity
发生onClick
事件的方法,那就更好了,让其有责任展示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");
}
}
答案 1 :(得分:0)
您无法在构造函数中显示它。如果要在启动活动时显示对话框,请在onCreate
方法中执行此操作。看看Activity lifecycle。