【问题标题】:Android app: If I change the code (and save it), when I run it, the app isn't changed at allAndroid 应用程序:如果我更改代码(并保存),当我运行它时,应用程序根本不会更改
【发布时间】:2014-01-15 22:35:10
【问题描述】:

我知道这个问题听起来非常愚蠢和基本,但基本上,我正在使用 Google Maps API 创建一个应用程序,我有一个自定义标记出现,然后我尝试让地理编码工作,但我遇到了麻烦所以我决定去掉那部分代码并重新开始,但现在标记没有出现。

然而,我意识到,无论我如何更改代码,当我运行应用程序时,它根本不会发生变化。我已经尝试卸载它并重新安装它,关闭 Eclipse 并重新打开它,当然我也尝试过谷歌它,但我没有找到任何东西。

这是我的 MainActivity(我目前唯一的活动)的代码:

public class MainActivity extends FragmentActivity 
{
  @Override
  protected void onCreate(Bundle savedInstanceState) 
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    GoogleMap map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

    LatLng ireland = new LatLng(53.0000, -8.0000);
    LatLng microsoftLatLng = new LatLng(53.276863, -6.211802);

    map.setMyLocationEnabled(true);
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(ireland, 6));

    map.addMarker(new MarkerOptions()
    .position(microsoftLatLng)
    .icon(BitmapDescriptorFactory.fromResource(R.drawable.green_briefcase_icon)));      
  }  
}

我认为问题与我的代码无关,我认为这可能是 Eclipse 的问题,但我不明白为什么会这样。

当我运行该应用程序时,它以所需的缩放级别 (6) 聚焦于爱尔兰,并具有 GPS 位置按钮,但无论我输入的纬度和经度如何,或者我是否摆脱了“map.setMyLocationEnabled” (true)",当我再次运行它时(即使我已经先卸载了它),或者即使我将所有代码一起删除并设置内容视图,它仍然以该缩放级别显示爱尔兰并且有 GPS 定位按钮,但没有标记(但我之前有这个确切的代码完美运行)。

这是我的布局文件:

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

        <fragment
                android:id="@+id/map"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                class="com.google.android.gms.maps.SupportMapFragment" />

    </RelativeLayout>

这是我的清单文件:

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

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

        <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.mapsexample.MainActivity"
                android:label="@string/app_name" >

                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>

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

            <meta-data
                android:name="com.google.android.gms.version"
                android:value="@integer/google_play_services_version" />
        </application>
    </manifest> 

【问题讨论】:

  • 发生这种情况时,通常 Project->Clean 会解决问题。

标签: android eclipse google-maps


【解决方案1】:

可能是因为您将相机移动到“爱尔兰”LatLng 对象?

map.moveCamera(CameraUpdateFactory.newLatLngZoom(ireland, 6));

我相信如果你想要当前位置,你必须使用位置服务并询问你的当前位置,像这样

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

//ask via gps
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
        myLocationListener);

//ask via the network providers
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0,
        0, myLocationListener);

由于位置将异步到达,因此您必须实现 LocationListener,这意味着实现:

public void onLocationChanged(Location location)

在那里,您想将相机移动到更改为的位置。

类似的东西。

【讨论】:

  • 我认为这不是问题,因为我之前使用此代码工作过,当它运行它以移动到“爱尔兰”LatLng 对象时我希望它做什么,但有一个标记在“microsoftLatLng”LatLng 对象上,然后在单击 GPS 按钮时移动到用户的当前位置,我已经完成了所有这些工作,但现在它不再显示标记了。问题是当我更改代码时应用程序没有改变,无论我将代码更改为什么,而不是我觉得奇怪的代码本身。
【解决方案2】:

首先,确保您的资源文件中没有错误,因为这会阻止R.java 重新生成。然后按照建议使用Project -&gt; Clean... 清理整个项目。另外,我假设你打开了Project -&gt; Build Automatically,对吗?

【讨论】:

  • 我在资源文件中看不到任何错误,我已对其进行了清理,但现在当我尝试运行它时出现错误“找不到 MapsExample.apk!”,并且是的,我已将其设置为自动构建。
  • 所以看起来自从你清理它之后,它从bin/ 中删除了之前成功的构建,所以这就是它再也找不到它的原因。您可以通过任何方式发布更多代码(布局、清单)或图片,我们可以看到罪魁祸首可能是什么?
  • 我现在把布局和清单文件放好,以防它们引起问题。
  • 您是否更改/更新了您正在编译的任何构建工具、Eclipse 版本、SDK?对 Google Play 服务的外部依赖项有任何更改吗?那些都编译好了吗?
  • 我没有故意改变这些东西,但我一定是不小心做了什么,我开始了一个新项目并复制了代码,现在一切都很好,我仍然不知道它是什么我是否改变了,但感谢您的建议。
【解决方案3】:

我决定创建一个新项目,复制代码并为该项目创建一个新的 API 密钥,一切都再次完美运行,当我运行应用程序时可以看到我所做的更改,所以一切都很好。我不知道问题是什么,但我希望新应用程序不会发生这种情况!感谢您的所有意见!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多