久久久久久久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 在方向更改時(shí)返回空指

      Android: Why DialogFragment return nullpointer on orientation change(Android:為什么 DialogFragment 在方向更改時(shí)返回空指針)
    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 在方向更改時(shí)返回空指針的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

                問(wèn)題描述

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

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

                對(duì)話(huà)框:

                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) 放在調(diào)用 dialogFragment 的主片段中,然后將其放在 onCreate 方法的 DialogFragment 中,那么我會(huì)看到 getRetainInstance 返回 true 并且 getDialog 在方向更改過(guò)程中具有對(duì)象(否則為 NPE).在這種情況下,我也沒(méi)有 NPE,但是會(huì)出現(xiàn)以下奇怪的行為:創(chuàng)建并呈現(xiàn)對(duì)話(huà)框,重新創(chuàng)建對(duì)話(huà)框(方向更改)并關(guān)閉(為什么?),重新創(chuàng)建對(duì)話(huà)框(再次更改方向)并呈現(xiàn)(wtf?上次它被解除了)等等,即對(duì)話(huà)在一側(cè)被解除,但請(qǐng)記住它應(yīng)該在另一側(cè)呈現(xiàn).那是什么?

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

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

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

                相關(guān)文檔推薦

                Get user#39;s current location using GPS(使用 GPS 獲取用戶(hù)的當(dāng)前位置)
                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(如何檢測(cè)位置提供者?GPS 或網(wǎng)絡(luò)提供商)
                Get current location during app launch(在應(yīng)用啟動(dòng)期間獲取當(dāng)前位置)
                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影院在线午夜 | 精品视频一区二区三区 | 337p日本欧洲亚洲大胆精蜜臀 | 亚洲精品888 | 日韩五月天 | 国产一在线观看 | 在线中文视频 | 国产激情小视频 | 久久精品国产99国产精品 | 日韩精品视频一区二区三区 | 亚洲欧美日韩一区 | 久久成人免费视频 | 国产精品毛片av一区 | 成人在线视频免费看 | www.蜜桃av| 久久国产精品网 | 夜久久| 高清免费av | 又黑又粗又长的欧美一区 | 成人一区二区在线 | 日本一区二区三区精品视频 | 欧美精品二区 | 蜜桃av一区二区三区 | 久久高清| 狠狠干天天干 | 精品一区二区三区91 | 欧美日韩亚洲国产 | 伊人网站| av黄色在线 | 欧美精品成人 | 免费观看黄网站 | 欧美一区二区三区在线播放 | 色就是色欧美 | 雨宫琴音一区二区在线 | 一区二区三区四区视频 | 亚洲www啪成人一区二区麻豆 | 欧美小视频在线观看 | 欧美日韩久久 | 国产a区 |