Android對話方塊裡面的輸入值擷取不到,null 指標異常

來源:互聯網
上載者:User

標籤:android對話方塊擷取不到值   android對話方塊獲值null 指標   android對話方塊的按鈕事件   

寫的一個Android對話方塊,點擊按鈕擷取EditText裡面的值,這裡一直報null 指標異常,研究了很長時間終於解決了。

異常如下:

我原來的代碼:

//更新對話方塊private void updateDialog(final String id){TableLayout updatemsg = (TableLayout)getLayoutInflater().inflate(R.layout.updatemsg, null);new AlertDialog.Builder(this).setTitle("更新該條資訊").setView(updatemsg).setPositiveButton("更新",new OnClickListener(){@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubString musicN = ((EditText)findViewById(R.id.musicN)).getText().toString();String singerN = ((EditText)findViewById(R.id.singerN)).getText().toString();updateData(db,id,musicN,singerN);}}).setNegativeButton("取消", new OnClickListener(){public void onClick(DialogInterface dialog,int which){}}).create().show();}

對話方塊中引入了另一個xml設定檔updatemsg.xml:

<?xml version="1.0" encoding="utf-8"?><TableLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <TableRow>        <TextView            android:layout_width="100dip"            android:layout_height="wrap_content"            android:text="音樂名"            android:textSize="30px"            android:textColor="#ffffff" />        <EditText            android:id="@+id/musicN"            android:layout_width="100dip"            android:layout_height="wrap_content" />    </TableRow>    <TableRow>        <TextView            android:layout_width="100dip"            android:layout_height="wrap_content"            android:text="歌手"            android:textSize="30px"            android:textColor="#ffffff" />        <EditText            android:id="@+id/singerN"            android:layout_width="100dip"            android:layout_height="wrap_content" />    </TableRow></TableLayout>,

問題解決

出錯原因就是這個引入的xml

在通過findViewById找EditText的時候,它是從原來的布局檔案裡面找的,而不是對話方塊的布局檔案,所以當然要報null 指標異常

怎樣讓才能找到對話方塊的布局檔案呢?

把那兩句改成;

String musicN = ((EditText)updatemsg.findViewById(R.id.musicN)).getText().toString();
    String singerN = ((EditText)updatemsg.findViewById(R.id.singerN)).getText().toString();

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.