android - Why does Apps Main Activity not take new values of variables after they are changed when the activity starts? -
hi know why though ive created methods change co-ordinate values (which executed before main method). main method still taking default values gave co-ordinate variables @ start of activity?
here code:
for first activity shown on app (start activity) allows change lat , long co-ordinates typing in alert dialog, starting 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 code main activity, contains methods called in start activity should change lat , lng values (but takes original lat , lng values every time 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() { (; 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 menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } }
i'm not sure if matter ill include manifest file , xml files both activities well:
here 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 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> here 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 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 appreciated :)
remove:
mainactivity longlat = new mainactivity();
you don't need create object of activity inside another. take user input value 1 activity , start activity using intent.
intent intent = new intent(currentactivity.this, newactivity.class); startactivity(intent);
if want pass data between them, can pass parameters new activity
using putextra
function.
intent.putextra("param1", paramvalue);
see how pass data between activities in android application? more.
Comments
Post a Comment