2012-10-24 71 views
-1

我正在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(); 
} 
+1

搜索活动之间共享数据,或在组件之间传递数据等等。这个问题有很多版本,许多答案已经在SO上。 –

回答

1

添加国家地点到你的意图,开始使用

地图
intent.putExtra("location", this.plaats); 

然后在你映射的活动,在OnCreate中:

String map_plaats = this.getIntent().getStringExtra("location"); 
+0

我已经做了这个,但现在它给出了一个错误:java.lang.IllegalArgumentException:locationName == null – Sidneyvp

+0

我没有看到你的代码中的任何位置的locationName,所以我很难说出发生了什么。 – raydowe

+0

这是geoCoder.getFromLocationName(plaats,5); 我做过的: String map_plaats = this.getIntent()。getStringExtra(“location”); geoCoder.getFromLocationName(map_plaats,5); – Sidneyvp

1

你要发送的字符串,似乎其他活动。

为此,您可以使用putExtra("key", "value");

你可以得到这个字符串Map.class这样

getIntent().getStringExtra("key"); 

Study here知道更多关于意图

相关问题