0

例からこのコードを試しましたが、onResume() で例外が発生しました。何が問題なのか理解できませんか?誰でもこれについて提案できますか?

私のコード

public class MainActivity extends Activity 
        implements LocationSource, LocationListener{

final int RQS_GooglePlayServices = 1;
private GoogleMap myMap;
TextView tvLocInfo;

LocationManager myLocationManager = null;
OnLocationChangedListener myLocationListener = null;
Criteria myCriteria;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tvLocInfo = (TextView)findViewById(R.id.locinfo);

    FragmentManager myFragmentManager = getFragmentManager();
    MapFragment myMapFragment 
        = (MapFragment)myFragmentManager.findFragmentById(R.id.map);
    myMap = myMapFragment.getMap();

    myMap.setMyLocationEnabled(true);

    myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

    myCriteria = new Criteria();
    myCriteria.setAccuracy(Criteria.ACCURACY_FINE);
    myLocationManager = (LocationManager)getSystemService(LOCATION_SERVICE);

}

@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_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_legalnotices:
        String LicenseInfo = GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(
                getApplicationContext());
        AlertDialog.Builder LicenseDialog = new AlertDialog.Builder(MainActivity.this);
        LicenseDialog.setTitle("Legal Notices");
        LicenseDialog.setMessage(LicenseInfo);
        LicenseDialog.show();
        return true;    
    }
    return super.onOptionsItemSelected(item);   
}

@Override
protected void onResume() {
    super.onResume();

    int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());

    if (resultCode == ConnectionResult.SUCCESS){
        Toast.makeText(getApplicationContext(), 
                "isGooglePlayServicesAvailable SUCCESS", 
                Toast.LENGTH_LONG).show();

        //Register for location updates using a Criteria, and a callback on the specified looper thread.
        myLocationManager.requestLocationUpdates(
                0L,             //minTime
                0.0f,           //minDistance
                myCriteria,     //criteria
                this,           //listener
                null);          //looper

        //Replaces the location source of the my-location layer.
        myMap.setLocationSource(this);

    }else{
        GooglePlayServicesUtil.getErrorDialog(resultCode, this, RQS_GooglePlayServices);    
    }

}

@Override
protected void onPause() {
    myMap.setLocationSource(null);
    myLocationManager.removeUpdates(this);

    super.onPause();
}

@Override
public void activate(OnLocationChangedListener listener) {
    myLocationListener = listener;
}

@Override
public void deactivate() {
    myLocationListener = null;
}

@Override
public void onLocationChanged(Location location) {
    if (myLocationListener != null) {
        myLocationListener.onLocationChanged(location);

        double lat = location.getLatitude();
        double lon = location.getLongitude();

        tvLocInfo.setText(
                "lat: " + lat + "\n" +
                "lon: " + lon);
    }
}

@Override
public void onProviderDisabled(String arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    // TODO Auto-generated method stub

}

}

Logcat ビュー

02-12 10:56:34.907: W/dalvikvm(1986): threadid=1: thread exiting with uncaught exception (group=0x40e55258)
02-12 10:56:34.922: E/AndroidRuntime(1986): FATAL EXCEPTION: main
02-12 10:56:34.922: E/AndroidRuntime(1986): java.lang.RuntimeException: Unable to resume activity {com.example.androidmapsv2/com.example.androidmapsv2.MainActivity}: java.lang.IllegalArgumentException: no provider_s found for criteria
02-12 10:56:34.922: E/AndroidRuntime(1986):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2491)
02-12 10:56:34.922: E/AndroidRuntime(1986):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2519)
02-12 10:56:34.922: E/AndroidRuntime(1986):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2033)
02-12 10:56:34.922: E/AndroidRuntime(1986):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
02-12 10:56:34.922: E/AndroidRuntime(1986):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1179)
02-12 10:56:34.922: E/AndroidRuntime(1986):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-12 10:56:34.922: E/AndroidRuntime(1986):     at android.os.Looper.loop(Looper.java:137)
02-12 10:56:34.922: E/AndroidRuntime(1986):     at android.app.ActivityThread.main(ActivityThread.java:4508)
02-12 10:56:34.922: E/AndroidRuntime(1986):     at java.lang.reflect.Method.invokeNative(Native Method)
02-12 10:56:34.922: E/AndroidRuntime(1986):     at java.lang.reflect.Method.invoke(Method.java:511)
02-12 10:56:34.922: E/AndroidRuntime(1986):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
02-12 10:56:34.922: E/AndroidRuntime(1986):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
02-12 10:56:34.922: E/AndroidRuntime(1986):     at dalvik.system.NativeStart.main(Native Method)
02-12 10:56:34.922: E/AndroidRuntime(1986): Caused by: java.lang.IllegalArgumentException: no provider_s found for criteria
02-12 10:56:34.922: E/AndroidRuntime(1986):     at android.os.Parcel.readException(Parcel.java:1331)
02-12 10:56:34.922: E/AndroidRuntime(1986):     at android.os.Parcel.readException(Parcel.java:1281)
02-12 10:56:34.922: E/AndroidRuntime(1986):     at android.location.ILocationManager$Stub$Proxy.requestLocationUpdates(ILocationManager.java:671)
02-12 10:56:34.922: E/AndroidRuntime(1986):     at android.location.LocationManager._requestLocationUpdates(LocationManager.java:582)
02-12 10:56:34.922: E/AndroidRuntime(1986):     at android.location.LocationManager.requestLocationUpdates(LocationManager.java:563)
02-12 10:56:34.922: E/AndroidRuntime(1986):     at com.example.androidmapsv2.MainActivity.onResume(MainActivity.java:88)
02-12 10:56:34.922: E/AndroidRuntime(1986):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1159)
02-12 10:56:34.922: E/AndroidRuntime(1986):     at android.app.Activity.performResume(Activity.java:4553)
02-12 10:56:34.922: E/AndroidRuntime(1986):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2481)
02-12 10:56:34.922: E/AndroidRuntime(1986):     ... 12 more
4

1 に答える 1

0

requestLocationUpdatesmethodの宣言を見つけることができる Android-Developers ページを参照する必要があります。

public void requestLocationUpdates (long minTime, float minDistance, Criteria criteria, LocationListener listener, Looper looper)

そこで、メソッドがスローする例外を確認できます

Throws
    IllegalArgumentException    if criteria is null
    IllegalArgumentException    if listener is null
    SecurityException   if no suitable permission is present

今あなたのコードを見ると

    myLocationManager.requestLocationUpdates(
            0L,             //minTime
            0.0f,           //minDistance
            myCriteria,     //criteria
            this,           //listener
            null);          //looper

そしてあなたの例外

E/AndroidRuntime: java.lang.RuntimeException: Unable to resume activity {...}: 
        java.lang.IllegalArgumentException: no provider_s found for criteria
                   ^^^^^^^ this                              and this  ^^^^

としても

at com.example.androidmapsv2.MainActivity.onResume(MainActivity.java:88)

88 行目で がスローされていることがわかります。IllegalArgumentExceptionこの理由criteriaは、 が呼び出されたnullときrequestLocationUpdatesです。

inの初期化だけcriteriaOnCreateは不十分です。が呼び出されたら、インスタンス化さOnResumeれていることを確認するために null チェックを行う必要があります。criteria

于 2013-02-21T23:45:16.943 に答える