我正在創建一個GPS跟蹤應用程序,我在開始階段,我使用LocationManager跟蹤了緯度和經度,現在我想要做的下一件事是將所有數據存儲在數據庫中,我有創建適配器類,並從MainActivity調用它,但在綁定上下文和打開數據庫時得到NullPointerException,請看看我的Logcat詳細信息,並請給我一個解決方案。NullPointerException在創建數據庫時
我越來越近
dbAdapter = new LocationDbAdapter(this);
dbAdapter.open();
錯誤日誌中的錯誤消息:
android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:234)
android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:149)
om.example.androidtrackingapplication.LocationDbAdapter.open(LocationDbAdapter.java:78)
我附上完整的logcat文件下面請讓我知道我錯了。
我將位置發送到適配器類,它將獲取值並存儲在表中。
我打電話給數據庫實例,當我得到的位置更新,當我點擊 定位按鈕,將調用LocationManager來獲取位置更新(這是我的應用程序崩潰點)。
LocationDbAdapter.java
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.location.Location;
import android.util.Log;
public class LocationDbAdapter {
public static final String KEY_NAME = "name";
public static final String KEY_LATITUDE = "latitude";
public static final String KEY_LONGITUDE = "longitude";
public static final String KEY_ACCURACY = "accuracy";
public static final String KEY_TIME = "time";
public static final String KEY_ROWID = "_id";
private static final String TAG = "LocationDbAdapter";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static final String DATABASE_CREATE =
"create table locations (_id integer primary key autoincrement, "
+ "name text not null, latitude real not null, longitude real not null, accuracy integer not null, time integer not null);";
private static final String DATABASE_NAME = "data";
private static final String DATABASE_TABLE = "locations";
private static final int DATABASE_VERSION = 4;
private final Context mCtx;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.i(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS locations");
onCreate(db);
}
}
/**
* Constructor - takes the context to allow the database to be
* opened/created
*
* @param ctx the Context within which to work
*/
public LocationDbAdapter(Context ctx) {
this.mCtx = ctx;
}
/**
* Open the notes database. If it cannot be opened, try to create a new
* instance of the database. If it cannot be created, throw an exception to
* signal the failure
*
* @return this (self reference, allowing this to be chained in an
* initialization call)
* @throws SQLException if the database could be neither opened or created
*/
public LocationDbAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
mDbHelper.close();
}
/**
* Add new location. If the location is
* successfully created return the new rowId, otherwise return
* a -1 to indicate failure.
*
* @param location
* @return rowId or -1 if failed
*/
public long addLocation(Location location) {
Log.i(TAG, "Writing new location:"+location.toString());
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_NAME, location.getProvider());
contentValues.put(KEY_LATITUDE, location.getLatitude());
contentValues.put(KEY_LONGITUDE, location.getLongitude());
contentValues.put(KEY_ACCURACY, location.getAccuracy());
contentValues.put(KEY_TIME, location.getTime());
return mDb.insert(DATABASE_TABLE, null, contentValues);
}
public int clearDatabase() {
Log.i(TAG, "clearDatabase()");
return mDb.delete(DATABASE_TABLE, null, null);
}
public Cursor fetchAllLocations() {
return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME, KEY_LATITUDE, KEY_LONGITUDE, KEY_ACCURACY,
KEY_TIME}, null, null, null, null, null);
}
}
MainActivity.java
package com.example.androidtrackingapplication;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.BatteryManager;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity
{
LocationManager mlocManager ;
LocationListener mlocListener ;
Location loc;
double lat;
double longitude;
EditText Lat;
EditText Long,battery,hitTime,intervalTime;
int level = -1;
static int i;
Button Send,Reset,Locate,Close;
private LocationDbAdapter dbAdapter;
Timer t;
protected CountDownTimer waitTimer;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Lat= (EditText) findViewById(R.id.editText1);
Long=(EditText) findViewById(R.id.editText2);
//hitTime= (EditText) findViewById(R.id.editText3);
//intervalTime=(EditText) findViewById(R.id.editText4);
Locate=(Button) findViewById(R.id.button1);
Send= (Button) findViewById(R.id.button2);
Close=(Button) findViewById(R.id.button3);
Locate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// String hit=hitTime.getText().toString();
// int hit1= Integer.parseInt(hit);
// System.out.println("Hit"+hit1);
Send.setEnabled(false);
Lat.setText(" ");
Long.setText(" ");
/* Enable the Gps */
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
sendBroadcast(intent);
waitTimer = new CountDownTimer(60000,1000) {
public void onTick(long millisUntilFinished) {
i = i+1;
System.out.println("IN:"+i);
mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mlocListener = new MyLocationListener();
loc = mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
// mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,300000, 0, mlocListener);
//mlocManager.requestSingleUpdate(LocationManager.GPS_PROVIDER,mlocListener, null);
mlocManager.requestSingleUpdate(LocationManager.GPS_PROVIDER,mlocListener, null);
}
public void onFinish() {
System.out.println("Location not found");
Toast.makeText(getApplicationContext(), "Location Not Found ", Toast.LENGTH_LONG).show();
if(mlocManager!=null)
{
}
Lat.setText(" ");
Long.setText(" ");
if(loc!=null)
{
Lat.setText(String.valueOf(loc.getLatitude()));
Long.setText(String.valueOf(loc.getLongitude()));
}
Toast.makeText(getApplicationContext(), "Last Location ",Toast.LENGTH_LONG).show();
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", false);
sendBroadcast(intent);
}
}.start();
}
});
Send.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// String lat1="15.";
// String longitude1="75.";
HttpClient httpClient = new DefaultHttpClient();
String postURL = "";
postURL = "http://bb.trackfleet.biz/android_gps/lat_long.php?lat="+lat+"&long="+longitude;
HttpPost httpPost = new HttpPost(postURL);
HttpResponse httpResponse = null;
try
{
Toast.makeText(getApplicationContext(),"send", Toast.LENGTH_SHORT).show();
httpResponse = httpClient.execute(httpPost);
String str = inputStreamToString(httpResponse.getEntity().getContent()).toString();
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
}
catch(Exception ex)
{
Toast.makeText(getApplicationContext(),"error", Toast.LENGTH_SHORT).show();
}
}
});
Close.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(mlocManager!=null)
{
mlocManager.removeUpdates(mlocListener);
mlocManager=null;
}
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", false);
sendBroadcast(intent);
finish();
}
});
}
private StringBuilder inputStreamToString(InputStream is) {
String line = "";
StringBuilder total = new StringBuilder();
// Wrap a BufferedReader around the InputStream
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
// Read response until the end
try {
while ((line = rd.readLine()) != null) {
total.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
// Return full string
return total;
}
MyLocationListener服務
public class MyLocationListener extends Service implements LocationListener
{
@Override
public void onCreate() {
dbAdapter = new LocationDbAdapter(this);
dbAdapter.open();
}
@Override
public void onLocationChanged(Location loc)
{
if(loc!=null)
{
dbAdapter = new LocationDbAdapter(this);
dbAdapter.open();
long id=dbAdapter.addLocation(loc);
Toast.makeText(getApplicationContext(), "Added Data Sucessfully " +id ,Toast.LENGTH_SHORT).show();
}
lat=loc.getLatitude();
longitude=loc.getLongitude();
waitTimer.cancel();
String Text = "My current location is: " +
"Latitud = " + loc.getLatitude() +
"Longitud = " + loc.getLongitude();
Lat.setText(String.valueOf(lat));
Long.setText(String.valueOf(longitude));
if((Lat.getText()!=null) && (Long.getText()!=null))
{
Send.setEnabled(true);
Send.performClick();
}
}
@Override
public void onProviderDisabled(String provider)
{
Toast.makeText(getApplicationContext(),"Gps Disabled Pls Enable It",Toast.LENGTH_SHORT).show();
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
@Override
public void onProviderEnabled(String provider)
{
Toast.makeText(getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
@Override
public void onDestroy()
{
super.onDestroy();
dbAdapter.close();
mlocManager.removeUpdates(this);
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
}
任何異常/錯誤日誌文件? –
@Glenn檢查日誌文件 – krish
這只是一個簡單的情況,沒有在目標上的SQLite庫?這將拋出一個異常 –