設定ボタンを押して「マップにスワップ」をクリックすると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