gpt4 book ai didi

java - android: 如何在 MainActivity 中显示 ProgressDialogFragment

转载 作者:行者123 更新时间:2023-11-30 01:58:51 25 4
gpt4 key购买 nike

我在下面的代码中遇到了一些问题,当我构建应用程序时,它在 MainActivity 端显示错误。下面是代码:ProgressDialogFragment.java

public class ProgressDialogFragment extends DialogFragment
{

public static ProgressDialogFragment newInstance() {
ProgressDialogFragment frag = new ProgressDialogFragment ();
return frag;
}

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setCancelable(false);
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
ProgressDialog dialog = new ProgressDialog(getActivity(), getTheme());
dialog.setTitle(getString(R.string.updatename_loading_title));
dialog.setMessage(getString(R.string.updatename_loading_message));
dialog.setIndeterminate(true);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
return dialog;
}

}

主 Activity .java

public void doPositiveClick(DialogFragment dialog) {
Dialog dialogView = dialog.getDialog();
EditText editName = (EditText) dialogView.findViewById(R.id.editName);
String updateName = editName.getText().toString().trim();
login.setName(updateName);
new HttpAsyncTask().execute(uNameUrl);
dialogView.dismiss();
}

private class HttpAsyncTask extends AsyncTask<String, Void, String> {

@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog.show(MainActivity.getFragmentManager(),"loading");
}

@Override
protected String doInBackground(String... urls) {

// Do Action Here
}
}

我想在提交后显示加载,但在下面一行出现错误:

progressDialog.show(MainActivity.getFragmentManager(),"loading"); 

在我构建应用程序后显示以下错误消息。

Error:(216, 45) error: non-static method getFragmentManager() cannot be referenced from a static context

我该如何解决这个问题?谢谢

@编辑我修复了第一个错误,我只是忘了在 ProgressDialogFragment.java 上添加它

@Override
public void show(FragmentManager manager, String tag) {
if (manager.findFragmentByTag(tag) == null) {
super.show(manager, tag);
}
}

防止旋转手机时出现重复对话框。现在出现第二个错误

Error:(40, 5) error: method does not override or implement a method from a supertype
Error:(43, 18) error: no suitable method found for show(android.app.FragmentManager,String)
method DialogFragment.show(android.support.v4.app.FragmentManager,String) is not applicable
(argument mismatch; android.app.FragmentManager cannot be converted to android.support.v4.app.FragmentManager)
method DialogFragment.show(FragmentTransaction,String) is not applicable
(argument mismatch; android.app.FragmentManager cannot be converted to FragmentTransaction)

最佳答案

你是在内部类中调用外部类方法,你应该把MainActivity.getFragmentManager()改成MainActivity.this.getFragmentManager()

关于java - android: 如何在 MainActivity 中显示 ProgressDialogFragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31798700/

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