私のコードは activity_show_location.xml です
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dip"
android:orientation="horizontal" >
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="5dip"
android:text="@string/lat"
android:textSize="20sp" >
</TextView>
<TextView
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/unkwn"
android:textSize="20sp" >
</TextView>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="5dip"
android:text="@string/lon"
android:textSize="20sp" >
</TextView>
<TextView
android:id="@+id/TextView04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/unkwn"
android:textSize="20sp" >
</TextView>
</LinearLayout>
</LinearLayout>
ShowLocationActivity.java
public class ShowLocationActivity extends Activity implements LocationListener {
private TextView latitudeField;
private TextView longitudeField;
private LocationManager locationManager;
private String provider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_location);
latitudeField = (TextView) findViewById(R.id.TextView02);
longitudeField = (TextView) findViewById(R.id.TextView04);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Activity a;
new Thread(new Runnable() {
@Override
public void run() {
Location location = locationManager
.getLastKnownLocation(provider);
if (location != null) {
System.out.println("Provider " + provider
+ " has been selected.");
onLocationChanged(location);
} else {
latitudeField.setText("Location not available.");
longitudeField.setText("Location not available.");
}
}
}).start();
}
@Override
public void onLocationChanged(Location location) {
int lat = (int) (location.getLatitude());
int lng = (int) (location.getLongitude());
latitudeField.setText(String.valueOf(lat));
longitudeField.setText(String.valueOf(lng));
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(this, "Disabled provider " + provider,
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(this, "Enabled new provider " + provider,
Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) { // TODO
// Auto-generated
// method
// stub
}
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider, 400, 1, this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this
// adds items to the action
// bar if it is present.
getMenuInflater().inflate(R.menu.activity_show_location, menu);
return true;
}
}
ADT Eclipse で実行すると、logcat が表示されます
これは私のLogCatショーです:
03-20 07:44:56.383: W/Trace(871): Unexpected value from nativeGetEnabledTags: 0
03-20 07:44:56.383: W/Trace(871): Unexpected value from nativeGetEnabledTags: 0
03-20 07:44:56.783: W/Trace(871): Unexpected value from nativeGetEnabledTags: 0
03-20 07:44:56.783: W/Trace(871): Unexpected value from nativeGetEnabledTags: 0
03-20 07:44:57.343: D/AndroidRuntime(871): Shutting down VM
03-20 07:44:57.343: W/dalvikvm(871): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
03-20 07:44:57.373: E/AndroidRuntime(871): FATAL EXCEPTION: main
03-20 07:44:57.373: E/AndroidRuntime(871): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.locationapi/com.example.locationapi.ShowLocationActivity}: java.lang.IllegalArgumentException: invalid provider: null
03-20 07:44:57.373: E/AndroidRuntime(871): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-20 07:44:57.373: E/AndroidRuntime(871): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-20 07:44:57.373: E/AndroidRuntime(871): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-20 07:44:57.373: E/AndroidRuntime(871): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-20 07:44:57.373: E/AndroidRuntime(871): at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 07:44:57.373: E/AndroidRuntime(871): at android.os.Looper.loop(Looper.java:137) 03-20 07:44:57.373: E/AndroidRuntime(871): at android.app.ActivityThread.main(ActivityThread.java:5039)
03-20 07:44:57.373: E/AndroidRuntime(871): at java.lang.reflect.Method.invokeNative(Native Method)
03-20 07:44:57.373: E/AndroidRuntime(871): at java.lang.reflect.Method.invoke(Method.java:511)
03-20 07:44:57.373: E/AndroidRuntime(871): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-20 07:44:57.373: E/AndroidRuntime(871): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-20 07:44:57.373: E/AndroidRuntime(871): at dalvik.system.NativeStart.main(Native Method) 03-20 07:44:57.373: E/AndroidRuntime(871): Caused by: java.lang.IllegalArgumentException: invalid provider: null
03-20 07:44:57.373: E/AndroidRuntime(871): at android.location.LocationManager.checkProvider(LocationManager.java:1594)
03-20 07:44:57.373: E/AndroidRuntime(871): at android.location.LocationManager.getLastKnownLocation(LocationManager.java:1138)
03-20 07:44:57.373: E/AndroidRuntime(871): at com.example.locationapi.ShowLocationActivity.onCreate(ShowLocationActivity.java:32)
03-20 07:44:57.373: E/AndroidRuntime(871): at android.app.Activity.performCreate(Activity.java:5104)
03-20 07:44:57.373: E/AndroidRuntime(871): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-20 07:44:57.373: E/AndroidRuntime(871): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-20 07:44:57.373: E/AndroidRuntime(871): ... 11 more
03-20 07:45:39.193: I/Process(871): Sending signal. PID: 871 SIG: 9
そして、エミュレーターが「残念ながらLocationAPIが停止しました」として実行されたときに取得しました。解決策を教えてください。