It seems that my implementation of toString() method in an Activity is never called no matter what I do. I always get result from java.lang.Object (like Ljava.lang.Object;@4059d6a8) instead of “I was called!”. I’ve read a bunch of articles and scanned through a lot of code and I can’t figure out what I’m doing wrong. Could someone please explain to me how do I override the method? This is my code:
public class OverrideTestActivity extends Activity {
private int number = 27;
private String[] items = { "MyFirstItem", "MySecondItem" };
public Object[] stuff = { number, items };
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.test);
OverrideTest();
}
public void OverrideTest() {
TextView result = (TextView) findViewById(R.id.textView1);
result.setText(stuff.toString());
}
@Override
public String toString() {
return "I was called!";
}
}
Thanks a lot in advance!
In this case, the
toString()is called only on objects of typeOverrideTestActivity.Instead you are calling toString() on an array, which doesn’t have such an implementation.
If you did something like this
You would find that it outputs a format that represents the contents of the List. The format will have been determined by the
toString()method on the List implementation.