0

我正在尝试制作一个应用程序,当您点击设置按钮并单击“切换到地图”时将切换到 MapView 但是每次强制关闭时。我似乎无法弄清楚问题是什么。我的老师没有解释如何做这一切,并告诉我们我们可以在互联网上弄清楚......她很好,对吗?

这是似乎有问题的班级。

  package com.example.zip.code;

import android.app.Activity;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.google.android.maps.MapView;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;

public class OpenMap extends Activity {
    private MapController mapController;
     private LocationManager locationManager;
      private String provider;
      double lat;
      double lng;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.maplayout);

        Criteria criteria = new Criteria();
        provider = locationManager.getBestProvider(criteria, false);
        Location location = locationManager.getLastKnownLocation(provider);

        // Get Mapping Controllers etc
        MapView mapView = (MapView) findViewById(R.id.map_view);
        mapController = mapView.getController();

        // Center on Current Position
        mapController.setCenter(new GeoPoint((int) (location.getLatitude() * 1E6),
                                                  (int) (location.getLongitude() * 1E6)));
        mapController.setZoom(11);
        mapView.setBuiltInZoomControls(true);

    }

    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          MenuInflater inflater = getMenuInflater();
          inflater.inflate(R.menu.mapsettings, menu);
          return true;
      }

      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle item selection
          switch (item.getItemId()) {
              case R.id.zipswap:
                  swapToZip();
                  return true;
              default:
                  return super.onOptionsItemSelected(item);
          }
      }

      public void swapToZip(){
          Intent i = new Intent(getApplicationContext(), MainActivity.class);
          startActivity(i);
      }
}

这是相应的 XML 文件。

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

    <com.google.android.maps.MapView
        android:id="@+id/map_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:enabled="true"
        android:clickable="true"
        android:apiKey="ADD YOUR API KEY HERE"
        />

    <LinearLayout android:id="@+id/zoom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        /> 

</RelativeLayout>

这是导致问题的类的类。包 com.example.zip.code;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements LocationListener {

     private TextView latituteField;
      private TextView longitudeField;
      private TextView zipField;
      private TextView cityField;
      private LocationManager locationManager;
      private String provider;
      double lat;
      double lng;

      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        latituteField = (TextView) findViewById(R.id.TextView02);
        longitudeField = (TextView) findViewById(R.id.TextView04);
        zipField = (TextView) findViewById(R.id.textView6);
        cityField = (TextView) findViewById(R.id.textView2);

        // Get the location manager
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        // Define the criteria how to select the locatioin provider -> use
        // default
        Criteria criteria = new Criteria();
        provider = locationManager.getBestProvider(criteria, false);
        Location location = locationManager.getLastKnownLocation(provider);

        // Initialize the location fields
        if (location != null) {
          System.out.println("Provider " + provider + " has been selected.");
          onLocationChanged(location);
        } else {
          latituteField.setText("Location not available");
          longitudeField.setText("Location not available");
        }


      }

      /* Request updates at startup */
      @Override
      protected void onResume() {
        super.onResume();
        locationManager.requestLocationUpdates(provider, 400, 1, this);
      }

      /* Remove the locationlistener updates when Activity is paused */
      @Override
      protected void onPause() {
        super.onPause();
        locationManager.removeUpdates(this);
      }

      @Override
      public void onLocationChanged(Location location) {
        lat = (location.getLatitude());
        lng = (location.getLongitude());
        latituteField.setText(String.valueOf(lat));
        longitudeField.setText(String.valueOf(lng));
        Geocoder geocoder = new Geocoder(this, Locale.getDefault());
        try {
            List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
            if (addresses != null && !addresses.isEmpty()) {
                Address address = addresses.get(0);
                String zip = address.getPostalCode();
                String city = address.getLocality();
                zipField.setText(zip);
                cityField.setText(city);

            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
      }

      @Override
      public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

      }

      @Override
      public void onProviderEnabled(String provider) {
        Toast.makeText(this, "Enabled new provider " + provider,
            Toast.LENGTH_SHORT).show();

      }

      @Override
      public void onProviderDisabled(String provider) {
        Toast.makeText(this, "Disabled provider " + provider,
            Toast.LENGTH_SHORT).show();
      }

      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          MenuInflater inflater = getMenuInflater();
          inflater.inflate(R.menu.zipsettings, menu);
          return true;
      }

      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle item selection
          switch (item.getItemId()) {
              case R.id.mapswap:
                  System.out.println("Derp");
                  swapToMap();
                  return true;
              default:
                  return super.onOptionsItemSelected(item);
          }
      }

      public void swapToMap(){
          Intent i = new Intent(getApplicationContext(), OpenMap.class);
          startActivity(i);
      }

}

