错误:android.app.SuperNotCalledException

时间:2011-07-15 11:21:23

标签: android

我是android的新用户,我已经创建了一个android数据库连接并创建了表应用程序,但在运行时会产生错误。
听到错误:

 07-15 16:25:55.404: ERROR/AndroidRuntime(3308): Uncaught handler: thread main exiting due to uncaught exception
 07-15 16:25:55.454: ERROR/AndroidRuntime(3308): android.app.SuperNotCalledException: Activity {org.example.sqldemo/org.example.sqldemo.SQLDemo} did not call through to super.onDestroy()
 07-15 16:25:55.454: ERROR/AndroidRuntime(3308):     at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3134)
 07-15 16:25:55.454: ERROR/AndroidRuntime(3308):    enter code here at                android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3159)
 07-15 16:25:55.454: ERROR/AndroidRuntime(3308):     at android.app.ActivityThread.access$2400(ActivityThread.java:112)
 07-15 16:25:55.454: ERROR/AndroidRuntime(3308):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1724)
 07-15 16:25:55.454: ERROR/AndroidRuntime(3308):     at android.os.Handler.dispatchMessage(Handler.java:99)
 07-15 16:25:55.454: ERROR/AndroidRuntime(3308):     at android.os.Looper.loop(Looper.java:123)
 07-15 16:25:55.454: ERROR/AndroidRuntime(3308):     at android.app.ActivityThread.main(ActivityThread.java:3948)
 07-15 16:25:55.454: ERROR/AndroidRuntime(3308):     at java.lang.reflect.Method.invokeNative(Native Method)
 07-15 16:25:55.454: ERROR/AndroidRuntime(3308):     at java.lang.reflect.Method.invoke(Method.java:521)
 07-15 16:25:55.454: ERROR/AndroidRuntime(3308):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
 07-15 16:25:55.454: ERROR/AndroidRuntime(3308):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
 07-15 16:25:55.454: ERROR/AndroidRuntime(3308): at dalvik.system.NativeStart.main(NativeMethod)

SQLDemo.java 我的代码听到了:

 package com.dailynote;     
 import android.app.Activity;     
 import android.content.ContentValues;     
 import android.database.Cursor;     
 import android.database.sqlite.SQLiteDatabase;     
 import android.os.Bundle;     
 import android.widget.TextView;     
 public class SQLDemo extends Activity {     
   EventDataSQLHelper eventsData;     
   TextView output;     
   @Override     
   public void onCreate(Bundle savedInstanceState) {     
          super.onCreate(savedInstanceState);
     setContentView(R.layout.output);     
     output = (TextView) findViewById(R.id.textView1);     
     eventsData = new EventDataSQLHelper(this);     
     addEvent("Hello Android Event");     
     Cursor cursor = getEvents();     
     showEvents(cursor);     
   }       
   @Override     
   public void onDestroy() {     
     eventsData.close();     
   }     
   private void addEvent(String title) {     
     SQLiteDatabase db = eventsData.getWritableDatabase();     
     ContentValues values = new ContentValues();     
     values.put(EventDataSQLHelper.TIME, System.currentTimeMillis());     
     values.put(EventDataSQLHelper.TITLE, title);
     db.insert(EventDataSQLHelper.TABLE, null, values);
   }
   private Cursor getEvents() {
     SQLiteDatabase db = eventsData.getReadableDatabase();
     Cursor cursor = db.query(EventDataSQLHelper.TABLE, null, null, null, null,
         null, null);    
     startManagingCursor(cursor);
     return cursor;
   }
   private void showEvents(Cursor cursor) {
     StringBuilder ret = new StringBuilder("Saved Events:\n\n");
     while (cursor.moveToNext()) {
       long id = cursor.getLong(0);
       long time = cursor.getLong(1);
       String title = cursor.getString(2);
       ret.append(id + ": " + time + ": " + title + "\n");
     }
     output.setText(ret);
   }
 }

EventDataSQLHelper.java

 package com.dailynote;
 import android.content.Context;
 import android.database.sqlite.SQLiteDatabase;
 import android.database.sqlite.SQLiteOpenHelper;
 import android.provider.BaseColumns;
 import android.util.Log;
 /** Helper to the database, manages versions and creation */
 public class EventDataSQLHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "events.db";
    private static final int DATABASE_VERSION = 1;
    // Table name
    public static final String TABLE = "events";
    // Columns
    public static final String TIME = "time";
    public static final String TITLE = "title";
    public EventDataSQLHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "create table " + TABLE + "( " + BaseColumns._ID
            + " integer primary key autoincrement, " + TIME + " integer, "
                + TITLE + " text not null);";
        Log.d("EventsData", "onCreate: " + sql);
        db.execSQL(sql);
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (oldVersion >= newVersion)
            return;
        String sql = null;
        if (oldVersion == 1) 
            sql = "alter table " + TABLE + " add note text;";
        if (oldVersion == 2)
            sql = "";
        Log.d("EventsData", "onUpgrade  : " + sql);
        if (sql != null)
            db.execSQL(sql);
    }
 } 

5 个答案:

答案 0 :(得分:22)

public void onDestroy() {   
    super.onDestroy();
    eventsData.close();     
}

这是要调用的,因为android中的Activity类本身会进行一些清理。 当派生类重写基类函数时,就像在onDestroy()的情况下所做的那样,需要显式调用基类函数来执行预期的操作。

答案 1 :(得分:14)

我添加了这一行,一切正常:

super.onCreate(savedInstanceState);

将其添加到OnCreate()方法的第一行。

答案 2 :(得分:2)

 @Override     
   public void onDestroy() {   
  super.onDestroy()
     eventsData.close();     
   }   

答案 3 :(得分:0)

我为我的情况找到了一个解决方案,我希望在允许onDestroy()继续之前让标签栏动画起来。仅当super.onDestroy()未设置无法访问的mCalled字段时才会抛出SuperNotCalledException,但是在源代码中挖掘显示公共方法super.onDestroyView()仅设置此字段 - 所以我做了以下操作以允许实际超级要在endAction中调用的.onDestroy()(使用RetroLambda):

@Override
public void onDestroy() {
    super.onDestroyView();  //Calling this public method will prevent the android.support.v4.app.SuperNotCalledException when we don't immediately call the super.onDestroy - since it is in an endAction
    removeHeaderView(super::onDestroy);
}

希望这有助于其他人!

答案 4 :(得分:0)

为onDestroy()方法添加此代码。

super.onDestroy();

它对我有用。