1

我的 Android 应用程序有一些奇怪的问题。我正在创建AlertDialog两个EditText控件和一些按钮(编辑现有对象)。

@Override
public void onClick(View arg0) {
    AlertDialog.Builder alert = new AlertDialog.Builder(mContext);

    final View dialogLayout = mInflater.inflate(R.layout.event_edit, null);
    alert.setView(dialogLayout);

    ((EditText)dialogLayout.findViewById(R.id.txtCompany)).setText(cEvent.getCompany());
    ((EditText)dialogLayout.findViewById(R.id.txtCity)).setText(cEvent.getCity());

    alert.setTitle(R.string.DialogEditEventTitle);
    alert.setPositiveButton(R.string.DialogEditEventDelete,
            new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // Delete action
        }
    });

    alert.setNeutralButton(R.string.DialogEditEventSave,
            new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // Save
        }
    });

    alert.setNegativeButton(R.string.DialogEditEventCancel,
            new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // Cancel action
        }
    });
    AlertDialog dialog = alert.show();
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

}

如果我尝试改变焦点或放置一些东西来编辑文本字段,那么我会得到异常:

12-03 14:32:25.663 E/AndroidRuntime( 5074): FATAL EXCEPTION: main
12-03 14:32:25.663 E/AndroidRuntime( 5074): java.lang.IllegalArgumentException: parameter must be a descendant of this view
12-03 14:32:25.663 E/AndroidRuntime( 5074):     at android.view.ViewGroup.offsetRectBetweenParentAndChild(ViewGroup.java:2627)
12-03 14:32:25.663 E/AndroidRuntime( 5074):     at android.view.ViewGroup.offsetDescendantRectToMyCoords(ViewGroup.java:2564)
12-03 14:32:25.663 E/AndroidRuntime( 5074):     at android.view.ViewRoot.scrollToRectOrFocus(ViewRoot.java:1508)
12-03 14:32:25.663 E/AndroidRuntime( 5074):     at android.view.ViewRoot.draw(ViewRoot.java:1249)
12-03 14:32:25.663 E/AndroidRuntime( 5074):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1163)
12-03 14:32:25.663 E/AndroidRuntime( 5074):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
12-03 14:32:25.663 E/AndroidRuntime( 5074):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-03 14:32:25.663 E/AndroidRuntime( 5074):     at android.os.Looper.loop(Looper.java:123)
12-03 14:32:25.663 E/AndroidRuntime( 5074):     at android.app.ActivityThread.main(ActivityThread.java:4627)
12-03 14:32:25.663 E/AndroidRuntime( 5074):     at java.lang.reflect.Method.invokeNative(Native Method)
12-03 14:32:25.663 E/AndroidRuntime( 5074):     at java.lang.reflect.Method.invoke(Method.java:521)
12-03 14:32:25.663 E/AndroidRuntime( 5074):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
12-03 14:32:25.663 E/AndroidRuntime( 5074):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
12-03 14:32:25.663 E/AndroidRuntime( 5074):     at dalvik.system.NativeStart.main(Native Method)

此异常的原因是什么,我该如何解决?

4

1 回答 1

1

Please try;

//first create, set soft input mode and then show//

AlertDialog dialog = alert.create();     
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
dialog.show();
于 2012-12-03T20:28:55.210 回答