这就是第一类对应的XML。

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="16dp"
        android:text="@string/current"
        android:textColor="@color/Blue"
        android:textSize="@dimen/Header" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:text="unknown"
        android:textColor="@color/Blue"
        android:textSize="@dimen/Text" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="48dp"
        android:text="@string/gps"
        android:textColor="@color/Red"
        android:textSize="@dimen/Header" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="33dp"
        android:text="unknown"
        android:textColor="@color/Green"
        android:textSize="@dimen/Text" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView6"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="20dp"
        android:text="@string/zip"
        android:textColor="@color/Green"
        android:textSize="@dimen/Header" />

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView3"
        android:layout_marginTop="25dp"
        android:text="Latitude: "
        android:textColor="@color/Red"
        android:textSize="20dip" />

    <TextView
        android:id="@+id/TextView03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/TextView01"
        android:layout_below="@+id/TextView01"
        android:layout_marginTop="51dp"
        android:text="Longitute: "
        android:textColor="@color/Red"
        android:textSize="20dip" />

    <TextView
        android:id="@+id/TextView02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/TextView01"
        android:layout_alignBottom="@+id/TextView01"
        android:layout_alignLeft="@+id/textView6"
        android:text="unknown"
        android:textColor="@color/Red"
        android:textSize="20dip" />

    <TextView
        android:id="@+id/TextView04"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/TextView03"
        android:layout_alignBottom="@+id/TextView03"
        android:layout_alignLeft="@+id/TextView02"
        android:text="unknown"
        android:textColor="@color/Red"
        android:textSize="20dip" />

</RelativeLayout>

这是我在手机上调试的 LogCat。

  10-25 23:00:05.610: I/System.out(28034): Provider network has been selected.
    10-25 23:00:06.438: D/libEGL(28034): loaded /system/lib/egl/libGLES_android.so
    10-25 23:00:06.438: D/libEGL(28034): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
    10-25 23:00:06.446: D/libEGL(28034): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
    10-25 23:00:06.453: D/libEGL(28034): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
    10-25 23:00:06.711: D/OpenGLRenderer(28034): Enabling debug mode 0
    10-25 23:00:13.125: I/System.out(28034): Derp
    10-25 23:00:13.321: E/dalvikvm(28034): Could not find class 'com.google.android.maps.MapView', referenced from method com.example.zip.code.OpenMap.onCreate
    10-25 23:00:13.321: W/dalvikvm(28034): VFY: unable to resolve check-cast 438 (Lcom/google/android/maps/MapView;) in Lcom/example/zip/code/OpenMap;
    10-25 23:00:13.321: D/dalvikvm(28034): VFY: replacing opcode 0x1f at 0x002b
    10-25 23:00:13.321: D/dalvikvm(28034): DexOpt: unable to opt direct call 0x0c01 at 0x43 in Lcom/example/zip/code/OpenMap;.onCreate
    10-25 23:00:13.328: D/AndroidRuntime(28034): Shutting down VM
    10-25 23:00:13.328: W/dalvikvm(28034): threadid=1: thread exiting with uncaught exception (group=0x40aad210)
    10-25 23:00:13.336: E/AndroidRuntime(28034): FATAL EXCEPTION: main
    10-25 23:00:13.336: E/AndroidRuntime(28034): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.zip.code/com.example.zip.code.OpenMap}: android.view.InflateException: Binary XML file line #6: Error inflating class com.google.android.maps.MapView
    10-25 23:00:13.336: E/AndroidRuntime(28034):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2049)
    10-25 23:00:13.336: E/AndroidRuntime(28034):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2083)
    10-25 23:00:13.336: E/AndroidRuntime(28034):    at android.app.ActivityThread.access$600(ActivityThread.java:134)
    10-25 23:00:13.336: E/AndroidRuntime(28034):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1233)
    10-25 23:00:13.336: E/AndroidRuntime(28034):    at android.os.Handler.dispatchMessage(Handler.java:99)
    10-25 23:00:13.336: E/AndroidRuntime(28034):    at android.os.Looper.loop(Looper.java:137)
    10-25 23:00:13.336: E/AndroidRuntime(28034):    at android.app.ActivityThread.main(ActivityThread.java:4697)
    10-25 23:00:13.336: E/AndroidRuntime(28034):    at java.lang.reflect.Method.invokeNative(Native Method)
    10-25 23:00:13.336: E/AndroidRuntime(28034):    at java.lang.reflect.Method.invoke(Method.java:511)
    10-25 23:00:13.336: E/AndroidRuntime(28034):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
    10-25 23:00:13.336: E/AndroidRuntime(28034):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
    10-25 23:00:13.336: E/AndroidRuntime(28034):    at dalvik.system.NativeStart.main(Native Method)
    10-25 23:00:13.336: E/AndroidRuntime(28034): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class com.google.android.maps.MapView
    10-25 23:00:13.336: E/AndroidRuntime(28034):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
    10-25 23:00:13.336: E/AndroidRuntime(28034):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
    10-25 23:00:13.336: E/AndroidRuntime(28034):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
    10-25 23:00:13.336: E/AndroidRuntime(28034):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
    10-25 23:00:13.336: E/AndroidRuntime(28034):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
    10-25 23:00:13.336: E/AndroidRuntime(28034):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:255)
    10-25 23:00:13.336: E/AndroidRuntime(28034):    at android.app.Activity.setContentView(Activity.java:1879)
    10-25 23:00:13.336: E/AndroidRuntime(28034):    at com.example.zip.code.OpenMap.onCreate(OpenMap.java:29)
    10-25 23:00:13.336: E/AndroidRuntime(28034):    at android.app.Activity.performCreate(Activity.java:4539)
    10-25 23:00:13.336: E/AndroidRuntime(28034):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
    10-25 23:00:13.336: E/AndroidRuntime(28034):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2013)
    10-25 23:00:13.336: E/AndroidRuntime(28034):    ... 11 more
    10-25 23:00:13.336: E/AndroidRuntime(28034): Caused by: java.lang.ClassNotFoundException: com.google.android.maps.MapView
    10-25 23:00:13.336: E/AndroidRuntime(28034):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
    10-25 23:00:13.336: E/AndroidRuntime(28034):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
    10-25 23:00:13.336: E/AndroidRuntime(28034):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
    10-25 23:00:13.336: E/AndroidRuntime(28034):    at android.view.LayoutInflater.createView(LayoutInflater.java:552)
    10-25 23:00:13.336: E/AndroidRuntime(28034):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
    10-25 23:00:13.336: E/AndroidRuntime(28034):    ... 21 more
