0

嗨,有谁知道为什么即使我创建了改变一些坐标值的方法(在我的主要方法之前执行)。我的 Main 方法仍然采用我在活动开始时给我的坐标变量的默认值?

这是我的代码:

对于应用程序上显示的第一个活动(开始活动),它允许您通过在警报对话框中键入来更改纬度和经度坐标,以及启动主地图活动:

    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);

            }});
        }}

这是来自我的 Main Activity 的代码,它还包含在 Start Activity 中调用的方法,这些方法应该更改我的 lat 和 lng 值(但每次 Activity 启动时都会采用原始​​ lat 和 lng 值):

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

}

我不确定这是否重要,但我的清单文件和我的 XML 文件也包含在这两个活动中:

这是我的清单:

<?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>

这是我的开始活动 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>

这是我的主要活动 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>

任何帮助将不胜感激 :)

4

1 回答 1

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.

于 2013-08-28T10:38:22.677 回答