日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

安卓Dialog对话框多次显示而闪退的解决办法

發布時間:2024/6/30 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 安卓Dialog对话框多次显示而闪退的解决办法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

事情是這樣子的,我在一個活動中自定義了一個AlertDialog,通過一個按鈕點擊即可彈出,而后來出現的情況是,第一次點擊就沒問題,

正常跳出,而第二次就直接程序閃退,然后報The specified child already has a parent. You must call removeView的錯誤,

這是我的AlertDialog的代碼,和布局

final AlertDialog setTheOrder = new AlertDialog.Builder(Passenger.this).setView(R.layout.order_layout).setPositiveButton("確定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) { //以下都是這個Dialog布局中的一些控件getLocation("湘潭",startInput.getText().toString(),0);getLocation("湘潭",endInput.getText().toString(),1);startPlace = startInput.getText().toString();endPlace = endInput.getText().toString();startTime = getFormatTime(startTimeInput_h.getText().toString(),startTimeInput_m.getText().toString());endTime = getFormatTime(endTimeInput_h.getText().toString(),endTimeInput_m.getText().toString());supplyCar = getSupplyCar(canSupplyCar.getText().toString());}}).setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {}}).create();setTheOrder.show();

?

再來看這個錯誤,字面意思呢,就是說這個特定的child已經有了一個父布局,必須移除這個布局,才能繼續你的內容,經過一番查找,一個子類View,要使用必須依賴于父類View,如果要使用子類的View,必須先調用父類的removeView的方法才能保證子類的使用(不知道表述的對不對,錯了煩請大佬們指正)

好了,既然大概的思路有了,我們先得得到一個父類對象,我找到兩個解決辦法,

第一

通過活動的onCreate方法里面setContentView里面那個布局,調用

LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);LinearLayout parent = (LinearLayout) inflater.inflate(R.layout.order_layout, null);

這兩個方法得到父類對象,然后使用完這個Dialog之后,調用removeAllViews,在上面AlertDialog的兩個button里面的最后,調用這個方法

final AlertDialog setTheOrder = new AlertDialog.Builder(Passenger.this).setView(parent).setPositiveButton("確定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {getLocation("湘潭",startInput.getText().toString(),0);getLocation("湘潭",endInput.getText().toString(),1);startPlace = startInput.getText().toString();endPlace = endInput.getText().toString();startTime = getFormatTime(startTimeInput_h.getText().toString(),startTimeInput_m.getText().toString());endTime = getFormatTime(endTimeInput_h.getText().toString(),endTimeInput_m.getText().toString());supplyCar = getSupplyCar(canSupplyCar.getText().toString());parent.removeAllViews();}}).setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {parent.removeAllViews();}}).create();setTheOrder.show();

我這里是規定了只能點這兩個按鈕才能結束這個Dialog,其實如果設置了點擊Dialog之外的屏幕區域也可以退出Dialog的話,在相應的方法里面也要調用

?

第二,報這個錯誤的原因是因為本來的視圖有一個View,而你重新顯示的時候,相當于重新給他添加一個View(因為Dialog生成的時候執行了setView這個函數),這時就會報錯,所以,粗暴一點的解決辦法就是在緊接著Dialog前面將View重新加載,相當于每次都重新生成一個新的對象,這樣便不會報錯

轉載于:https://www.cnblogs.com/Yintianhao/p/9461979.html

總結

以上是生活随笔為你收集整理的安卓Dialog对话框多次显示而闪退的解决办法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。