我有一个片段,其中包含用户必须填写的一些字段。让我们举个例如电话号码。 当用户输入数字时,片段从MainActivity调用一个方法,并且在mainActivity中完成了一些操作,我们假设:
1-验证
2-验证
我使用BroadcarstReceiver从MainActivity回传到Fragment。因此,当验证完成后,我使用BroadcastReceiver向片段发送一个标志,向用户显示progressDialog。
此时,progressDialog应该显示,直到收到另一个验证标志。
问题是我遇到了一些关于RuntimeException的错误,应用程序崩溃的时候应该显示progressDialog。
public class ProfileFragment extends Fragment {
ProgressDialog progressDialog;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_profile, container, false);
...
progressDialog = new ProgressDialog(getContext());
progressDialog.setContentView(R.layout.custom_progressdialog);
...
return rootView;
}
protected BroadcastReceiver ActivityDataReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(ACTION_INTENT.equals(intent.getAction())) {
progressDialog.show();
}
if(ACTION_INTENT2.equals(intent.getAction())) {
progressDialog.dismiss();
}
}
}
这是logcat显示的内容:
FATAL EXCEPTION: main
Process: com.detgsm, PID: 21736
android.util.AndroidRuntimeException: requestFeature() must be called before adding content
at com.android.internal.policy.PhoneWindow.requestFeature(PhoneWindow.java:398)
at com.android.internal.app.AlertController.installContent(AlertController.java:249)
at android.app.AlertDialog.onCreate(AlertDialog.java:423)
at android.app.ProgressDialog.onCreate(ProgressDialog.java:249)
at android.app.Dialog.dispatchOnCreate(Dialog.java:578)
at android.app.Dialog.show(Dialog.java:314)
at com.detgsm.ProfileFragment$7.onReceive(ProfileFragment.java:271)
at android.support.v4.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:297)
at android.support.v4.content.LocalBroadcastManager.access$000(LocalBroadcastManager.java:46)
at android.support.v4.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:116)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
答案 0 :(得分:1)
答案 1 :(得分:0)
请更改为以下代码
public class ProfileFragment extends Fragment {
ProgressDialog progressDialog;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
progressDialog = new ProgressDialog(getContext());
progressDialog.setContentView(R.layout.custom_progressdialog);
}
protected BroadcastReceiver ActivityDataReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(ACTION_INTENT.equals(intent.getAction())) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
progressDialog.show();
}
});
}
if(ACTION_INTENT2.equals(intent.getAction())) {
progressDialog.dismiss();
}
}
}