i have try with commonsware sample
here is the activity
public class ConstantsBrowser extends ListActivity {
private LocationManager lm;
private LocationListener locListener;
private TextView latTxt, lonTxt;
private double dLat=0;
private double dLon=0;
Intent intent = null;
private static final int ADD_ID = Menu.FIRST+1;
private static final int DELETE_ID = Menu.FIRST+3;
private static final int UPDATE_ID = Menu.FIRST+4;
public static final int SHOW_SUB_ACTIVITY_VIEW=3;
private DatabaseHelper db=null;
private Cursor constantsCursor=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.banner);
latTxt = (TextView) findViewById(R.id.latitudeTxt);
lonTxt = (TextView) findViewById(R.id.longitudeTxt);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locListener = new MyLocationListener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 10,locListener);
db=new DatabaseHelper(this);
constantsCursor=db
.getReadableDatabase()
.rawQuery("SELECT _ID, title, value "+
"FROM constants ORDER BY _ID",
null);
ListAdapter adapter=new SimpleCursorAdapter(this,
R.layout.row, constantsCursor,
new String[] {
DatabaseHelper.ID,
DatabaseHelper.TITLE,
DatabaseHelper.VALUE},
new int[] {R.id.id, R.id.alamat, R.id.value});
setListAdapter(adapter);
registerForContextMenu(getListView());
}
@Override
public void onDestroy() {
super.onDestroy();
constantsCursor.close();
db.close();
}
private class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
// TODO Auto-generated method stub
if (loc != null) {
latTxt.setText(String.valueOf(loc.getLatitude()));
lonTxt.setText(String.valueOf(loc.getLongitude()));
}
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, ADD_ID, Menu.NONE, "Data Baru")
.setIcon(R.drawable.add)
.setAlphabeticShortcut('a');
return(super.onCreateOptionsMenu(menu));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case ADD_ID:
add();
return(true);
}
return(super.onOptionsItemSelected(item));
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
menu.add(Menu.NONE, DELETE_ID, Menu.NONE, "Delete");
menu.add(Menu.NONE, UPDATE_ID, Menu.NONE, "Update")
.setAlphabeticShortcut('d');
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case DELETE_ID:
AdapterView.AdapterContextMenuInfo info=
(AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
delete(info.id);
return(true);
case UPDATE_ID:
intent = new Intent(ConstantsBrowser.this, MainActivity.class);
startActivityForResult(intent, SHOW_SUB_ACTIVITY_VIEW);
return(true);
}
return(super.onOptionsItemSelected(item));
}
private void add() {
LayoutInflater inflater=LayoutInflater.from(this);
View addView=inflater.inflate(R.layout.add_edit, null);
final DialogWrapper wrapper=new DialogWrapper(addView);
new AlertDialog.Builder(this)
.setTitle(R.string.add_title)
.setView(addView)
.setPositiveButton(R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
processAdd(wrapper);
}
})
.setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// ignore, just dismiss
}
})
.show();
}
private void delete(final long rowId) {
if (rowId>0) {
new AlertDialog.Builder(this)
.setTitle(R.string.delete_title)
.setPositiveButton(R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
processDelete(rowId);
}
})
.setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// ignore, just dismiss
}
})
.show();
}
}
private void processAdd(DialogWrapper wrapper) {
ContentValues values=new ContentValues(2);
values.put(DatabaseHelper.TITLE, wrapper.getTitle());
values.put(DatabaseHelper.VALUE, wrapper.getValue());
db.getWritableDatabase().insert("constants", DatabaseHelper.TITLE, values);
constantsCursor.requery();
}
private void processDelete(long rowId) {
String[] args={String.valueOf(rowId)};
db.getWritableDatabase().delete("constants", "_ID=?", args);
constantsCursor.requery();
}
class DialogWrapper {
EditText titleField=null;
EditText valueField=null;
View base=null;
DialogWrapper(View base) {
this.base=base;
valueField=(EditText)base.findViewById(R.id.value);
}
String getTitle() {
return(getTitleField().getText().toString());
}
String getValue() {
return(getValueField().getText().toString());
}
private EditText getTitleField() {
if (titleField==null) {
titleField=(EditText)base.findViewById(R.id.title);
}
return(titleField);
}
private EditText getValueField() {
if (valueField==null) {
valueField=(EditText)base.findViewById(R.id.value);
}
return(valueField);
}
}
}
i add gps in this activity but when i try the app is force close
here is the manifest
i add a permission
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >
</uses-permission>
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.CAMERA" >
and here is the logcat nullpointer exception
02-20 12:07:32.203: ERROR/AndroidRuntime(8904): FATAL EXCEPTION: main
02-20 12:07:32.203: ERROR/AndroidRuntime(8904): java.lang.NullPointerException
02-20 12:07:32.203: ERROR/AndroidRuntime(8904): at com.commonsware.android.constants.ConstantsBrowser$MyLocationListener.onLocationChanged(ConstantsBrowser.java:98)
02-20 12:07:32.203: ERROR/AndroidRuntime(8904): at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:227)
02-20 12:07:32.203: ERROR/AndroidRuntime(8904): at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:160)
02-20 12:07:32.203: ERROR/AndroidRuntime(8904): at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:176)
02-20 12:07:32.203: ERROR/AndroidRuntime(8904): at android.os.Handler.dispatchMessage(Handler.java:99)
02-20 12:07:32.203: ERROR/AndroidRuntime(8904): at android.os.Looper.loop(Looper.java:130)
02-20 12:07:32.203: ERROR/AndroidRuntime(8904): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-20 12:07:32.203: ERROR/AndroidRuntime(8904): at java.lang.reflect.Method.invokeNative(Native Method)
02-20 12:07:32.203: ERROR/AndroidRuntime(8904): at java.lang.reflect.Method.invoke(Method.java:507)
02-20 12:07:32.203: ERROR/AndroidRuntime(8904): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-20 12:07:32.203: ERROR/AndroidRuntime(8904): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-20 12:07:32.203: ERROR/AndroidRuntime(8904): at dalvik.system.NativeStart.main(Native Method)
02-20 12:07:32.226: WARN/ActivityManager(1308): Force finishing activity com.commonsware.android.constants/.ConstantsBrowser
the null
is in
line latTxt.setText(String.valueOf(loc.getLatitude()));
how to fix that?
latTxt
is null. Make sure you have a <TextView>
with the id @+id/latitudeTxt
in your layout XML file.