久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

AlertDialog 中的 EditText 始終為空

EditText inside AlertDialog always null(AlertDialog 中的 EditText 始終為空)
本文介紹了AlertDialog 中的 EditText 始終為空的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時送ChatGPT賬號..

我已經(jīng)搜索了線程,但到目前為止我還沒有找到我要找的東西.我創(chuàng)建了一個顯示的自定義警報對話框,我?guī)缀蹩梢杂盟鋈魏问虑?它的自定義對話框由 3 個 TextViews 和 3 個 EditText 組成,但是每當(dāng)我需要獲取 EditText 時,我都會得到一個空組件.

I've search over the threads but so far I have not found what I'm looking for. I created a custom Alert Dialog that show up and I can do almost anything with it. It custom dialog is made of 3 TextViews and 3 EditText but whenever I need to get the EditText I get a null component.

來自我的 xml 文件

From my xml file

 <TextView android:layout_alignParentTop="true" 
    android:id="@+id/txtAccName" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Name"
    android:textSize="23dp"
    >

</TextView>
<EditText android:id="@+id/txtEditName" 
    android:layout_below="@+id/txtAccName" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="" 
    android:maxLength="20"
    >

我正在嘗試獲取 EditText 字段:

I'm trying to get the EditText field with:

    EditText txtAccName = (EditText) findViewById(R.id.txtEditName);
    Log.d("#############################", "txtAccName="+txtAccName.getText().toString());

但是這樣做時,第一行運(yùn)行良好,第二行導(dǎo)致崩潰,控件為空.這是我用來創(chuàng)建我需要的自定義 AlertDialog 的 Overwrite 方法.

But when doing this, the first line works well, the second home causes a crash and the control is null. This is the Overwrite method that I use to create the custom AlertDialog that I need.

    @Override
protected Dialog onCreateDialog(int id) {
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View layout = inflater.inflate(R.layout.accountdialog, (ViewGroup) findViewById(R.id.accDialog));

    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setNegativeButton(android.R.string.cancel,new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {

        }
        });
    builder.setPositiveButton(android.R.string.ok,
            new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                EditText txtAccName = (EditText) findViewById(R.id.txtEditName);
                //EditText txtAccCur = (EditText) findViewById(R.id.txtEditCur);
                //EditText txtAccCountry = (EditText) findViewById(R.id.txtEditCountry);
                //DBConn db = new DBConn(Accounts.this.getApplicationContext(), "msaverdb", null, 0);
                Log.d("#############################", "CALLING db.insertAccount");
                Log.d("#############################", "txtAccName="+txtAccName.getText().toString());
                //db.insertAccount(txtAccName.getText().toString(), txtAccCountry.getText().toString(), 
                //      txtAccCur.getText().toString());
                Accounts.this.removeDialog(ACC_DIALOG);
            }
            });
    builder.setNegativeButton(android.R.string.cancel,
            new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                Accounts.this.removeDialog(ACC_DIALOG);
            }
            });
    builder.setView(layout);
    AlertDialog ad = builder.create();
    return ad;
}

非常感謝任何幫助.

推薦答案

注意所有方法調(diào)用是如何在匿名內(nèi)部類中解析的.

Take note of how all of your method calls are being resolved within your anonymous inner classes.

findViewById 是一種存在于視圖和您的活動中的方法.您的活動上的此方法版本在活動窗口的視圖層次結(jié)構(gòu)中搜索視圖.視圖版本搜索該視圖實(shí)例和所有附加的子視圖.

findViewById is a method that exists on views and on your activity. The version of this method on your activity searches for a view within the activity window's view hierarchy. The version on views searches that view instance and all attached children.

您對有問題的代碼行的調(diào)用:

Your call on the problematic line of code:

EditText txtAccName = (EditText) findViewById(R.id.txtEditName);

正在解析為 Activity#findViewById.但是您的對話框布局并未附加到您的活動窗口,而是附加到對話框.您可以通過多種方式找到正確的視圖引用,但在您的情況下最簡單的可能是從您膨脹的布局的根目錄開始搜索:

is resolving to Activity#findViewById. But your dialog's layout is not attached to your activity window, it's attached to the dialog. You can find the correct view reference in several ways but the simplest in your case is probably to search from the root of the layout that you inflated:

EditText txtAccName = (EditText) layout.findViewById(R.id.txtEditName);

這篇關(guān)于AlertDialog 中的 EditText 始終為空的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Cut, copy, paste in android(在android中剪切、復(fù)制、粘貼)
android EditText blends into background(android EditText 融入背景)
Change Line Color of EditText - Android(更改 EditText 的線條顏色 - Android)
EditText showing numbers with 2 decimals at all times(EditText 始終顯示帶 2 位小數(shù)的數(shù)字)
Changing where cursor starts in an expanded EditText(更改光標(biāo)在展開的 EditText 中的開始位置)
EditText, adjustPan, ScrollView issue in android(android中的EditText,adjustPan,ScrollView問題)
主站蜘蛛池模板: 丝袜美腿一区二区三区 | 日本a v在线播放 | 国产精品视频一二三区 | 草草影院ccyy | 亚洲成av人片在线观看 | 黄网站在线观看 | 夜夜骑首页 | 在线男人天堂 | 免费污视频 | jizz18国产| 日韩精品免费视频 | 黄色网一级片 | 国产精品成人久久久久 | 精品日本久久久久久久久久 | 美女天天操 | 亚洲精品视频在线播放 | 欧美二区乱c黑人 | 日本一区二区高清视频 | 91高清免费| 国产精品一区二 | 国产丝袜一区二区三区免费视频 | 国产精品久久久久久久久免费桃花 | 久久久久久久久久久丰满 | 亚洲aⅴ精品| 中文字幕免费在线观看 | 日韩福利在线观看 | 男人的天堂一级片 | 国产欧美一区二区精品忘忧草 | 国产福利在线 | 国产成人精品一区二区三区视频 | 欧美二区在线 | 国产精品国产三级国产aⅴ原创 | 97人人干| 成人在线精品 | 99热欧美 | 在线播放国产一区二区三区 | 国产成人综合久久 | 成人av电影网 | 伦理一区二区 | 国产精品自产拍 | 黄色一级大片在线观看 |