当我点击接近响应的开始按钮它崩溃了我的应用程序

时间:2015-05-31 08:24:04

标签: android

当我点击按钮时,它会调用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);
}

2 个答案:

答案 0 :(得分:0)

在尝试访问其数据之前,请在光标上使用<? $url="https://api.github.com/search/repositories?page=2&q=language:javascript&sort=stars&order=desc"; // Initiate curl $ch = curl_init(); // Disable SSL verification //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Will return the response, if false it print the response curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Set the url curl_setopt($ch, CURLOPT_URL,$url); // Execute $result=curl_exec($ch); // Closing curl_close($ch); // Will dump a beauty json :3 var_dump(json_decode($result, true)); ?>方法。

答案 1 :(得分:0)

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;
        }   
    } 

如何使用

private DBAdapter db;

db = new DBAdapter(mContext);

在从数据库获取数据之前,您打开数据库,如:

db.open();

开始使用光标按查询

获取数据
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();

完成数据库操作后,您必须关闭数据库,如:

db.close();