未继承父类onCreate()方法。这个我在练习中遇到过,当时是看的这篇文章就摘抄下来了,忘了是谁的了,无法标明出处。见谅!现在看看这个错误挺简单的。认真分析错误提示就能解决。
写了一个简单的例子,就是从一个activity跳转到另一个activity,一个叫LoginDemoActivity,加一个叫NoteActiviy。跳转的代码段如下:
btnLogin=(Button)findViewById(R.id.btnLogin); btnLogin.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) { if("xxxx@126.com".equals(account.getText().toString().trim()) && "xxxxxxxxxxx".equals(password.getText().toString().trim())){ Intent intent=new Intent(); intent.setClass(LoginDemoActivity.this, NoteActivity.class); startActivity(intent); LoginDemoActivity.this.finish(); }else{ Toast.makeText(LoginDemoActivity.this, "输入的帐号或密码有误!", Toast.LENGTH_LONG).show(); } } }); 在NoteActivity的代码如下: protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.note); btnClose=(Button)findViewById(R.id.btnClose); btnClose.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) { NoteActivity.this.finish(); } }); }
在AndroidMainfest.xml中已经注册了这两个activity。
但当程序运行起来时,抛出如下异常:
android.app.SuperNotCalledException did not call through to supperonCreate()
Google了一把,原来是在NoteActivity的onCreate()函数中没有加super.onCreate(savedInstanceState);
修改代码后,运行正常,修改后的代码为:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.note);
btnClose=(Button)findViewById(R.id.btnClose);
btnClose.setOnClickListener(newButton.OnClickListener()
public voidonClick(View v) {
NoteActivity.this.finish();
}
});
}注意红色高亮处!