我正在为我的手机创建一个应用,并在保存数据时收到这些错误。我删除导入以获得更好的可视化效果。
03-19 22:16:16.040 21727-21727/com.thedarkstudios.spock.venlacesrf E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.thedarkstudios.spock.venlacesrf.dao.TorreDAO.createTorre(TorreDAO.java:47)
at com.thedarkstudios.spock.venlacesrf.activities.CadastrarTorre$2.onClick(CadastrarTorre.java:82)
at android.view.View.performClick(View.java:4101)
at android.view.View$PerformClick.run(View.java:17088)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5076)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)
我的dbHelper(似乎没问题):
/**
* Created by Usuario on 19/03/2015.
*/
public class DataBaseHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "venlacesrf.db";
private static final int DB_VERSION = 1;
public static final String TABLE_TORRES = "torres";
public static final String COL_ID = "_id";
public static final String COL_LAT = "lat";
public static final String COL_LNG = "lng";
public static final String COL_LOCAL = "local";
public static final String COL_DESC = "desc";
public static final String COL_ALTURA = "altura";
private static final String DB_CREATE = "CREATE TABLE "
+ TABLE_TORRES + "(" + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COL_LAT + " DOUBLE NOT NULL, "
+ COL_LNG + " DOUBLE NOT NULL, "
+ COL_LOCAL + " TEXT NOT NULL, "
+ COL_DESC + " TEXT NOT NULL, "
+ COL_ALTURA + " DOUBLE NOT NULL, "
+ ");";
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DB_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(DataBaseHelper.class.getName(),
"Atualizando banco de dados de " + oldVersion + " para "
+ newVersion + ", que irá destruir todos os seus dados.");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_TORRES);
onCreate(db);
}
}
存储库:
/**
* Created by Spock on 18/03/2015.
*/
public class Torre {
private int id;
private double lat;
private double lng;
private String local;
private String desc;
private double altura;
public Torre(){
}
public Torre(int id, double lat, double lng, String local, String desc, double altura){
super();
this.id = id;
this.lat = lat;
this.lng = lng;
this.local = local;
this.desc = desc;
this.altura = altura;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getLat() {
return lat;
}
public void setLat(double lat) {
this.lat = lat;
}
public double getLng() {
return lng;
}
public void setLng(double lng) {
this.lng = lng;
}
public String getLocal() {
return local;
}
public void setLocal(String local) {
this.local = local;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public double getAltura() {
return altura;
}
public void setAltura(double altura) {
this.altura = altura;
}
}
DAO(数据访问对象):
public class TorreDAO {
private SQLiteDatabase database;
private DataBaseHelper dbHelper;
private String[] allColumns = { DataBaseHelper.COL_ID,
DataBaseHelper.COL_LAT, DataBaseHelper.COL_LNG,
DataBaseHelper.COL_LOCAL, DataBaseHelper.COL_DESC,
DataBaseHelper.COL_ALTURA};
public TorreDAO(Context context) {
dbHelper = new DataBaseHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public Torre createTorre(double lat, double lng, String local, String desc, double altura) {
ContentValues values = new ContentValues();
values.put(DataBaseHelper.COL_LAT, lat);
values.put(DataBaseHelper.COL_LNG, lng);
values.put(DataBaseHelper.COL_LOCAL, local);
values.put(DataBaseHelper.COL_DESC, desc);
values.put(DataBaseHelper.COL_ALTURA, altura);
long insertId = database.insert(DataBaseHelper.TABLE_TORRES, null, values);
Cursor cursor = database.query(DataBaseHelper.TABLE_TORRES,
allColumns, DataBaseHelper.COL_ID + " = " + insertId, null, null, null, null);
cursor.moveToFirst();
Torre newTorre = cursorToTorre(cursor);
cursor.close();
return newTorre;
}
public void deleteTorre(Torre torre) {
long id = torre.getId();
System.out.println("Torre deletada com id: " + id);
database.delete(DataBaseHelper.TABLE_TORRES, DataBaseHelper.COL_ID + " = " + id, null);
}
public List<Torre> getAllTorres() {
List<Torre> torres = new ArrayList<Torre>();
Cursor cursor = database.query(DataBaseHelper.TABLE_TORRES,
allColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Torre torre = cursorToTorre(cursor);
torres.add(torre);
cursor.moveToNext();
}
// make sure to close the cursor
cursor.close();
return torres;
}
private Torre cursorToTorre(Cursor cursor) {
Torre torre = new Torre();
torre.setId(cursor.getInt(0));
torre.setLat(cursor.getDouble(1));
torre.setLng(cursor.getDouble(2));
torre.setLocal(cursor.getString(3));
torre.setDesc(cursor.getString(4));
torre.setAltura(cursor.getDouble(5));
return torre;
}
}
活动(表格):
public class CadastrarTorre extends ActionBarActivity {
private EditText txtLat;
private EditText txtLong;
private EditText txtLocal;
private EditText txtDescricao;
private EditText txtAltura;
private Button btnObtLocal;
private Button btnSalvar;
TorreDAO torreDAO = new TorreDAO(this);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cadastrar_torre);
setupElements();
}
// Cria os elementos;
public void setupElements(){
txtLat = (EditText) findViewById(R.id.txtLat);
txtLong = (EditText) findViewById(R.id.txtLong);
txtLocal = (EditText) findViewById(R.id.txtLocal);
txtDescricao = (EditText) findViewById(R.id.txtDescricao);
txtAltura = (EditText) findViewById(R.id.txtAltura);
btnObtLocal = (Button) findViewById(R.id.btnObtLocal);
btnSalvar = (Button) findViewById(R.id.btnSalvar);
// Inicia a captura do gps ao clicar no botao;
btnObtLocal.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
iniciaGPS();
}
});
btnSalvar.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
Torre objTorre = new Torre();
objTorre.setLat(Double.parseDouble(txtLat.getText().toString()));
objTorre.setLng(Double.parseDouble(txtLong.getText().toString()));
objTorre.setLocal(txtLocal.getText().toString());
objTorre.setDesc(txtDescricao.getText().toString());
objTorre.setAltura(Double.parseDouble(txtAltura.getText().toString()));
// Transforma para String
//String torre = String.valueOf(objTorre.getLatitude());
// Coloca na tela;
//txtLocal.setText(torre.toString());
System.out.println("Latitude: " + objTorre.getLat());
System.out.println("Longitude: " + objTorre.getLng());
System.out.println("Local: " + objTorre.getLocal());
System.out.println("Descrição: " + objTorre.getDesc());
System.out.println("Altura (mts): " + objTorre.getAltura());
torreDAO.createTorre(objTorre.getLat(), objTorre.getLng(), objTorre.getLocal(),
objTorre.getDesc(), objTorre.getAltura());
}
});
}
public void iniciaGPS(){
.... }
}
答案 0 :(得分:0)
你发布了很多代码,但似乎你只是错过了#34; open&#34;保存数据前的连接:
torreDAO.open();
torreDAO.createTorre(objTorre.getLat(), objTorre.getLng(), objTorre.getLocal(),
objTorre.getDesc(), objTorre.getAltura());