我在下面的代码上遇到了一些问题,当我构建应用程序时,它在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;
}
}
MainActivity.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
我该如何解决这个问题? 感谢
@Edit 我修复了第一个错误,我忘了在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)
答案 0 :(得分:1)
您在内部类中调用外部类方法,您应该将MainActivity.getFragmentManager()更改为MainActivity.this.getFragmentManager()
答案 1 :(得分:0)
我解决了这个问题: 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;
}
}
MainActivity.java
private class HttpAsyncTask extends AsyncTask<String, Void, String> {
ProgressDialogFragment progressDialog = new ProgressDialogFragment();
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog.show(getFragmentManager(), "loading");
}
@Override
protected String doInBackground(String... urls) {
return UpdateData(MainActivity.this, urls[0], login);
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
progressDialog.dismiss();
startActivity(new Intent(getApplicationContext(), MainActivity.class));
finish();
Log.d("posted", result);
Toast.makeText(getBaseContext(), "Display Name Successful Changed!", Toast.LENGTH_LONG).show();
}
}
关于
的第一个问题Error:(216, 45) error: non-static method getFragmentManager() cannot be referenced from a static context
我通过@Gennadii Saprykin和handrenliang修改了以下代码:
progressDialog.show(MainActivity.getFragmentManager(),"loading");
到
progressDialog.show(MainActivity.this.getFragmentManager(),"loading");
第二个错误:
Error:(216, 27) 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)
由@handrenliang修正,如下: 使用android-v4 DialogFragment,但你的活动不是v4,你可以在ProgressDialogFragment中导入非v4 DialogFragment。
import android.support.v4.app.DialogFragment;
到
import android.app.DialogFragment;
和崩溃的最后一个错误,我只是将对话框声明从MainActivity Class移动到HttpAsyncTask类内部。
ProgressDialogFragment progressDialog = new ProgressDialogFragment();
谢谢@Gennadii Saprykin和@handrenliang 寻求帮助。