2013-04-08 35 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