mercredi 22 juillet 2015

android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252) nullpointerexception

I'm trying to add current locations latitude and longitude in Sqlite Database. But when i open my activity it crashes by saying android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252) I can't understand what is the problem. My Code is

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_page);

    try {
        setUpMapIfNeeded();
    }catch (Exception e){
        e.printStackTrace();
        Toast.makeText(MainPage.this, "Google Play Services Out of Date. Please Update.", Toast.LENGTH_SHORT).show();
    }

    googleMap.setMyLocationEnabled(true);
    googleMap.getUiSettings().setMyLocationButtonEnabled(true);
    googleMap.getUiSettings().setMapToolbarEnabled(false);
    googleMap.getUiSettings().setZoomControlsEnabled(true);

    locMan =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
    locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,this);

    Button btnRecord=(Button)findViewById(R.id.button);
    btnRecord.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            LocationManager locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {
                @Override
                public void onLocationChanged(Location location) {
                    LatLng latLng=new LatLng(location.getLatitude(),location.getLongitude());
                    double latitude=location.getLatitude();
                    double longitude=location.getLongitude();
                    ContentValues cv=new ContentValues();
                    try{

                        DatabaseHelper databaseHelper=new DatabaseHelper(MainPage.this);
                        SQLiteDatabase db=databaseHelper.getReadableDatabase();

                        cv.put("latitude",latitude);
                        cv.put("longitude",longitude);

                        long insert=db.insert("latLngTable",null,cv);

                        if (insert!=-1){
                            Toast.makeText(MainPage.this,"Datas Inserted",Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(MainPage.this,"Insertion Failed",Toast.LENGTH_SHORT).show();
                        }

                    }catch (SQLiteException e){
                        Toast.makeText(MainPage.this,e.getMessage(),Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onStatusChanged(String provider, int status, Bundle extras) {

                }

                @Override
                public void onProviderEnabled(String provider) {

                }

                @Override
                public void onProviderDisabled(String provider) {

                }
            });
        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main_page, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.viewLocations) {
        Intent i=new Intent(MainPage.this,LocationsList.class);
        startActivity(i);
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
protected void onResume() {
    super.onResume();
    setUpMapIfNeeded();
}

private void setUpMapIfNeeded() {
    if (googleMap == null) {
        googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
                .getMap();
        if (googleMap != null) {
            setUpMap();
        }
    }
}

private void setUpMap() {
    //googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).icon(bitmapDescriptor).title(locality).snippet(""));
}

@Override
public void onLocationChanged(Location location) {
    LatLng latLng1 = new LatLng(location.getLatitude(), location.getLongitude());
    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng1, 16.5f);
    googleMap.animateCamera(cameraUpdate);
    locMan.removeUpdates(this);
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}

I have used SQLiteOpenHelper to create my database. I have previously used them without error but now I'm getting this error. Anyone please help.

Aucun commentaire:

Enregistrer un commentaire