0

Hi does anyone know why even though Ive created methods which change some co-ordinate values (which are executed before my main method). My Main method is still taking the default values i gave my co-ordinate variables at the start of the Activity?

Here is my code:

For the first Activity Shown on the App (Start Activity) which allows you to change the lat and long co-ordinates by typing in an alert dialog, as well as starting up the main Map Activity:

    public class StartViewActivity extends Activity {

MainActivity longlat = new MainActivity();

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

    Button button = (Button) findViewById(R.id.button1);
    Button button1 = (Button) findViewById(R.id.button2);
    Button button2 = (Button) findViewById(R.id.button3);

    button.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            AlertDialog.Builder alert = new AlertDialog.Builder(StartViewActivity.this);
            alert.setTitle("Longitude");
            alert.setMessage("Input Longitude");

            final EditText input = new EditText(StartViewActivity.this);
            alert.setView(input);

            alert.setPositiveButton( "Continue..", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int which) {
                            String lng1 = input.getText().toString();
                            longlat.lngSetter(lng1);
                        }
                    });
            alert.show();

        }

    });

    button1.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            AlertDialog.Builder alert = new AlertDialog.Builder(StartViewActivity.this);
            alert.setTitle("Latitude");
            alert.setMessage("Input Latitude");

            final EditText input = new EditText(StartViewActivity.this);
            alert.setView(input);

            alert.setPositiveButton( "Continue..", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            String lat1 = input.getText().toString();
                            longlat.latSetter(lat1);
                        }
                    });
            alert.show();
        }});

        button2.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                Intent intent = new    Intent(StartViewActivity.this,MainActivity.class);
                startActivity(intent);

            }});
        }}

This is the code from my Main Activity, which also contains the methods called in the Start Activity that should change my lat and lng values (but takes the original lat and lng values every time the Activity starts) :

    package com.example.workinggooglemaps;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;

public class MainActivity extends Activity {

double[] tokens = new double[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
        13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
        30, 31 };
private double lat = 50.597636;
private double lng = -2.447766;
private GoogleMap theMap;
private Random number = new Random();
private int counter = 0;


public void lngSetter(String f){
    lng = Double.parseDouble(f); 
    System.out.println(lng);
    }
public void latSetter(String g){
    lat = Double.parseDouble(g); 
    System.out.println(lat);
    }

public void polyLineDrawer() {

    theMap.addPolyline(new PolylineOptions()
            .add(new LatLng(tokens[0], tokens[1]))
            .add(new LatLng(tokens[2], tokens[3]))
            .add(new LatLng(tokens[4], tokens[5]))
            .add(new LatLng(tokens[6], tokens[7]))
            .add(new LatLng(tokens[8], tokens[9]))
            .add(new LatLng(tokens[10], tokens[11]))
            .add(new LatLng(tokens[12], tokens[13]))
            .add(new LatLng(tokens[14], tokens[15]))
            .add(new LatLng(tokens[16], tokens[17]))
            .add(new LatLng(tokens[18], tokens[19]))
            .add(new LatLng(tokens[20], tokens[21]))
            .add(new LatLng(tokens[22], tokens[23]))
            .add(new LatLng(tokens[24], tokens[25]))
            .add(new LatLng(tokens[26], tokens[27]))
            .add(new LatLng(tokens[28], tokens[29])).width(3)
            .color(0xffff0000).zIndex(0));

}

public void Drawer() {
    for (; counter < 30; counter = counter + 2)
        tokens[0] = (((number.nextDouble()) / 250) + lat) - 0.002;
    tokens[1] = (((number.nextDouble()) / 250) + lng) - 0.002;
    tokens[2] = (((number.nextDouble()) / 250) + lat) - 0.002;
    tokens[3] = (((number.nextDouble()) / 250) + lng) - 0.002;
    tokens[4] = (((number.nextDouble()) / 250) + lat) - 0.002;
    tokens[5] = (((number.nextDouble()) / 250) + lng) - 0.002;
    tokens[6] = (((number.nextDouble()) / 250) + lat) - 0.002;
    tokens[7] = (((number.nextDouble()) / 250) + lng) - 0.002;
    tokens[8] = (((number.nextDouble()) / 250) + lat) - 0.002;
    tokens[9] = (((number.nextDouble()) / 250) + lng) - 0.002;
    tokens[10] = (((number.nextDouble()) / 250) + lat) - 0.002;
    tokens[11] = (((number.nextDouble()) / 250) + lng) - 0.002;
    tokens[12] = (((number.nextDouble()) / 250) + lat) - 0.002;
    tokens[13] = (((number.nextDouble()) / 250) + lng) - 0.002;
    tokens[14] = (((number.nextDouble()) / 250) + lat) - 0.002;
    tokens[15] = (((number.nextDouble()) / 250) + lng) - 0.002;
    tokens[16] = (((number.nextDouble()) / 250) + lat) - 0.002;
    tokens[17] = (((number.nextDouble()) / 250) + lng) - 0.002;
    tokens[18] = (((number.nextDouble()) / 250) + lat) - 0.002;
    tokens[19] = (((number.nextDouble()) / 250) + lng) - 0.002;
    tokens[20] = (((number.nextDouble()) / 250) + lat) - 0.002;
    tokens[21] = (((number.nextDouble()) / 250) + lng) - 0.002;
    tokens[22] = (((number.nextDouble()) / 250) + lat) - 0.002;
    tokens[23] = (((number.nextDouble()) / 250) + lng) - 0.002;
    tokens[24] = (((number.nextDouble()) / 250) + lat) - 0.002;
    tokens[25] = (((number.nextDouble()) / 250) + lng) - 0.002;
    tokens[26] = (((number.nextDouble()) / 250) + lat) - 0.002;
    tokens[27] = (((number.nextDouble()) / 250) + lng) - 0.002;
    tokens[28] = (((number.nextDouble()) / 250) + lat) - 0.002;
    tokens[29] = (((number.nextDouble()) / 250) + lng) - 0.002;
}


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (theMap == null) {
        theMap = ((MapFragment) getFragmentManager().findFragmentById(
                R.id.the_map)).getMap();
    }
    if (theMap != null) {

    }

