我正在做一个谷歌地图应用程序。
我写了一个函数来查找地址。
当我在主要活动中编写代码时,它是工作。
但是当我在另一个活动中写作时。它总是强制关闭应用程序。
出了什么问题,我该如何解决?
这是我的日志:
11-24 08:56:52.659: E/AndroidRuntime(16334): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.zzzzzz/com.example.zzzzzz.Direction}: java.lang.NullPointerException
这是我的主要活动代码查找地址:
// find address
final Geocoder geocoder = new Geocoder(MainActivity.this);
final EditText ed = (EditText) findViewById(R.id.edAddress);
Button btAddress = (Button) findViewById(R.id.btAddress);
btAddress.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String Ten = ed.getText().toString();
try {
List<Address> l = geocoder.getFromLocationName(Ten, 5);
if (l != null && l.size() > 0) {
int lat = (int) (l.get(0).getLatitude() * 1000000);
int lon = (int) (l.get(0).getLongitude() * 1000000);
GeoPoint pt = new GeoPoint(lat, lon);
p = pt;
MapOverlay mar = new MapOverlay();
listOfOverlays.add(mar);
mv.getController().setZoom(17);
mv.getController().setCenter(pt);
}
} catch (IOException e) {
// TODO Auto-generated catch block
Log.v("Loi: ", "Loi 1 cua tui ne");
}
}
});
这是我的功能开始其他活动:
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.about:
//about
Intent about = new Intent("com.example.zzzzzz.About");
startActivity(about);
break;
case R.id.setting:
//setting
Intent setting = new Intent("com.example.zzzzzz.Setting");
startActivity(setting);
break;
case R.id.direction:
//direction
Intent direction = new Intent("com.example.zzzzzz.Direction");
startActivity(direction);
break;
case R.id.MyLocation:
//mylocation
//Intent mylocation = new Intent("com.example.zzzzzz.MyLocation");
//startActivity(mylocation);
initLocationManagerV2();
LocationManager locationManager;
locationManager = (LocationManager)getSystemService (Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation (LocationManager.GPS_PROVIDER);
double lat = location.getLatitude();
double lng = location.getLongitude();
GeoPoint find = new GeoPoint((int)(lat*1E6), (int)(lng*1E6));
mv.getController().animateTo(find);
mv.invalidate();
break;
}
return false;
}
这是我的指导活动:
public class Direction extends Activity {
MainActivity maintest;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.direction);
// find address
final Geocoder geocoder = new Geocoder(maintest);
final EditText edMylocation = (EditText) findViewById(R.id.edMylocation);
final EditText edEndpoint = (EditText) findViewById(R.id.edEndpoint);
Button btDirection = (Button) findViewById(R.id.btDirection);
btDirection.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String endpoint = edEndpoint.getText().toString();
try {
List<Address> l = geocoder.getFromLocationName(endpoint, 5);
if (l != null && l.size() > 0) {
int lat = (int) (l.get(0).getLatitude() * 1000000);
int lon = (int) (l.get(0).getLongitude() * 1000000);
GeoPoint pt = new GeoPoint(lat, lon);
//p = pt;
//MapOverlay mar = new MapOverlay();
//listOfOverlays.add(mar);
maintest.mv.getController().setZoom(17);
//maintest.mv.getController().setCenter(pt);
}
} catch (IOException e) {
// TODO Auto-generated catch block
Log.v("Loi: ", "Loi 1 cua tui ne");
}
}
});
}
}
////////////////////////////////这是我做的功能(AndylandDev):
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == RESULT_OK) {
String endpoint = data.getStringExtra("endpoint");
final Geocoder geocoder = new Geocoder(MainActivity.this);
try {
List<Address> l = geocoder.getFromLocationName(endpoint, 5);
if (l != null && l.size() > 0) {
int lat = (int) (l.get(0).getLatitude() * 1000000);
int lon = (int) (l.get(0).getLongitude() * 1000000);
GeoPoint pt = new GeoPoint(lat, lon);
p = pt;
//MapOverlay mar = new MapOverlay();
//listOfOverlays.add(mar);
mv.getController().setZoom(17);
mv.getController().setCenter(pt);
}
} catch (IOException e) {
// TODO Auto-generated catch block
Log.v("Loi: ", "Loi 1 cua tui ne");
}
}
}