我正在尝试在Sqlite Database中添加当前位置的纬度和经度。
但是当我打开我的活动时,它说android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)导致崩溃
我不明白是什么问题。
我的密码是

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) {

}


我已使用SQLiteOpenHelper创建数据库。
我以前使用它们时都没有错误,但现在却遇到了这个错误。
有人请帮忙。

我的SQLiteOpenHelper

public class DatabaseHelper extends SQLiteOpenHelper {
public static final String db_name="latLngDB";
public static final int version=1;
Context context;

public DatabaseHelper(Context context){
    super(context,db_name,null,version);
}

@Override
public void onCreate(SQLiteDatabase db){

    db.execSQL("create table latLngTable(_id INTEGER PRIMARY KEY AUTOINCREMENT,latitude integer,longitude integer)");
    Toast.makeText(context,"Database Created",Toast.LENGTH_SHORT).show();
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
    if (oldVersion>=newVersion)

        return;

    if (oldVersion==1){

        Log.d("New Version","Data's can be Upgraded");

    }

    Log.d("Sample Data", "onUpgrade:" + newVersion);

}


}


  LogCat是


07-22 15:31:17.652  14125-14125/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.realtech.latlngrecorder, PID: 14125
java.lang.NullPointerException
        at android.widget.Toast.<init>(Toast.java:93)
        at android.widget.Toast.makeText(Toast.java:241)
        at com.realtech.latlngrecorder.DatabaseHelper.onCreate(DatabaseHelper.java:22)
        at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
        at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
        at com.realtech.latlngrecorder.MainPage$1$1.onLocationChanged(MainPage.java:67)
        at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:279)
        at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:208)
        at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:224)
        at android.os.Handler.dispatchMessage(Handler.java:110)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:5292)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
        at dalvik.system.NativeStart.main(Native Method)

最佳答案

您没有将有效的Context传递给SQLiteOpenHelper构造函数。这就是getDatabaseLocked()中NPE的解释。

您发布的stacktrace显示您已经在帮助程序Toast中NPE初始化了onCreate()。请注意,您的成员变量context尚未初始化。

关于java - android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)nullpointerexception,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31559542/

10-12 04:15