2012-03-01 41 views
0

IHAVE被使用的自定義對話框,並在其中一個EditText,我通過顯示錯誤消息驗證EditText當用戶點擊確定按鈕,無需進入他/這裏的名字,但是當我運行它,然後點擊ok按鈕有或沒有輸入名稱例外 發生這種情況的原因是什麼?請幫助我..定製對話框例外

這是我的代碼

final Dialog nameDialog =new Dialog(ProfileView.this); 
      nameDialog.setContentView(R.layout.namedialog); 
      nameDialog.setTitle("please enter your name"); 
      name =(EditText)nameDialog.findViewById(R.id.nameETDialog); 
      Button okButton = (Button) nameDialog.findViewById(R.id.ok); 

      okButton.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        //validate the name 
        if(name.getText().toString()== null)  
         name.setError(" your name is requiered"); 


        else 
        { 
         EditText nametext=(EditText)findViewById(R.id.name_text); 
         nametext.setText(name.getText().toString()); 
         Toast.makeText(getBaseContext(),"your name has been entered ", Toast.LENGTH_SHORT).show(); 
       } 
        } 

    }); 
      Button cancelButton = (Button) nameDialog.findViewById(R.id.cancel); 
      cancelButton.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        nameDialog.dismiss(); 
       } 


      }); 

      nameDialog.show(); 


} 
    }); 

} 

}

+2

發佈您的LOGCAT請 – Natali 2012-03-01 15:06:26

+1

Logcat在eclipse中找到:Window - > Show View - > Logcat – Bigflow 2012-03-01 15:13:49

回答

0

看代碼,不logcat的,我認爲你必須改變:

name =(EditText)nameDialog.findViewById(R.id.nameETDialog); 
Button okButton = (Button) nameDialog.findViewById(R.id.ok); 

隨着

name =(EditText) findViewById(R.id.nameETDialog); 
Button okButton = (Button) findViewById(R.id.ok); 

我假設按鈕okButton位於佈局稱爲:namedialog