我得到java.lang.IllegalStateException:活動遭到破壞。當我嘗試顯示一個活動的對話框片段。錯誤DialogFragment show()方法
我已經在計算器見過許多解決方案,但那些didnt解決我的問題在下面的行發生錯誤
dialogfragment.show(英尺,「對話」);
代碼如下。
class MyDialog extends DialogFragment {
int mNum = 0;
static MyDialog newInstance(){
return new MyDialog();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//dialog view
Log.i("MyDialog", "onCreateview");
View v = inflater.inflate(R.layout.dialog, container, false);
TextView v1 = (TextView) v.findViewById(R.id.text);
v1.setText("dialog shown");
return v;
}
}
public class DialogActivity extends FragmentActivity{
DialogFragment dialogfragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
void show(){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment f = getSupportFragmentManager().findFragmentByTag("dialog");
if(f != null){
ft.remove(f);
}
dialogfragment = MyDialog.newInstance();
dialogfragment.setCancelable(false);
dialogfragment.show(ft, "dialog");
dialogfragment.setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Holo_Light_Dialog);
}
}
我打電話從Activity類此方法作爲
DialogActivity dl = new DialogActivity();
dl.show();
錯誤日誌
07-08 14:54:09.565:W/System.err的(10390): java.lang.IllegalStateException:活動已被破壞 07-08 14:54:09.565:W/System.err的(10390):在android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1314) 07 -08 14:54:09.565:W/System.err(10390):at a ndroid.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:541) 07-08 14:54:09.565:W/System.err的(10390):在android.support.v4.app.BackStackRecord.commit( BackStackRecord.java:525) 07-08 14:54:09.565:W/System.err的(10390):在android.support.v4.app.DialogFragment.show(DialogFragment.java:138)
我錯過了code.now的某些部分添加它
編輯:
有沒有任何其它辦法從調用show()方法中DialogActivity 210另一個Activity類?
你可以檢查它是否有幫助http://developer.android.com/guide/topics/ui/controls/pickers.html。因爲黑帶建議你需要開始一項活動。活動有一個生命週期。 'DialogActivity dl = new DialogActivity()'這不會啓動一個活動。 – Raghunandan