In Android, I have a class A which extends class B which extends an ActionBarActivity. Class A sets the content view. For some reason, I'm unable to access the view elements from Class B. How can I access the view elements (ex: TextView)? When I attempt to use findViewById in class B, it is always null.
public class A extends B {
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
}
}
public class B extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView myTextView = (TextView) findViewById(R.id.myTextView);
// myTextView is null
}
}
you're inheriting things backwards. Class B doesn't ever setContentView.
I think this is what you want:
public class A extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
}
}
public class B extends A {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// A called setContentView(), so now we can find views by id.
TextView myTextView = (TextView) findViewById(R.id.myTextView);
// myTextView now is not null.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.