Android lifecycle issue and null pointer

I have an application that uses an SQL database. This is encapsulated by the SQLiteOpenHelper class. When the splash screen starts, it calls init in the DataProvider class, which stores a protected static instance of SQLiteOpenHelper. init simply calls the SQLiteOpenHelper constructor:

public class UKMPGData extends SQLiteOpenHelper
{
public UKMPGData(Context context, String databaseName)
{
    super(context, databaseName, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db)
{
    //create table and set up triggers etc
}
    @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
    onCreate(db);
}
}

public class UKMPGDataProvider
{
protected static UKMPGData uKMpgData;

public static void init(Context aApplicationContext, String aDatabaseName)
{       
    uKMpgData = new UKMPGData(applicationContext, databaseName);
}

public static void close()
{
    uKMpgData.close();
}
}

Then I had two more classes that extended UKMPGDataProvider and therefore had access to uKMpgData. These classes retrieve and store specific types of data from the database. For instance.

public class VehicleDataProvider extends UKMPGDataProvider
{

    public static Cursor getVehicles()
{
    Cursor cursor = null;

    SQLiteDatabase db = uKMpgData.getReadableDatabase();
    cursor = db.query(VEHICLE_TABLE_NAME, GET_VEHICLES_FROM_CLAUSE, null, null, null, null, ORDER_BY);

    return cursor;
}
//...
}

, , , , , , , , Activity, getVehicles() (. ). , uKMpgData .

, Android , , , , - , ? , SplashScreen , , .

- - ( ), .

, . VehicleDataProvider (UKMPGDataProvider), ukMpgData. , , UKMPGDataProvider, .

,

+3
6

Android , Application/Activity . Android , , .

( , ) . , Android , (). "", , , , .

, Android ( ) Activity. , , Android / . , , . , UKMPGDataProvider, , NullPointerException.

.

Activity.onSaveInstanceState(Bundle) , , Activity.onCreate(Bundle) ( Bundle) . , , , ( , ). , UKMPGDataProvider , .

+8

onStart() onResume(), onCreate(). , , , . , , , , . onStop() onPause().

, , , , , .

0

SQLiteOpenHelper onCreate(), onDestroy() .

, SQLiteOpenHelper

0

, , , , .

,

public static Cursor getVehicles()

uKMpgData null, init. . , init, . - :

Cursor cursor = null;
SQLiteDatabase db = getuKMpgData().getReadableDatabase();
.....

public static UKMPGData getuKMpgData(){
   if(uKMpgData==null){
       Context cnt= //Some method for retrieving the context
       String dbname="myDB";
       init(cnt,dbname)
   }
   return uKMpgData;
}

, , getContext() getApplicationContext(). , .

0

VehicleDataProvider.init() onResume().

VehicleDataProvider onResume , onPause.

android. , ( , )

public class UKMPGDataProvider
{
  protected UKMPGData uKMpgData;

  protected UKMPGDataProvider(Context aApplicationContext, String aDatabaseName)
  {       
      uKMpgData = new UKMPGData(applicationContext, databaseName);
  }

  public void close()
  {
      uKMpgData.close();
  }
}

VehicleDataProvider

public class VehicleDataProvider extends UKMPGDataProvider
{
  public VehicleDataProvider(Context aApplicationContext, String aDatabaseName)
  {       
      super (aApplicationContext, aDatabaseName);
  }

    public Cursor getVehicles()
    {
        Cursor cursor = null;

        SQLiteDatabase db = uKMpgData.getReadableDatabase();
        cursor = db.query(VEHICLE_TABLE_NAME, GET_VEHICLES_FROM_CLAUSE, null, null, null, null, ORDER_BY);

        return cursor;
    }
    //...
}

..  extends Activity {
    private VehicleDataProvider vehicle;

    @Override
    protected void onResume() {
        super.onResume();
        vehicle = new VehicleDataProvider (this, "database.db");
        ... 
    }

    @Override
    protected void onPause() {
        vehicle.close ();
        vehicle = null;
        super.onPause();
    }
   ...
  }

When you get a null pointer exception, this is very similar to accessing the vehicle until early or too late.

It is absolutely necessary to free the database in onPause. OnPause happens, for example. when another action becomes visible. In this case, any memory occupied by the database must be freed.

android is designed as low memory memory, free up memory as soon as possible. This behavior is rare when you come from other java environments.

Hope this helps

0
source

All Articles