久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

      <bdo id='iPq1d'></bdo><ul id='iPq1d'></ul>

    <i id='iPq1d'><tr id='iPq1d'><dt id='iPq1d'><q id='iPq1d'><span id='iPq1d'><b id='iPq1d'><form id='iPq1d'><ins id='iPq1d'></ins><ul id='iPq1d'></ul><sub id='iPq1d'></sub></form><legend id='iPq1d'></legend><bdo id='iPq1d'><pre id='iPq1d'><center id='iPq1d'></center></pre></bdo></b><th id='iPq1d'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='iPq1d'><tfoot id='iPq1d'></tfoot><dl id='iPq1d'><fieldset id='iPq1d'></fieldset></dl></div>
      <tfoot id='iPq1d'></tfoot>

    1. <legend id='iPq1d'><style id='iPq1d'><dir id='iPq1d'><q id='iPq1d'></q></dir></style></legend>
    2. <small id='iPq1d'></small><noframes id='iPq1d'>

      Android:為什么 DialogFragment 在方向更改時返回空指

      Android: Why DialogFragment return nullpointer on orientation change(Android:為什么 DialogFragment 在方向更改時返回空指針)
    3. <small id='vahxV'></small><noframes id='vahxV'>

        <tbody id='vahxV'></tbody>

      1. <legend id='vahxV'><style id='vahxV'><dir id='vahxV'><q id='vahxV'></q></dir></style></legend>

        <tfoot id='vahxV'></tfoot>
          • <bdo id='vahxV'></bdo><ul id='vahxV'></ul>
            <i id='vahxV'><tr id='vahxV'><dt id='vahxV'><q id='vahxV'><span id='vahxV'><b id='vahxV'><form id='vahxV'><ins id='vahxV'></ins><ul id='vahxV'></ul><sub id='vahxV'></sub></form><legend id='vahxV'></legend><bdo id='vahxV'><pre id='vahxV'><center id='vahxV'></center></pre></bdo></b><th id='vahxV'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='vahxV'><tfoot id='vahxV'></tfoot><dl id='vahxV'><fieldset id='vahxV'></fieldset></dl></div>
              1. 本文介紹了Android:為什么 DialogFragment 在方向更改時返回空指針的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                在更改方向后,我遇到了正確對話框片段解散的問題.我猜這是由于舊的上下文,因為在重新創(chuàng)建該活動之后,創(chuàng)建了一個新的活動和相應的上下文.我知道我可以在 onSaveInstance 中設置一些變量來保存對話框狀態(tài)并在必要時重新創(chuàng)建對話框,或者只是將一個屬性放在清單方向"中.但是,也許有更好的方法可以做到不在清單中硬編碼它而不是手動保存在 onSaveIntance 中?我也嘗試在主片段和對話框片段中使用 setRetainInstance 但它對我沒有幫助.

                I have a problem with correct dialog fragment dismissing after that orientation was changed. I guess that's due to old context because after that activity was recreated there was created a new activity and corresponding context. I know that I can set some variable in onSaveInstance to save dialog status and recreate dialog if it`s necessary, or just put one attribute in manifest "orientation". But, maybe, there is something better way to do it to not hardcode it in manifest and not save in onSaveIntance manually? I also tried to use setRetainInstance in both main fragment and dialog fragment but it doesn't help me.

                片段:

                public class MainFragment extends Fragment implements ServiceExecutorListener, OnClickListener, DialogClickListener {
                
                private static final String TAG = MainFragment.class.getName();
                
                private TextView serviceStatus;
                Intent intent;
                Boolean bound = false;
                ServiceConnection sConn;
                RESTService service;
                ProgressDialogFragment pd;
                private Button btnSend, btnCheck;
                
                @Override
                public void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    Log.d(TAG, "onCreate");
                    setRetainInstance(true);
                    intent = new Intent(getActivity(), RESTService.class);
                    getActivity().startService(intent);
                    sConn = new ServiceConnection() {
                
                        @Override
                        public void onServiceConnected(ComponentName name, IBinder binder) {
                            Log.d(TAG, "MainFragment onServiceConnected");
                            service = ((RESTService.MyBinder) binder).getService();
                            service.registerListener(MainFragment.this);
                            if (service.tasksAreDone())
                                serviceStatus.setText(service.getResult());
                            bound = true;
                        }
                
                        public void onServiceDisconnected(ComponentName name) {
                            Log.d(TAG, "MainFragment onServiceDisconnected");
                            bound = false;
                        }
                
                    };
                }
                
                @Override
                public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                    View rootView = inflater.inflate(R.layout.main_fragment, container, false);
                    serviceStatus = (TextView) rootView.findViewById(R.id.tvServiceStatusValue);
                    btnSend = (Button) rootView.findViewById(R.id.btnSend);
                    btnCheck = (Button) rootView.findViewById(R.id.btnCheck);
                
                    btnSend.setOnClickListener(this);
                    btnCheck.setOnClickListener(this);
                    return rootView;
                }
                
                @Override
                public void onClick(View v) {
                    switch (v.getId()) {
                        case R.id.btnSend:
                            pd = new ProgressDialogFragment();
                            pd.newInstance(null);
                            pd.setDialogClickListener(this);
                            pd.show(getActivity().getSupportFragmentManager(), "ProgressDialog");
                            service.run(RESTService.REQUEST, 7);
                            service.run(RESTService.REQUEST, 2);
                            service.run(RESTService.REQUEST, 4);
                            break;
                        case R.id.btnCheck:
                            if (service != null)
                                serviceStatus.setText(String.valueOf(service.tasksAreDone()) + service.getTasksCount());
                            break;
                    }
                }
                
                @Override
                public void onStart() {
                    super.onStart();
                    Log.d(TAG, "onStart: Bind service");
                    getActivity().bindService(intent, sConn, 0);
                }
                
                @Override
                public void onPause() {
                    super.onPause();
                    Log.d(TAG, "onPause: Unbind service");
                    if (!bound)
                        return;
                    getActivity().unbindService(sConn);
                    service.unregisterListener();
                    bound = false;
                }
                
                @Override
                public void onComplete(int taskID, int action, String result) {
                    Log.d(TAG, "Task #" + taskID + ", action = " + action + " Completed");
                    pd.dismiss();
                    serviceStatus.setText(result);
                }
                
                @Override
                public void onDialogClick(int action) {
                    switch (action) {
                        case Dialog.BUTTON_POSITIVE:
                            Log.d(TAG, "POSITIVE BUTTON");
                            break;
                        case Dialog.BUTTON_NEGATIVE:
                            Log.d(TAG, "NEGATIVE BUTTON");
                            service.removeTasks();
                            break;
                        case Dialog.BUTTON_NEUTRAL:
                            Log.d(TAG, "NEUTRAL BUTTON");
                            break;
                    }
                }
                
                @Override
                public void onDestroy() {
                    super.onDestroy();
                    Log.d(TAG, "onDestroy");
                }
                }
                

                對話框:

                public class ProgressDialogFragment extends DialogFragment implements OnClickListener {
                
                final String TAG = ProgressDialogFragment.class.getName();
                private DialogClickListener listener;
                
                public ProgressDialogFragment newInstance(Bundle args) {
                    ProgressDialogFragment pdf = new ProgressDialogFragment();
                    pdf.setArguments(args);
                    return pdf;
                }
                
                public void setDialogClickListener(DialogClickListener listener) {
                    this.listener = listener;
                }
                
                @Override
                public void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setRetainInstance(true);
                    Log.d(TAG, "onCreate");
                }
                
                public Dialog onCreateDialog(Bundle savedInstanceState) {
                    AlertDialog.Builder adb = new AlertDialog.Builder(getActivity())
                            .setTitle("Title!")
                            .setPositiveButton(R.string.yes, this)
                            .setNegativeButton(R.string.no, this)
                            .setNeutralButton(R.string.maybe, this)
                            .setCancelable(false)
                            .setMessage(R.string.message_text)
                            .setOnKeyListener(new OnKeyListener() {
                                @Override
                                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                                    return true;
                                }
                            });
                    return adb.create();
                }
                
                public void onClick(DialogInterface dialog, int which) {
                    if (listener != null)
                        listener.onDialogClick(which);
                }
                
                public void onDismiss(DialogInterface dialog) {
                    Log.d(TAG, "Dialog: onDismiss, dialog = " + getDialog() + ", retainInstance = " + getRetainInstance());
                    // Fix to avoid simple dialog dismiss in orientation change
                    if ((getDialog() != null) && getRetainInstance())  
                        getDialog().setDismissMessage(null);
                
                }
                
                public void onCancel(DialogInterface dialog) {
                    super.onCancel(dialog);
                    Log.d(TAG, "Dialog: onCancel");
                }
                

                LOgCat:

                04-29 06:17:17.860: E/AndroidRuntime(4202): FATAL EXCEPTION: main
                04-29 06:17:17.860: E/AndroidRuntime(4202): java.lang.NullPointerException
                04-29 06:17:17.860: E/AndroidRuntime(4202):     at android.support.v4.app.DialogFragment.dismissInternal(DialogFragment.java:184)
                04-29 06:17:17.860: E/AndroidRuntime(4202):     at android.support.v4.app.DialogFragment.dismiss(DialogFragment.java:155)
                04-29 06:17:17.860: E/AndroidRuntime(4202):     at com.example.restservice.fragments.MainFragment.onComplete(MainFragment.java:108)
                04-29 06:17:17.860: E/AndroidRuntime(4202):     at com.example.restservice.service.RESTService$1.run(RESTService.java:79)
                04-29 06:17:17.860: E/AndroidRuntime(4202):     at android.os.Handler.handleCallback(Handler.java:605)
                04-29 06:17:17.860: E/AndroidRuntime(4202):     at android.os.Handler.dispatchMessage(Handler.java:92)
                04-29 06:17:17.860: E/AndroidRuntime(4202):     at android.os.Looper.loop(Looper.java:137)
                04-29 06:17:17.860: E/AndroidRuntime(4202):     at android.app.ActivityThread.main(ActivityThread.java:4514)
                04-29 06:17:17.860: E/AndroidRuntime(4202):     at java.lang.reflect.Method.invokeNative(Native Method)
                04-29 06:17:17.860: E/AndroidRuntime(4202):     at java.lang.reflect.Method.invoke(Method.java:511)
                04-29 06:17:17.860: E/AndroidRuntime(4202):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
                04-29 06:17:17.860: E/AndroidRuntime(4202):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
                04-29 06:17:17.860: E/AndroidRuntime(4202):     at dalvik.system.NativeStart.main(Native Method)
                

                如果我將 setRetainInstance(true) 放在調用 dialogFragment 的主片段中,然后將其放在 onCreate 方法的 DialogFragment 中,那么我會看到 getRetainInstance 返回 true 并且 getDialog 在方向更改過程中具有對象(否則為 NPE).在這種情況下,我也沒有 NPE,但是會出現以下奇怪的行為:創(chuàng)建并呈現對話框,重新創(chuàng)建對話框(方向更改)并關閉(為什么?),重新創(chuàng)建對話框(再次更改方向)并呈現(wtf?上次它被解除了)等等,即對話在一側被解除,但請記住它應該在另一側呈現.那是什么?

                If I put setRetainInstance(true) in main fragment which calls dialogFragment and in DialogFragment in onCreate method only then I see that getRetainInstance return true and getDialog has object in the process of orientation change(otherwise NPE). In this case I also don`t have NPE BUT there will be following strange behaviour: dialog created and presented, dialog recreated(orientation change) and dismissed(why?), dialog recreated(again orientation changed) and presented(wtf? last time it was dismissed) and so on i.e dialog dismissed on one side but remember that on another side it should be presented. What is that?

                推薦答案

                onCreateDialog(Bundle savedInstanceState) 中移除 setRetainInstance(true); 并保留在 onCreate(捆綁 savedInstance) 如下:

                Remove the setRetainInstance(true); from onCreateDialog(Bundle savedInstanceState) and keep it in onCreate(Bundle savedInstance) as follows :

                @Override
                public void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setRetainInstance(true);
                }
                

                這篇關于Android:為什么 DialogFragment 在方向更改時返回空指針的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                相關文檔推薦

                Get user#39;s current location using GPS(使用 GPS 獲取用戶的當前位置)
                IllegalArgumentException thrown by requestLocationUpdate()(requestLocationUpdate() 拋出的 IllegalArgumentException)
                How reliable is LocationManager#39;s getLastKnownLocation and how often is it updated?(LocationManager 的 getLastKnownLocation 有多可靠,多久更新一次?)
                How to detect Location Provider ? GPS or Network Provider(如何檢測位置提供者?GPS 或網絡提供商)
                Get current location during app launch(在應用啟動期間獲取當前位置)
                locationManager.getLastKnownLocation() return null(locationManager.getLastKnownLocation() 返回 null)
                  <bdo id='v9kNZ'></bdo><ul id='v9kNZ'></ul>
                  <legend id='v9kNZ'><style id='v9kNZ'><dir id='v9kNZ'><q id='v9kNZ'></q></dir></style></legend>
                      <tbody id='v9kNZ'></tbody>
                    <tfoot id='v9kNZ'></tfoot>

                      • <i id='v9kNZ'><tr id='v9kNZ'><dt id='v9kNZ'><q id='v9kNZ'><span id='v9kNZ'><b id='v9kNZ'><form id='v9kNZ'><ins id='v9kNZ'></ins><ul id='v9kNZ'></ul><sub id='v9kNZ'></sub></form><legend id='v9kNZ'></legend><bdo id='v9kNZ'><pre id='v9kNZ'><center id='v9kNZ'></center></pre></bdo></b><th id='v9kNZ'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='v9kNZ'><tfoot id='v9kNZ'></tfoot><dl id='v9kNZ'><fieldset id='v9kNZ'></fieldset></dl></div>

                          <small id='v9kNZ'></small><noframes id='v9kNZ'>

                          主站蜘蛛池模板: 成人片免费看 | 97日韩精品 | av手机在线 | 欧洲成人 | 日日躁狠狠躁aaaaxxxx | 国产精品一区二区欧美黑人喷潮水 | 国产在线观看不卡一区二区三区 | 色婷婷一区二区三区四区 | 拍戏被cao翻了h承欢 | 国产一级视频免费播放 | 日韩一区二区三区在线看 | 中文字幕在线观看国产 | 国产精品一区在线观看你懂的 | 天天操天天天干 | 色呦呦在线 | 亚洲自拍偷拍av | 亚洲综合久久久 | 成年人黄色小视频 | 韩日一区 | 欧美色综合一区二区三区 | 欧美久久久 | 91原创视频| 免费的av网站 | 欧美日韩在线免费观看 | 精品久草 | 欧美特级黄色 | 天天躁日日躁狠狠躁白人 | 国产www.| 午夜丰满少妇一级毛片 | 亚洲一区 | 国产一区二区在线免费播放 | www成人免费 | 国产一级视频免费播放 | 亚洲欧美中文字幕在线观看 | 求毛片 | 国产三级精品视频 | 日本精品一区二区三区在线观看视频 | 国产91丝袜在线18 | 国产精品久久久久久久久久久新郎 | 欧美日韩在线视频观看 | 中文字幕精品视频 |