我在做Google地图应用程序 我写了一个函数来查找地址 当我在主要活动中编写代码时,它是有效的 但是当我写另一个活动时。它总是强制关闭app。
有什么问题以及如何解决?
这是我的日志:
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");
}
}
}
答案 0 :(得分:1)
所以
new Geocoder(maintest);
可能会抛出NullPointerException,因为你的maintest变量是null。一般来说,活动不应该像这样直接相互访问。这将是正确的方法:
启动方向活动时:
case R.id.direction:
//direction
Intent direction = new Intent("com.example.zzzzzz.Direction");
startActivityForResult(direction, 1);
break;
在方向活动的onCreate()
...
btDirection.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("endpoint", edEndpoint.getText().toString());
setResult(RESULT_OK, intent);
finish();
});
...
然后将此方法添加到MainActivity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == RESULT_OK) {
String endpoint = data.getStringExtra("endpoint");
//do whatever with it
}