I got a superclass like this:
public class SuperClass extends Activity
and a child class
public class ChildClass extends SuperClass
The SuperClass contains a function to set a layout
public void setTabBar(String layout){
inflater.inflate(...., ...);
}
The only thing that different in my child class and superclass is the layout. So over to my question:
Is there anyway I can send a String to the superclass method setTabBar("name of the layout"); and then spesify the layout. I’v tried this:
public void setTabBar(String layout) {
int layoutFromString = Integer.parseInt("R.layout."+layout);
inflater.inflate(layoutFromString, null);
}
But this doesnt seems to work. Any ideas?
EDIT
As mentioned I tried this:
public void setTabBar(int id) {
inflater.inflate(id, null);
}
This will work for the SuperClass, but when I call the function from the child class like this:
public class TestClass extends SuperClass{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTabBar(R.layout.test);
}
}
LogCat only outputs this:
07-23 13:57:15.775: E/AndroidRuntime(7329): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
You’re trying to parse an integer from the string passed in to setTabBar. Reflection would be required to convert the variable name to the actual value, and I personally wouldn’t want to tread down that rabbit hole.
Why not just specify that the parameter passed in is an integer?
And, in the calling class: