Android 当我点击“开始”按钮进行邻近响应时,它会使我的应用程序崩溃
当我点击按钮时,它调用Android 当我点击“开始”按钮进行邻近响应时,它会使我的应用程序崩溃,android,Android,当我点击按钮时,它调用setProximityAlert()函数,然后将显示Toast。我不明白问题出在哪里 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.locations); Button start = (Button) findViewById(R.id.start
setProximityAlert()
函数,然后将显示Toast。我不明白问题出在哪里
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.locations);
Button start = (Button) findViewById(R.id.startl);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setProximityAlert();
Toast.makeText(getBaseContext(), "Started", Toast.LENGTH_LONG).show();
}
});
}
private static final String MMH_PROXIMITY_ALERT = "com.example.ProximityAlert";
private void setProximityAlert() {
Cursor c = db.rawQuery("SELECT * from locations", null);
Double lng = c.getDouble(c.getColumnIndex("longitude"));
c.moveToNext();
Double lat = c.getDouble(c.getColumnIndex("latitude"));
c.moveToNext();
Float rds = c.getFloat(c.getColumnIndex("radius"));
c.moveToNext();
String locService = Context.LOCATION_SERVICE;
LocationManager locationManager;
locationManager = (LocationManager)getSystemService(locService);
long expiration = -1; // do not expire
Intent intent = new Intent(MMH_PROXIMITY_ALERT);
PendingIntent proximityIntent = PendingIntent.getBroadcast(this, -1, intent, 0);
locationManager.addProximityAlert(lat, lng, rds, expiration, proximityIntent);
c.close();
locationManager.addProximityAlert(
lat, // the latitude of the central point of the alert region
lng, // the longitude of the central point of the alert region
rds, // the radius of the central point of the alert region, in meters
expiration, // time for this proximity alert, in milliseconds, or -1 to indicate no expiration
proximityIntent // will be used to generate an Intent to fire when entry to or exit from the alert region is detected
);
IntentFilter filter = new IntentFilter(MMH_PROXIMITY_ALERT);
registerReceiver(new ProximityIntentReceiver(), filter);
}
在尝试访问光标数据之前,请在光标上使用
moveToFirst
方法。DBAdapter类:
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;
public class DBAdapter {
// Common params
private static final int DATABASE_VERSION = 1;
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public static final String DATABASE_NAME = "db_name";
public DBAdapter(Context ctx) {
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_MY_TABLE);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS Savedfiles");
onCreate(db);
}
}
// ---open database---
public DBAdapter open() throws SQLException {
db = DBHelper.getWritableDatabase();
return this;
}
// ---closes database---
public void close() {
DBHelper.close();
}
// --- Table ---
public static final String MY_TABLE = "my_tbl";
public static final String COLUMN_ID = "id";
public static final String COLUMN_USER_ID = "user_id";
public static final String COLUMN_TITLE = "title";
private static final String CREATE_MY_TABLE = "create table "
+ MY_TABLE + "(" + COLUMN_ID
+ " text not null, " + COLUMN_USER_ID
+ " text not null, " + COLUMN_TITLE
+ " text not null);";
// ---insert data in database---
public long insertData(Model model) {
ContentValues initialValues = new ContentValues();
initialValues.put(COLUMN_ID, model.getId());
initialValues.put(COLUMN_USER_ID, model.getUserId());
initialValues.put(COLUMN_TITLE, model.getTitle());
return db.insert(MY_TABLE, null, initialValues);
}
// ---get all table's data---
public Cursor getAllData() {
return db.query(MY_TABLE, GET_DATA(), null, null,
null, null, null);
}
// ---deletes all table's data---
public void deleteAllTables() {
SQLiteDatabase db = DBHelper.getWritableDatabase();
db.delete(MY_TABLE, null, null);
}
// ---updates a table's details---
public boolean updateTable(String id, String title) {
ContentValues initialValues = new ContentValues();
// update any field
// initialValues.put(COLUMN_TITLE, title);
return db.update(MY_TABLE, initialValues,
COLUMN_ID + "=" + id, null) > 0;
}
private static String[] GET_DATA(){
String[] getArray = new String []{ COLUMN_ID, COLUMN_USER_ID,COLUMN_TITLE };
return getArray;
}
}
如何使用它
专用数据库适配器数据库
db = new DBAdapter(mContext);
在从数据库中提取数据之前,是否打开数据库,如:
db.open();
db.close();
开始使用光标按查询获取数据
Cursor cur = db.getAllData();
if(cur!=null && cur.getCount()>0){
cur.moveToFirst();
for(int i = 0; i < cur.getCount(); i++)
// Here get all data from cursor
cur.movetoNext();
}
cur.close();
logcat窗口中的错误是什么?您能在这里提供清单文件内容吗?如何发布logcat plz帮助它太大,无法进行评论找到包含单词“异常”和“原因”的红色块。它通常位于应用程序之后的logcat内容末尾crashed@sarfraz你试过我编辑过的答案吗?先生,谢谢你的回答,我真的没有用过这些东西。Plz I使用db=openOrCreateDatabase(“EventsDB”,Context.MODE_PRIVATE,null)创建了db;execSQL(“如果不存在位置(名称VarChar、纬度双精度、经度双精度、半径浮点)则创建表;”;在这种情况下我可以使用DBAdapter吗。这段代码对DBAdapter足够了吗;db=新的DBAdapter(mContext);它在oncreat和setProximityAlert()db.open()中给出了一个错误;太给了一个错误,谢谢先生,因为下面的回答对我有效,所以我不能尝试你的鳕鱼,但谢谢你的帮助。