当前位置: 代码迷 >> Android >> android.app.SuperNotCalledException did not call through to supper onCreate()的有关问题
  详细解决方案

android.app.SuperNotCalledException did not call through to supper onCreate()的有关问题

热度:171   发布时间:2016-05-01 18:52:43.0
android.app.SuperNotCalledException did not call through to supper onCreate()的问题

写了一个简单的例子,就是从一个activity跳转到另一个activity,一个叫LoginDemoActivity,加一个叫NoteActiviy。跳转的代码段如下:

btnLogin=(Button)findViewById(R.id.btnLogin);
??????? btnLogin.setOnClickListener(new Button.OnClickListener(){

??? ??? ??? public void onClick(View v) {
??? ??? ??? ??? if("[email protected]".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 supper onCreate();

?

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(new Button.OnClickListener(){

??? ??? ??? public void onClick(View v) {
??? ??? ??? ??? NoteActivity.this.finish();
??? ??? ??? }
??? ??? ???
??? ??? });
??? }

注意红色高亮处!

  相关解决方案