4

4 回答 4

0

如果您尝试使用模拟器运行您的应用程序,请检查您是否已经创建了模拟器的目标,Google's Api如果不只是创建一个新Android Virtual Deice (AVD)的目标为Google's Api否则,您将无法MapView在您的模拟器中运行您的相关应用程序。对于前 -

图片 http://intrepidusgroup.com/insight/wp-content/uploads/2011/08/Screen-Shot-2011-08-24-at-9.45.55-AM.png

为您生成API密钥MapView您可以在下面的第二个教程链接中了解如何创建 api 密钥。并且,确保您在类中使用MapView组件的位置,您必须扩展该类,MapActivity而不是Activity不要忘记在清单文件中添加以下内容 -

<uses-library android:name="com.google.android.maps"/>

上面一个是给你的mapView图书馆的。并且,以下一项是互联网许可

<uses-permission android:name="android.permission.INTERNET"/>

无论如何看看下面的教程。它可以帮助你——

  1. 地图视图教程

  2. 如何在安卓中使用谷歌地图

于 2012-10-26T05:22:26.940 回答
0
  1. 扩展MapActivity而不是Activity

  2. 在 Manifest 中,只需检查您是否实现了以下几行,

    < 使用库 android:name="com.google.android.maps" />

    并且您需要授予 INTERNET 权限

    < 使用权限 android:name="android.permission.INTERNET"/>

  3. 另一个是MapKey,

从谷歌获取您MapKey的调试证书MD5 API key

于 2012-10-26T04:59:27.063 回答
0

只需将以下行添加到您的清单中..它可能有效

<uses-library android:name="com.google.android.maps"/>

从您的 CML 看来,您似乎还没有添加 API 密钥。

<com.google.android.maps.MapView
    android:id="@+id/map_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:enabled="true"
    android:clickable="true"
    android:apiKey="ADD YOUR API KEY HERE"             // here your API key which is provided by google map to you
    />
于 2012-10-26T05:02:18.373 回答
0

请检查您的 manifest.xml 文件。如果您没有在应用程序中添加使用用户库文件,那么您可以在应用程序清单文件中添加此用户库

像这样

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
    android:name=".MainActivity"
    android:label="@string/title_activity_main" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
 </activity>
<activity android:name="com.android.calendar.AgendaActivity"></activity>
<uses-library android:name="com.google.android.maps"/>
</application>
于 2012-10-26T04:48:19.247 回答