无法获得位置

时间:2013-09-18 12:21:38

标签: android location

这是链接https://developer.android.com/training/location/retrieve-current.html上的代码,尽管它与我收到错误的代码相同。我获得了清单文件的权限。谢谢......

public class MainActivity extends FragmentActivity implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener, OnClickListener{
// Global constants
/*
 * Define a request code to send to Google Play services
 * This code is returned in Activity.onActivityResult
 */
 Location mCurrentLocation;

private final static int
        CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
LocationClient mLocationClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button b = (Button) findViewById(R.id.button1);
    b.setOnClickListener(this);
    /*
     * Create a new location client, using the enclosing class to
     * handle callbacks.
     */
    mLocationClient = new LocationClient(this, this, this);
}

// Define a DialogFragment that displays the error dialog
public static class ErrorDialogFragment extends DialogFragment {
    // Global field to contain the error dialog
    private Dialog mDialog;
    // Default constructor. Sets the dialog field to null
    public ErrorDialogFragment() {
        super();
        mDialog = null;
    }
    // Set the dialog to display
    public void setDialog(Dialog dialog) {
        mDialog = dialog;
    }
    // Return a Dialog to the DialogFragment.
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return mDialog;
    }
}
/*
 * Handle results returned to the FragmentActivity
 * by Google Play services
 */
@Override
protected void onActivityResult(
        int requestCode, int resultCode, Intent data) {
    // Decide what to do based on the original request code
    switch (requestCode) {
        case CONNECTION_FAILURE_RESOLUTION_REQUEST :
        /*
         * If the result code is Activity.RESULT_OK, try
         * to connect again
         */
            switch (resultCode) {
                case Activity.RESULT_OK :
                /*
                 * Try the request again
                 */
                break;
            }
    }
 }
private boolean servicesConnected() {
    // Check that Google Play services is available
    int resultCode =
            GooglePlayServicesUtil.
                    isGooglePlayServicesAvailable(this);
    // If Google Play services is available
    if (ConnectionResult.SUCCESS == resultCode) {
        // In debug mode, log the status
        Log.d("Location Updates",
                "Google Play services is available.");
        // Continue
        return true;
    // Google Play services was not available for some reason
    } else {
        // Get the error code
        // Get the error dialog from Google Play services
        Dialog errorDialog = GooglePlayServicesUtil.getErrorDialog(
                resultCode,
                this,
                CONNECTION_FAILURE_RESOLUTION_REQUEST);

        // If Google Play services can provide an error dialog
        if (errorDialog != null) {
            // Create a new DialogFragment for the error dialog
            ErrorDialogFragment errorFragment =
                    new ErrorDialogFragment();
            // Set the dialog in the DialogFragment
            errorFragment.setDialog(errorDialog);
            // Show the error dialog in the DialogFragment
            errorFragment.show(getSupportFragmentManager(),
                    "Location Updates");
        }
    }
    return false;
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    // TODO Auto-generated method stub
      if (connectionResult.hasResolution()) {
            try {
                // Start an Activity that tries to resolve the error
                connectionResult.startResolutionForResult(
                        this,
                        CONNECTION_FAILURE_RESOLUTION_REQUEST);
                /*
                 * Thrown if Google Play services canceled the original
                 * PendingIntent
                 */
            } catch (IntentSender.SendIntentException e) {
                // Log the error
                e.printStackTrace();
            }
        } else {
            /*
             * If no resolution is available, display a dialog to the
             * user with the error.
             */
            showDialog(connectionResult.getErrorCode());
        }

}
@Override
public void onConnected(Bundle arg0) {
    // TODO Auto-generated method stub

}
@Override
public void onDisconnected() {
    // TODO Auto-generated method stub

}

    /*
     * Called when the Activity becomes visible.
     */
    @Override
    protected void onStart() {
        super.onStart();
        // Connect the client.
        mLocationClient.connect();
    }
    /*
     * Called when the Activity is no longer visible.
     */
    @Override
    protected void onStop() {
        // Disconnecting the client invalidates it.
        mLocationClient.disconnect();
        super.onStop();
    }
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        TextView tv = (TextView) findViewById(R.id.textView1);
         mCurrentLocation = mLocationClient.getLastLocation();
        tv.setText(mCurrentLocation.getLatitude()+"");
    }

}

我得到的错误在这里

09-18 15:11:43.660: E/AndroidRuntime(6839): FATAL EXCEPTION: main
09-18 15:11:43.660: E/AndroidRuntime(6839): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.testapplication/com.example.testapplication.MainActivity}: java.lang.ClassNotFoundException: com.example.testapplication.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.testapplication-1.apk]
09-18 15:11:43.660: E/AndroidRuntime(6839):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
09-18 15:11:43.660: E/AndroidRuntime(6839):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
09-18 15:11:43.660: E/AndroidRuntime(6839):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-18 15:11:43.660: E/AndroidRuntime(6839):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
09-18 15:11:43.660: E/AndroidRuntime(6839):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-18 15:11:43.660: E/AndroidRuntime(6839):     at android.os.Looper.loop(Looper.java:130)
09-18 15:11:43.660: E/AndroidRuntime(6839):     at android.app.ActivityThread.main(ActivityThread.java:3693)
09-18 15:11:43.660: E/AndroidRuntime(6839):     at java.lang.reflect.Method.invokeNative(Native Method)
09-18 15:11:43.660: E/AndroidRuntime(6839):     at java.lang.reflect.Method.invoke(Method.java:507)
09-18 15:11:43.660: E/AndroidRuntime(6839):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
09-18 15:11:43.660: E/AndroidRuntime(6839):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
09-18 15:11:43.660: E/AndroidRuntime(6839):     at dalvik.system.NativeStart.main(Native Method)
09-18 15:11:43.660: E/AndroidRuntime(6839): Caused by: java.lang.ClassNotFoundException: com.example.testapplication.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.testapplication-1.apk]
09-18 15:11:43.660: E/AndroidRuntime(6839):     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
09-18 15:11:43.660: E/AndroidRuntime(6839):     at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
09-18 15:11:43.660: E/AndroidRuntime(6839):     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
09-18 15:11:43.660: E/AndroidRuntime(6839):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
09-18 15:11:43.660: E/AndroidRuntime(6839):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
09-18 15:11:43.660: E/AndroidRuntime(6839):     ... 11 more

3 个答案:

答案 0 :(得分:3)

Caused by: java.lang.ClassNotFoundException: com.example.testapplication.MainActivity

这意味着活动不在清单中,或者在那里不正确地声明。 如果不是这样,项目的Eclipse设置不会导出支持库。这是一个link about this issue

答案 1 :(得分:2)

Right click on your project goto Properties->Java Build Path->Order and Export select all checkbox now clean your project and run 

答案 2 :(得分:0)

java.lang.ClassNotFoundException:com.example.testapplication.MainActivity in loader dalvik.system.PathClassLoader [/data/app/com.example.testapplication-1.apk] 表示您的pakage不是MainActivity的com.example.testapplication,或者您只是不在manifest.xml中宣布它