子活动继承父活动
我有一个父 Activity 持有一个 ActionBar 对象:
import android.os.Bundle;
import android.support.v4.app.ActionBar;
import android.support.v4.app.FragmentActivity;
public class ParentActivity extends FragmentActivity{
private ActionBar actionBar; //Action bar
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
actionBar = getSupportActionBar();
}
public void setActionBar(String title){
actionBar.setTitle(title);
}
}
然后,我的子 Activity 继承父 Activity,并尝试在父 Activity 中设置 ActionBar:
public class ChildActivity extends ParentActivity {
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
//try to set action bar title, but get error
setActionBar(getString(R.string.myname));
...
}
}
但我收到错误:
android.app.SuperNotCalledException
at ...ParentActivity.setActionBar(..)
然后,我更改为使用 super.setActionBar(...);
我收到错误: NullPointerException setActionBar
为什么?
I have a parent Activity holds an ActionBar
object:
import android.os.Bundle;
import android.support.v4.app.ActionBar;
import android.support.v4.app.FragmentActivity;
public class ParentActivity extends FragmentActivity{
private ActionBar actionBar; //Action bar
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
actionBar = getSupportActionBar();
}
public void setActionBar(String title){
actionBar.setTitle(title);
}
}
Then, my child Activity inherits the parent Activity, and try to set the ActionBar
in parent:
public class ChildActivity extends ParentActivity {
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
//try to set action bar title, but get error
setActionBar(getString(R.string.myname));
...
}
}
But I got error:
android.app.SuperNotCalledException
at ...ParentActivity.setActionBar(..)
Then, I changed to use super.setActionBar(...);
I got error: NullPointerException setActionBar
Why??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尝试使用空对象的值,因此它显示空指针异常。为
ActionBar
对象提供new
运算符的一些内存。You're trying to use the value of an object which is null, so it shows a null pointer exception. Give the
ActionBar
object some memory the thenew
operator.