You have not set you content view for your activity class MyOwnOnClickListener
. So method findViewById()
cannot find the view you are passing as argument.
I think the MyOwnOnClickListener
class shouldn't be an Activity
class, but just a listener. You can pass a reference to the TextView
to the listener to use it.
Or you can implement the listener in your MainActivity
class and suppress the MyOwnOnClickListener
class.
public class MainActivity extends Activity implements OnClickListener {
Button mybutton;
TextView tvView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
mybutton = (Button) findViewById(R.id.myactualbutton);
tvView = (TextView) findViewById(R.id.produceText1);
mybutton.setOnClickListener(this);
}
public void onClick(View v) {
addiTion(v);
}
public void addiTion(View v){
tvView.setText("fgdsgdfhgdfh");
}
}
EDIT: If you want to use a listener class, just keep your MainActivity
as it is, change only the call to the MyOwnOnClickListener
to pass the tvView
reference, and change the MyOwnOnClickListener
class like this:
public class MyOwnOnClickListener implements OnClickListener{
TextView id2;
// MainActivity caller;
public MyOwnOnClickListener(TextView id2) {
this.id2 = id2;
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.produceText1:
id2.setText("fgdsgdfhgdfh");
break;
// Other cases here...
default:
// Whatever you want to manage the situation...
}
}
}
It should work...