2014-03-28 79 views
0

我試圖從對話框中獲取文本,但它不起作用。所有的代碼作品除了username = txtDialog.getText().toString();Android對話框EditText不賦值

我得到一個NullPointerException

下面是完整的代碼:

btn_next.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      dialog = new Dialog(ResActivity.this); 
      dialog.setContentView(R.layout.custom); 
      dialog.show(); 

      Button declineButton = (Button) dialog.findViewById(R.id.declineButton); 
      declineButton.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        txtDialog = (EditText)findViewById(R.id.textDialog); 
        username = txtDialog.getText().toString(); 
        Intent intent = new Intent(getApplicationContext(), EndActivity.class); 
        intent.putExtra(MESSAGE_NAME, username); 
        startActivity(intent); 
        dialog.dismiss(); 
       } 
      }); 

     } 
    }); 

有人能幫忙嗎?

+0

發佈錯誤日誌。 –

+0

edittext在對話框中? –

回答

2

我覺得你EdittextDialog 改變這一行

txtDialog = (EditText)findViewById(R.id.textDialog); 

txtDialog = (EditText)dialog.findViewById(R.id.textDialog); 
+1

是的,這是正確的,謝謝所有 – Yves

+0

@Yves很樂意幫助你.. –

2

試試這個

txtDialog = (EditText)dialog.findViewById(R.id.textDialog); 
+0

@Yves請接受答案,如果它解決了您的問題。 – Nand

0

您的編輯中的文本對話框即使用這樣

txtDialog = (EditText)dialog.findViewById(R.id.textDialog); 
0

我覺得你EditText裏面的對話框:

然後改變這一報關行

txtDialog = (EditText)findViewById(R.id.textDialog); 

txtDialog = (EditText)dialog.findViewById(R.id.textDialog); 
0

無效connectToHotSpot(){ AlertDialog.Builder alert = new AlertDialog.Builder(this);

 alert.setTitle("Inpaint Server"); 

     alert.setMessage("IP Address"); 

     final EditText input = new EditText(this); 
     alert.setView(input); 


     alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) { 

      String nickname=input.getText().toString(); 
      if(!nickname.equals("")) 
      { 
       Variables.Serversip=nickname; 
       ipaddress.setText("Server's IP "+nickname); 
       showAlertMessage("Message","Ok.Proceed"); 
      } 
      else 
      { 
       showAlertMessage("Oops","Specify the Server's IP Address"); 
      } 

     } 
     }); 

     alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) { 
      Toast.makeText(getApplicationContext(),"Must give me the IP B4 Inpainting",Toast.LENGTH_LONG).show(); 
     } 
     }); 

     alert.show(); 
    }