我有一个活动
package com.commonsware.android.constants;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
public class ConstantsBrowser extends ListActivity {
private LocationManager lm;
private LocationListener locListener;
private TextView latTxt, lonTxt;
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);
}
}
}
我成功地在我的主要活动中显示纬度经度
但对话框不显示纬度和经度
我的对话框在同一个活动中
这是界面
如何在对话框中显示纬度经度?
编辑
我添加了这个但强制关闭
private void add() {
LayoutInflater inflater=LayoutInflater.from(this);
View addView=inflater.inflate(R.layout.add_edit, null);
final DialogWrapper wrapper=new DialogWrapper(addView);
TextView tvsellAll = (TextView) findViewById(R.id.longitudeTxtt);
TextView tvsell10 = (TextView) findViewById(R.id.longitudeTxtt);
tvsellAll.setText(latTxt.toString());
tvsell10.setText(lonTxt.toString());
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();
}
行中的空指针tvLat.setText(latTxt.toString());
请帮我
BR
亚历克斯
解决了
我忘记添加addView
了findViewById
。谢谢大家