    LatLng co_ords = new LatLng(lat, lng);
    theMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    theMap.addCircle(new CircleOptions().center(co_ords).radius(200)
            .strokeWidth(3).zIndex(1));
    theMap.addCircle(new CircleOptions().center(co_ords).radius(150)
            .strokeWidth(3).zIndex(1));
    theMap.addCircle(new CircleOptions().center(co_ords).radius(100)
            .strokeWidth(3).zIndex(1));
    theMap.addCircle(new CircleOptions().center(co_ords).radius(50)
            .strokeWidth(3).zIndex(1));
    theMap.addMarker(new MarkerOptions().position(co_ords)
            .title("Cerberus"));
    theMap.animateCamera(CameraUpdateFactory.newLatLngZoom(co_ords, 17));
    Drawer();
    polyLineDrawer();
}

@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;
}

}

I'm not sure if it will matter but ill include my Manifest file and my XML files for both activities as well:

Here is my Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.workinggooglemaps"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="17" />

    <permission
        android:name="com.example.workinggooglemaps.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.example.workinggooglemaps.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.workinggooglemaps.StartViewActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.workinggooglemaps.MainActivity"
            android:label="@string/app_name" >

         </activity>
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyAgtM2ZtsmEveqylJDvNkosyxAdLu2j_rc" />
    </application>

</manifest>

Here is my Start Activity XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_alignRight="@+id/button1"
        android:layout_below="@+id/button1"
        android:text="Set Latitude" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="72dp"
        android:text="Set Longitude" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/button2"
        android:layout_marginBottom="83dp"
        android:text="Show on Map" />

</RelativeLayout>

Finally here is my Main Activity XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <fragment
        android:id="@+id/the_map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.49" />

</LinearLayout>

Here is my Main Activity XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <fragment
        android:id="@+id/the_map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.49" />

</LinearLayout>

Any help would be greatly appreciated :)

user2682570
  • 74
  • 1
  • 2
  • 10
  • An activity inside an activity? I'm not developing android since a long time ago, but I don't remember that being a topic in android's activities lifecycle. I left in 2.2, but you should `Intent` a launch instead of holding one. – Sebastian Aug 28 '13 at 10:37
  • Yeeeep that was a problem :L – user2682570 Aug 29 '13 at 13:20

1 Answers1

0

Remove:

MainActivity longlat = new MainActivity();

You don't need to create an object of an Activity inside another. You take the user input value from one activity and start another activity using Intent.

Intent intent = new Intent(CurrentActivity.this, NewActivity.class);
startActivity(intent);

If you want to pass data between them, you can pass extra parameters to the new Activity using putExtra function.

intent.putExtra("param1", paramValue);

See How do I pass data between Activities in Android application? for more.

Community
  • 1
  • 1
Shobhit Puri
  • 24,785
  • 8
  • 89
  • 115
  • So i pass through two new variables that would become the new lat and lng as in just two new variables that can be referenced in the new activity. Or do i give them the same ID as the already present lat and lng variables, so these variables are replaced with new values? – user2682570 Aug 28 '13 at 11:34
  • The way it goes is that, you'll collect the two variables in your first activity. Then you'll pass them to the new activity using `putExtra`. You start a new Intent and pass these values, you can extract them in `onCreate` of your new activity like http://stackoverflow.com/questions/4233873/how-to-get-extra-data-from-intent-in-android. The value of the variables will be overwritten in the new activity each time you start a new Intent. – Shobhit Puri Aug 28 '13 at 11:46
  • ok Ive followed the advice on the page you sent me to and it still doesn't work. I created a new intent got the intent from the activity that started the intent, then used getDoubleExtra() method to try and recieve the doubles but it goes straight to the default value any reason why do you think? – user2682570 Aug 28 '13 at 13:11
  • ive got it working now cheers i had the put extra after the activity started :L – user2682570 Aug 29 '13 at 12:39