android - Logcat 首先显示 “The specified child already has a parent. You must call removeView() on the child' 的父级。”在我的第二个警报对话框中

标签 android view

正如我在标题中所说,我在用户交互时显示了 2 个警报对话框。我的菜单有 4 个选项:设置、积分、促销代码和退出,但是当我单击促销代码时,我收到问题中提到的错误。这是我的代码:

switch ((int) mMergeAdapter.getItemId(position)) {
                    case 0:
                        Intent intent2 = new Intent(Browser.this,
                                SettingsActivity.class);
                        startActivity(intent2);
                        break;
                    case 1:
                        AlertDialog.Builder builder = new AlertDialog.Builder(Browser.this);
                        builder.setTitle("Credits");
                        builder.setMessage("Code\n\nTraabefi\n\nDesign\n\nSamuel 'Samael' Chmura");
                        builder.setIcon(R.drawable.holo_dark_action_info);
                        builder.setPositiveButton("OK", null);
                        AlertDialog dialog = builder.create();
                        dialog.show();
                        TextView txt = (TextView)dialog.findViewById(android.R.id.message);
                        txt.setGravity(Gravity.CENTER);
                        break;
                    case 2:
                        AlertDialog.Builder alert = new AlertDialog.Builder(Browser.this);
                        alert.setTitle("Enter your promocode.");
                        final EditText input = (EditText) findViewById(android.R.id.edit);
                        alert.setView(input);
                        alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                        if (input.getText().toString()=="abcd"){
                                 isPremium = true;
                        }
                        else {
                                 isPremium = false;
                        }}});
                        alert.show();
                        break;
                    case 3:
                        finish();
                }

最佳答案

导致错误的行是:

 final EditText input = (EditText) findViewById(android.R.id.edit);
 alert.setView(input);

android.R.id.edit 是 fragment 的一部分,并且,正如错误所述,该 View 已经有一个父 View 。要解决您的问题,您可以定义 xml,使用 inflater 检索 View 对象并调用 setView,并提供此参数作为参数。

编辑:

在 res/layout 文件夹中创建一个 xml 文件。将其命名为 my_edit_text.xml,其中包含内容

<?xml version="1.0" encoding="utf-8"?>
   <EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myid"
    android:layout_width="match_parent"  
    android:layout_height="wrap_content" />

并替换

final EditText input = (EditText) findViewById(android.R.id.edit);
alert.setView(input);

 final View view = LayoutInflater.from(this).inflate(R.layout.my_edit_text, null); 
 alert.setView(view);
 final EditText input = (EditText) view.findViewById(R.id.myid);

关于android - Logcat 首先显示 “The specified child already has a parent. You must call removeView() on the child' 的父级。”在我的第二个警报对话框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26074928/

相关文章:

android - 在 XML 中更改 Spinner 下拉箭头的颜色

android - 如何在 Android.mk 构建过程结束时运行 shell 命令?

android - 使用 "?android:colorBackground"作为颜色会使我的应用程序在 Galaxy Note 3 (4.4.2) 上崩溃,但在 Nexus 上不会崩溃

sql-server - SQL Server View 中是否支持 ORDER BY?

java - GWT:处理 View 中的空请求工厂代理引用

c# - 为什么 EvaluateJavaScriptAsync 没有被执行?

java - Android Studio apk可以在苹果手机上运行吗?

android - 我怎样才能看到那个 View 变得可见

从线快速查看动画

python - Django rest 框架 api_view 与普通 View