2015-05-31 32 views
-1

當我點擊按鈕,它調用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); 
} 
+0

logcat窗口中的錯誤是什麼? –

+0

你能否在這裏提供你的清單文件內容? –

+0

如何發佈日誌貓plz幫助它是太大的意見 – sarfraz

回答

0

使用moveToFirst上光標的方法試圖訪問其數據之前。

+0

謝謝先生,它爲我工作 – sarfraz

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

如何使用它

私人將對DBAdapter分貝;

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(); 

獲取數據的數據庫操作完成後,您必須關閉數據庫裏ke:

db.close(); 
+0

先生感謝您的回覆我真的沒有用過這些東西。 PLZ我創建了數據庫使用db = openOrCreateDatabase(「EventsDB」,Context.MODE_PRIVATE,null); (「CREATE TABLE IF NOT EXISTS locations(name VarChar,latitude Double,longitude Double,radius Float);」); 我可以在這種情況下使用DBAdapter。這段代碼是否足夠用於DBAdapter。 – sarfraz

+0

whare我必須編寫這個私人DBAdapter數據庫; db = new DBAdapter(mContext);它在oncreat和setProximityAlert()中給出一個錯誤db.open();也給出了一個錯誤 – sarfraz

+0

@sarfraz,請檢查我編輯的答案 –

相關問題