我从SDK版本<8.我刚刚在安卓市场上发布了这个应用程序,在发布之前,我的测试手机是HTC Thunderbolt和Droid X。两者都没有这个问题。
我正通过Flurry收到这份错误报告。我无法直接测试,因为我没有带SDK<8,出于某种原因,我无法让我的模拟器启动比应用程序默认SDK设置更低的版本。
java.lang.IollegalArgumentException,android.app.Activity.createDialog:880-(Activity#onCreateDialog没有为id 1创建对话框)
下面是我实现的onCreateDialog(int id)。
@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;
}
以下是对话被驳回的原因。
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();
}
});
也有这个问题,并用以下方法解决了它:
-方法中不返回null对话框:protected Dialog onCreateDialog(int id)
在Activity.java的Android 2.1中,第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中查看,会有一个空对话框的检查,并且会返回。所以它在这里工作。
-不使用捆绑包(在API8中更改):
protected Dialog onCreateDialog(int id,Bundle Bundle);
用途:
protected Dialog onCreateDialog(int id);
-我还使用了以下检查(有一个带有自定义对话框的特殊情况):
if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.ECLAIR_MR1) {
return dialog;
} else {
return null;
}
也许这对某人有帮助。。。
啊,看看你的代码:
dialog = new CustomCalcDialog(this);
dialog.setTitle("Enter Shipping %");
activeTextView = shippingPercent;
dialog.show();
dialog = null;
break;
您将dialog
设置为null:这就是出现错误的原因。这样做:
dialog = new CustomCalcDialog(this);
dialog.setTitle("Enter Shipping %");
activeTextView = shippingPercent;
break;
我相信处理这个讨厌的异常的最简单方法就是抓住它:
@Override
public void showDialog(int dialogId) {
try {
super.showDialog(dialogId);
} catch (IllegalArgumentException e) { /* Log it if you wish*/ }
}
也就是说,我不认为你使用onCreateDialog()
作为API的意图(这就是你看到奇怪行为的原因),它肯定会在大多数时候返回对话框对象。
我最终不得不去掉dialog=null。我还必须将activeTextView从onCreateDialog(int id)切换到onPrepareDialog(int-id,Dialog Dialog)。
以下是更新后的代码。
@Override
protected Dialog onCreateDialog(int id) {
super.onCreateDialog(id);
Dialog dialog = null;
switch(id){
case 1:
dialog = new CustomCalcDialog(this);
dialog.setTitle("Enter Shipping %");
break;
case 2:
dialog = new CustomCalcDialog(this);
dialog.setTitle("Enter Tax Rate");
break;
case 3:
dialog = new CustomCalcDialog(this);
dialog.setTitle("Enter Commission %");
break;
case 4:
dialog = new CustomCalcDialog(this);
dialog.setTitle("Calculate Subtotal");
break;
case 5:
dialog = new CustomCalcDialog(this);
dialog.setTitle("Additional Shipping");
break;
case 6:
dialog = new BackgroundOptionsDialog(this);
dialog.setTitle("Choose Background:");
break;
}
return dialog;
}
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
super.onPrepareDialog(id, dialog);
switch(id){
case 1:
activeTextView = shippingPercent;
break;
case 2:
activeTextView = taxPercent;
break;
case 3:
activeTextView = commissionPercent;
break;
case 4:
activeTextView = productSubtotal;
break;
case 5:
activeTextView = addShipping;
break;
}
}
这是因为您从onCreateDialog(int id)
方法返回null(此方法已弃用)。
相反,请使用此选项。