findViewById return null using parameter of OnClick method of OnClickListener interface

android, java

Solution

The parameter `View v` references your button. That button view does not contain an element with `R.id.resultado`. Try `v.getRootView()`.

Problem

my code: ``` @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn = (Button) findViewById(R.id.btn_ok); btn.setOnClickListener(new Click()); } ``` my class witch implements OnClickListener but on method Click of "Click" class.. the parameter View v.. dont work. using method findViewById.. always return null :( any help? ``` public class Click implements View.OnClickListener { @Override public void onClick(View v) { TextView view = (TextView) v.findViewById(R.id.resultado); EditText textoEdit = (EditText) v.findViewById(R.id.campo_texto); String texto = textoEdit.getText().toString(); view.setText("Obrigado " + texto); } } ``` (sorry for my english)

Original source