android 中带有 onclicklistener 的警报对话框

alert dialog in android with onclicklistener

应用程序停止启动并出现该日志猫错误,我检查了我的代码很多次,但没有弄清楚问题是什么 我想这可能是我在布局上做错了,但没有错 提前致谢

我的 logcat 错误是:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.ahmed_samir.dialog, PID: 4229
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ahmed_samir.dialog/com.example.ahmed_samir.dialog.MainActivity}: android.content.res.Resources$NotFoundException: File  from xml type layout resource ID #0x1020009
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                  at android.app.ActivityThread.access0(ActivityThread.java:151)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:135)
                  at android.app.ActivityThread.main(ActivityThread.java:5254)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
               Caused by: android.content.res.Resources$NotFoundException: File  from xml type layout resource ID #0x1020009
                  at android.content.res.Resources.loadXmlResourceParser(Resources.java:2696)
                  at android.content.res.Resources.loadXmlResourceParser(Resources.java:2651)
                  at android.content.res.Resources.getLayout(Resources.java:1082)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:412)
                  at com.android.internal.app.AlertController.setupView(AlertController.java:479)
                  at com.android.internal.app.AlertController.installContent(AlertController.java:236)
                  at android.app.AlertDialog.onCreate(AlertDialog.java:356)
                  at android.app.Dialog.dispatchOnCreate(Dialog.java:373)
                  at android.app.Dialog.show(Dialog.java:274)
                  at com.example.ahmed_samir.dialog.MainActivity.showDialog(MainActivity.java:65)
                  at com.example.ahmed_samir.dialog.MainActivity.onCreate(MainActivity.java:42)
                  at android.app.Activity.performCreate(Activity.java:5990)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                  at android.app.ActivityThread.access0(ActivityThread.java:151) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:135) 
                  at android.app.ActivityThread.main(ActivityThread.java:5254) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:372) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
               Caused by: java.io.FileNotFoundException: 
                  at android.content.res.AssetManager.openXmlAssetNative(Native Method)
                  at android.content.res.AssetManager.openXmlBlockAsset(AssetManager.java:485)
                  at android.content.res.Resources.loadXmlResourceParser(Resources.java:2678)
                  at android.content.res.Resources.loadXmlResourceParser(Resources.java:2651) 
                  at android.content.res.Resources.getLayout(Resources.java:1082) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:412) 
                  at com.android.internal.app.AlertController.setupView(AlertController.java:479) 
                  at com.android.internal.app.AlertController.installContent(AlertController.java:236) 
                  at android.app.AlertDialog.onCreate(AlertDialog.java:356) 
                  at android.app.Dialog.dispatchOnCreate(Dialog.java:373) 
                  at android.app.Dialog.show(Dialog.java:274) 
                  at com.example.ahmed_samir.dialog.MainActivity.showDialog(MainActivity.java:65) 
                  at com.example.ahmed_samir.dialog.MainActivity.onCreate(MainActivity.java:42) 
                  at android.app.Activity.performCreate(Activity.java:5990) 
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) 
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                  at android.app.ActivityThread.access0(ActivityThread.java:151) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:135) 
                  at android.app.ActivityThread.main(ActivityThread.java:5254) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:372) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

我更新的代码:

public class MainActivity extends Activity {
//mobily
private Button button;

private AlertDialog myDialog;

private TextView result;

private SharedPreferences prefs;
private String prefName = "report";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final EditText input = new EditText(this);
    button = (Button) findViewById(R.id.button1);
    result = (TextView) findViewById(R.id.tv);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showDialog();
        }
    });

    showDialog();
}

public void showDialog() {
    if (myDialog == null) {
        myDialog = new AlertDialog.Builder(this)
                .setTitle("ID Number")
                .setMessage("Please enter your ID")
                .setView(input)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //your code
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .create();
    }
    myDialog.show();
}

}

如异常所述,您的警报仍附加到视图。 尝试关闭肯定按钮和否定按钮上的对话框。

您收到该错误是因为您将 EditText 输入设置为不同的 Dialog,因为每次单击按钮都会创建一个新的 Dialog,因此出现错误.

您的代码第一次运行时,您的输入被设置为 dialog1 的视图,但是当您第二次调用它时,您告诉 EditText 被设置为 dialog2 中的视图,但它已经有一个父对象是 dialog1。

改为设置 AlertDialog 的全局变量,然后仅当对话框尚不存在时才创建对话框。像这样:

private AlertDialog myDialog;

public void showDialog() {
        if (myDialog == null) {
            myDialog = new AlertDialog.Builder(this)
                    .setTitle("ID Number")
                    .setMessage("Please enter your ID")
                    .setView(input)
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //your code
                        }
                    })
                    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    })
                    .create();
        }
        myDialog.show();
    }

然后在您的 onClick 中调用 showDialog() 方法,而不是每次都重新创建对话框。

还要注意我是如何调用构建器中的每个方法的,这些命令是链式的,这是 Builder 的要点,可以轻松地一次调用多个方法,然后创建。

经过大量研究,我找到了答案,我想与 xml 文件分享我的知识和她的完整代码,希望它有用

public class NationalId extends Activity {

final Context context = this;
private Button button;
private TextView result;

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_national_id);

    // components from main.xml
    button = (Button) findViewById(R.id.button1);
    result = (TextView) findViewById(R.id.tv1);

    // add button listener
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            // get prompts.xml view
            LayoutInflater li = LayoutInflater.from(context);
            View promptsView = li.inflate(R.layout.prompts, null);

            android.app.AlertDialog.Builder alertDialogBuilder = new android.app.AlertDialog.Builder(
                    context);

            // set prompts.xml to alertdialog builder
            alertDialogBuilder.setView(promptsView);

            final EditText userInput = (EditText) promptsView
                    .findViewById(R.id.editTextDialogUserInput);

            // set dialog message
            alertDialogBuilder
                    .setCancelable(false)
                    .setPositiveButton("OK",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,int id) {
                                    // get user input and set it to result
                                    // edit text
                                    result.setText(userInput.getText());
                                }
                            })
                    .setNegativeButton("Cancel",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,int id) {
                                    dialog.cancel();
                                }
                            });

            // create alert dialog
            android.app.AlertDialog alertDialog = alertDialogBuilder.create();

            // show it
            alertDialog.show();

        }
    });
}
}

我的主要 xml 文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/tv1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_marginTop="47dp"
    android:ems="10"
    android:inputType="phone"
    android:hint="yourid"
    >

    <requestFocus />
</TextView>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tv1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="32dp"
    android:text="Edit"

    />

最后是我的提示 xml 布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="YOUR ID  : "
    android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
    android:id="@+id/editTextDialogUserInput"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="phone"
    >

    <requestFocus />

</EditText>

我最诚挚的问候..