gpt4 book ai didi

android - 带有对话框 fragment 的 tabview android.view.WindowManager$BadTokenException :

转载 作者:行者123 更新时间:2023-11-29 22:11:13 24 4
gpt4 key购买 nike

您好,我正在尝试在我的一个选项卡中显示一个对话框 fragment ,但是当我单击该按钮时,它显示错误为

android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@4137eb30 is not valid; is your activity running?

但相同的代码在正常 Activity 中工作(意味着没有选项卡)。我尝试了不同的解决方案,但都没有用。

下面是代码。

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;
}
}

Activity

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");
}

}

最佳答案

尝试使用您的父 Activity 上下文 Like

(Activity(mn)).getParent()

关于android - 带有对话框 fragment 的 tabview android.view.WindowManager$BadTokenException :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9584349/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com