問題描述
我從 SDK 版本的手機收到以下錯誤消息 <8. 我剛剛在安卓市場上發(fā)布了這個應(yīng)用程序,在發(fā)布之前我的測試手機是 HTC Thunderbolt 和 Droid X.根本沒有這個問題.
I'm getting the below error message from phones that are SDK version < 8. I just released this app on the android market and prior to release my test phones were a HTC Thunderbolt and the Droid X. Neither had this problem at all.
我通過 Flurry 收到此錯誤報告.我無法直接對此進行測試,因為我沒有帶有 SDK < 的手機8 并且由于某種原因,我無法讓我的模擬器啟動低于為應(yīng)用程序設(shè)置的默認(rèn) SDK 的版本.
I'm getting this error report through Flurry. I'm not able to test this directly because I don't have a phone with SDK < 8 and for some reason I can't get my emulator to start a lower version than the default SDK set for an app.
java.lang.IllegalArgumentException, android.app.Activity.createDialog:880 -(Activity#onCreateDialog 沒有為 id 1 創(chuàng)建對話框)
java.lang.IllegalArgumentException, android.app.Activity.createDialog:880 - (Activity#onCreateDialog did not create a dialog for id 1)
下面是我實現(xiàn)的 onCreateDialog(int id).
Below is the onCreateDialog(int id) that i've implemented.
@Override
protected Dialog onCreateDialog(int id) {
super.onCreateDialog(id);
Dialog dialog = null;
switch(id){
case 1:
dialog = new CustomCalcDialog(this);
dialog.setTitle("Enter Shipping %");
activeTextView = shippingPercent;
dialog.show();
dialog = null;
break;
case 2:
dialog = new CustomCalcDialog(this);
dialog.setTitle("Enter Tax Rate");
activeTextView = taxPercent;
dialog.show();
dialog = null;
break;
case 3:
dialog = new CustomCalcDialog(this);
dialog.setTitle("Enter Commission %");
activeTextView = commissionPercent;
dialog.show();
dialog = null;
break;
case 4:
dialog = new CustomCalcDialog(this);
dialog.setTitle("Calculate Subtotal");
activeTextView = productSubtotal;
dialog.show();
dialog = null;
break;
case 5:
dialog = new CustomCalcDialog(this);
dialog.setTitle("Additional Shipping");
activeTextView = addShipping;
dialog.show();
dialog = null;
break;
case 6:
dialog = new BackgroundOptionsDialog(this);
dialog.setTitle("Choose Background:");
dialog.show();
dialog = null;
break;
default:
dialog = null;
}
return dialog;
}
下面是關(guān)閉對話框的方式.
And below is how the Dialog is being dismissed.
private void registerListeners () {
enterTotal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
calcLogic(EQUALS);
}catch(Exception ex){}
operatorChange = DONT_CHANGE;
activeTextView.setText(calcDialogDisplay.getText().toString());
try {
if ((Float.parseFloat(calcDialogDisplay.getText().toString())) < 0) {}
}catch(Exception ex) {
activeTextView.setText("0");
}
mathCalculations();
CustomCalcDialog.this.dismiss();
}
});
推薦答案
也遇到了這個問題,用下面的方法解決了:
Had the problem also and solved it with the following:
-不要在方法中返回空對話框:protected Dialog onCreateDialog(int id)
-Don't return a null Dialog in method: protected Dialog onCreateDialog(int id)
在 Android 2.1 中的 Activity.java 中,第 871 行引發(fā)了錯誤.
In Android 2.1 in Activity.java the error was raised on line 871.
private Dialog createDialog(Integer dialogId, Bundle state) {
869 final Dialog dialog = onCreateDialog(dialogId);
870 if (dialog == null) {
871 throw new IllegalArgumentException("Activity#onCreateDialog did "
872 + "not create a dialog for id " + dialogId);
873 }
874 dialog.dispatchOnCreate(state);
875 return dialog;
876 }
如果您在以后的 Android 中查看,則會檢查空 Dialog,并且它會返回并處理.所以在這里它起作用了.
If you look in later Android there is a check for a null Dialog and it's handeld with a return. So here it works.
-不要使用 Bundle(在 API8 中更改):
-Don't use Bundle (changed in API8):
受保護的對話框 onCreateDialog(int id, Bundle bundle);
protected Dialog onCreateDialog(int id, Bundle bundle);
使用:
受保護的對話框 onCreateDialog(int id);
protected Dialog onCreateDialog(int id);
-我還使用了以下檢查(有一個帶有自定義對話框的特殊情況):
-I also used the following check (had a special case with a custom dialog):
if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.ECLAIR_MR1) {
return dialog;
} else {
return null;
}
也許它可以幫助某人......
Maybe it helps someone...
這篇關(guān)于Android - java.lang.IllegalArgumentException 在 2.1 和低 android 上創(chuàng)建對話框時出錯的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!