您好我正在尝试在我的某个标签中显示一个对话框片段,但是当我点击该按钮时,它显示错误为
android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌android.app.LocalActivityManager$LocalActivityRecord@4137eb30是 无效;你的活动在运行吗?
但是相同的代码在正常活动中工作(没有制表符的意思)。我尝试了不同的解决方案,但没有一个正在运作。
以下是代码。
public class MyDialogFragment extends DialogFragment {
static Context mn;
static Activity ji;
static MyDialogFragment newInstance() {
String title = "is this success";
MyDialogFragment f = new MyDialogFragment();
Bundle args = new Bundle();
args.putString("title", title);
f.setArguments(args);
return f;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String title = getArguments().getString("title");
Dialog myDialog = new AlertDialog.Builder(getActivity()).setIcon(R.drawable.ic_launcher).setTitle(title)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
((Code) getActivity()).okClicked();
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
}).create();
return myDialog;
}
}
活动
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
mthird.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
OpenDialog();
}
});
}
void OpenDialog() {
MyDialogFragment myDialogFragment = MyDialogFragment.newInstance();
myDialogFragment.show(getFragmentManager(), "myDialogFragment");
System.out.println("cancelbutton");
}
public void okClicked() {
String textok = " Title";
MySecondDialog myDialogFragment = MySecondDialog.newInstance(textok);
myDialogFragment.show(getFragmentManager(), "myDialog");
}
public void cancelClicked() {
String textcancel = "Reason";
MySecondDialog myDialogFragment = MySecondDialog.newInstance(textcancel);
myDialogFragment.show(getFragmentManager(), "myDialogFragment");
}
}
答案 0 :(得分:2)
尝试使用您的父活动上下文赞
(Activity(mn)).getParent()