Java 在另一个类中使用一个类中的变量
我正在用android制作一个应用程序,我正在使用谷歌地图 我有一个导航屏幕,用户可以在其中放置一个位置 然后他们点击一个按钮,就会打开一个地图视图 在这里,他们输入的位置也应该被导航 当我运行这段代码时,我在map.class中得到了plaat的nullpointerexception 我的导航代码是:Java 在另一个类中使用一个类中的变量,java,android,Java,Android,我正在用android制作一个应用程序,我正在使用谷歌地图 我有一个导航屏幕,用户可以在其中放置一个位置 然后他们点击一个按钮,就会打开一个地图视图 在这里,他们输入的位置也应该被导航 当我运行这段代码时,我在map.class中得到了plaat的nullpointerexception 我的导航代码是: public class Navigatie extends Activity{ public String plaats; @Override
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));
}
});
}
}
位置显示编码的“我的地图”的代码是:(只是代码的一部分)
公共导航导航;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
mapView=(mapView)findViewById(R.id.mapview1);
mc=mapView.getController();
mapView.SetBuilTinZoomControl(真);
字符串plaats=nav.plaats;
Geocoder Geocoder=新的Geocoder(这个,Locale.getDefault());
尝试
{
列表地址=geoCoder.getFromLocationName(plaats,5);
字符串strCompleteAddress=“”;
如果(地址.size()>0){
地质点p=新的地质点(
(int)(addresses.get(0.getLatitude()*1E6),
(int)(addresses.get(0.getLongitude()*1E6));
司马迁(p),;
mapView.invalidate();
}
}捕获(IOE异常){
e、 printStackTrace();
}
将plaat添加到您的意图中,这将使用
intent.putExtra("location", this.plaats);
然后在映射活动中,在oncreate中:
String map_plaats = this.getIntent().getStringExtra("location");
您希望将字符串发送到其他活动 为此,您可以使用
putExtra(“键”、“值”);
和
您可以像这样在Map.class中获取此字符串
getIntent().getStringExtra("key");
要了解更多关于意图的信息,请搜索在活动之间共享数据或在组件之间传递数据等。这个问题有很多版本,也有很多答案。我已经这样做了,但现在它给出了一个错误:java.lang.IllegalArgumentException:locationName==Null我在代码中的任何地方都看不到locationName,所以它是空的我很难说到底发生了什么。这是地理编码器。getFromLocationName(plaats,5);我已经完成了:String map_plaats=this.getIntent().getStringExtra(“location”);geoCoder.getFromLocationName(map_plaats,5);
getIntent().getStringExtra("key");