将LatLng发送到下一个活动,并将其值放在编辑文本框中

时间:2015-02-18 15:42:38

标签: android google-maps

我设置了项目,以便点击地图并添加标记,我现在想尝试将该标记的坐标发送到下一个活动,我可以填写更多有关位置的详细信息发送我的数据库。如果有人可以提供任何帮助,我将非常感激。但是,每当我尝试将LatLn坐标发送到下一个活动时,我都会收到错误:

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.homescreen/com.homescreen.ClubForm}: java.lang.NullPointerException

继承我的代码,将标记添加到地图中,工作正常:

public class Map extends ActionBarActivity{

private GoogleMap myMap;
String c_name;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map);

    myMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
            .getMap();
    // Initializing
    // Getting reference to Button
    Button btnDraw = (Button) findViewById(R.id.btn_route);
    Button btnAddClub = (Button) findViewById(R.id.btn_add_club);

    btnDraw.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // if club button is pressed start ClubLogin Activity
            Intent i = new Intent(getApplicationContext(), DrawRoute.class);

            startActivity(i);
        }
    });


     LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
     Criteria criteria = new Criteria();

     Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
     if (location != null)
     {

         myMap.setMyLocationEnabled(true);
         myMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
                 new LatLng(location.getLatitude(), location.getLongitude()), 13));

         CameraPosition cameraPosition = new CameraPosition.Builder()
         .target(new LatLng(location.getLatitude(), location.getLongitude()))      // Sets the center of the map to location user
         .zoom(6)                   // Sets the zoom
         .bearing(0)                // Sets the orientation of the camera to east
         .tilt(40)                   // Sets the tilt of the camera to 30 degrees
         .build();                   // Creates a CameraPosition from the builder
     myMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
     }
     btnAddClub.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             // Perform action on click
             myMap.setOnMapClickListener(new OnMapClickListener() {
                 @Override
                 public void onMapClick(LatLng latlng) {
                     addMarker(latlng, c_name);
                     Intent c = new Intent(getApplicationContext(), ClubForm.class);
                     c.putExtra("latlng", latlng);
                    startActivity(c);
                 }
             });
         }
     });

}

// Adding marker on the GoogleMaps
private void addMarker(LatLng latlng, String c_name) {
    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.position(latlng);
    markerOptions.title(c_name);
    markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.club_marker));
    myMap.addMarker(markerOptions);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

下面是我试图将latlng坐标发送到编辑文本框的表单的下一个活动:

public class ClubForm extends ActionBarActivity {

String c_name;
String c_email;
String c_address;
String c_phno;
String c_size;
InputStream is = null;
String result = null;
String line = null;
int code;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.club_form);

    Bundle bundle = getIntent().getExtras();

    if(bundle != null){

        LatLng = new GeoPoint((int)(bundle.getDouble("lat") / 1E6),(int)(bundle.getDouble("long")/1E6));
        animateToCurrentPoint(currentGeoPoint);

    }


    final EditText e_name = (EditText) findViewById(R.id.editbox2);
    final EditText e_email = (EditText) findViewById(R.id.editbox7);
    final EditText e_address = (EditText) findViewById(R.id.editbox3);
    final EditText e_phno = (EditText) findViewById(R.id.editbox6);
    final EditText e_size = (EditText) findViewById(R.id.editbox4);
    final EditText e_latlng = (EditText) findViewById(R.id.editbox8);
    Button submit = (Button) findViewById(R.id.btn_submit);

    submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            c_name = e_name.getText().toString();
            c_email = e_email.getText().toString();
            c_address = e_address.getText().toString();
            c_phno = e_phno.getText().toString();
            c_size = e_size.getText().toString();
            //latlng = e_latlng.getText().toString();
            //submit();
        }
    });
}

/*public void submit() {
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

    nameValuePairs.add(new BasicNameValuePair("c_name", c_name));
    nameValuePairs.add(new BasicNameValuePair("c_email", c_email));
    nameValuePairs.add(new BasicNameValuePair("c_address", c_address));
    nameValuePairs.add(new BasicNameValuePair("c_phno", c_phno));
    nameValuePairs.add(new BasicNameValuePair("c_size", c_size));
    nameValuePairs.add(new BasicNameValuePair("latlng", latlng));

    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("purposely wrong");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
        Log.e("pass 1", "connection success ");
    } catch (Exception e) {
        Log.e("Fail 1", e.toString());
        Toast.makeText(getApplicationContext(), "Invalid IP Address",
                Toast.LENGTH_LONG).show();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        result = sb.toString();
        Log.e("pass 2", "connection success ");
    } catch (Exception e) {
        Log.e("Fail 2", e.toString());
    }

    try {
        JSONObject json_data = new JSONObject(result);
        code = (json_data.getInt("code"));

        if (code == 1) {
            Toast.makeText(getBaseContext(), "Inserted Successfully",
                    Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getBaseContext(), "Sorry, Try Again",
                    Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Log.e("Fail 3", e.toString());
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}*/

}

1 个答案:

答案 0 :(得分:0)

下面

  

LatLng = new GeoPoint((int)(bundle.getDouble(&#34; lat&#34;)/   1E6),(INT)(bundle.getDouble(&#34;长&#34;)/ 1E6));

NPE因为bundle不包含latlong个密钥。

意图发送LatLng个对象。使用Intent.getParcelable来获取onCreate方法中的接收意图:

Intent intent = getIntent().getExtras();
if(intent !=null)
 LatLng latlng = intent.getParcelable("latlng");