2013-04-08 50 views
0

我試圖從對話框中獲取一些值。我有一個名爲Splash的活動,它顯示一個帶有窗體的對話框。它顯示形式福利,但我給我一個錯誤,當我嘗試獲得該值(空指針)。從事件獲取變量/控件

我有如下因素代碼:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_splash); 


    //datos objeto 
    nombre = (TextView) findViewById(R.id.tv_nombre); 
    padre = (TextView) findViewById(R.id.tv_nom_padre); 
    madre = (TextView) findViewById(R.id.tv_nom_madre); 
    rgSexo = (RadioGroup) findViewById(R.id.rg_sexo); 
    fecha_nac = (DatePicker) findViewById(R.id.dp_fecha_nac); 

    rb_m = (RadioButton) findViewById(R.id.rb_masc); 
    rb_f = (RadioButton) findViewById(R.id.rb_fem); 


    perfil = new PerfilCRUD(this); 
    perfil.open(); 

    //first time 
    if (perfil.primeraVez()){ 
     //muestro el popup 
     Log.v(TAG, "prim vez"); 




     Dialog dialog = new Dialog(this); 
     dialog.setContentView(R.layout.dialog_perfil); 
     dialog.setTitle("Datos del usuario");     
     dialog.show(); 

     Button botOk = (Button) dialog.findViewById(R.id.b_ok); 


     botOk.setOnClickListener(new OnClickListener(){ 
      public void onClick(View v) { 

       Log.v(TAG, "boton ok"); 
       Log.v(TAG, nombre.getText().toString()); 

      } 
     }); 

它給我的錯誤,當我試圖讓nombre.getText()的toString()值。它發生在所有的變量/控制值。它迫使關閉應用程序。

謝謝!

+0

是什麼logcat的說使用name? – 2013-04-08 16:55:20

+0

它說NullPointerException。 – alxsimo 2013-04-08 16:56:38

+0

我必須使用視圖v對象,但我不知道如何.. – alxsimo 2013-04-08 17:01:28

回答

0

這是因爲​​在您的原始layout。無論是呼叫在layoutinflate一個TextViewdialog或創建一個類級別的變量String name = "";然後在

if (perfil.primeraVez()){ 
    name = nombre.getText().toString(); 

那麼您可以在您的dialog

+0

他已經在使用'ombre.getText()。toString();'但仍然有問題。 – 2013-04-08 17:08:01

+0

@PareshMayani對,但是他改變了''對話框'中的'佈局',所以''對話框裏面沒有訪問'原始佈局'中的'Views',對嗎? – codeMagic 2013-04-08 17:09:55