我正在android中制作应用程序,并且我正在使用Google地图。使用另一个类中的一个类的变量
我有一个导航屏幕,用户可以放在一个位置。
然后他们点击一个按钮并打开一个mapview。
这里他们输入的位置也应该被导航。
当我运行这段代码我得到国家地点一个NullPointerException我map.class
我的导航代码:
public class Navigatie extends Activity{
public String plaats;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.navigatie);
EditText text = (EditText)findViewById(R.id.title);
this.plaats = text.getText().toString();
// We create a new ImageButton which links to the map.java class
ImageButton imageButton = (ImageButton) findViewById(R.id.imageButton1);
// We give a onclicklistener method to the button imageButton
imageButton.setOnClickListener(new OnClickListener() {
// if the imageButton is clicked we start a new activity called "Map"
public void onClick(View v) {
startActivity(new Intent(Navigatie.this, Map.class));
}
});
}
}
我的地图可以看到的位置显示编码的代码为: (只是代码的一部分)
public Navigatie nav;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
mapView = (MapView) findViewById(R.id.mapview1);
mc = mapView.getController();
mapView.setBuiltInZoomControls(true);
String plaats = nav.plaats;
Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
try
{
List<Address> addresses = geoCoder.getFromLocationName(plaats, 5);
String strCompleteAddress = "";
if (addresses.size() > 0) {
GeoPoint p = new GeoPoint(
(int) (addresses.get(0).getLatitude() * 1E6),
(int) (addresses.get(0).getLongitude() * 1E6));
mc.animateTo(p);
mapView.invalidate();
}
} catch (IOException e) {
e.printStackTrace();
}
搜索活动之间共享数据,或在组件之间传递数据等等。这个问题有很多版本,许多答案已经在SO上。 –