android - 无法获取位置

标签 android location

这是链接上的代码 https://developer.android.com/training/location/retrieve-current.html尽管它是相同的代码,但我出错了。我获得了 list 文件的许可。谢谢...

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

最佳答案

引起:java.lang.ClassNotFoundException: com.example.testapplication.MainActivity

这意味着 Activity 不在 list 中,或者在那里声明不当。 如果不是这样,您的项目的 Eclipse 设置没有导出支持库。这是一个 link about this issue .

关于android - 无法获取位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18872133/

相关文章:

android - 无法在 Android 应用程序中对齐复选框(水平/垂直)

flutter - Flutter location.altitude返回0.0

flutter - 检测 flutter 中的模拟位置

Android 位置监听器不工作

ios - 线程 0 因 ARM 线程状态(64 位)而崩溃

android - Skobbler Android SDK : problems with SKMaps. zip

android - 使用嵌套的 viewpager 将触摸事件委托(delegate)给父级

android - java.lang.SecurityException : ConnectivityService: Neither user 10134 nor current process has android. 权限.ACCESS_NETWORK_STATE

android - Android 是否为 AIDL 接口(interface)提供任何版本控制机制?

android - 当我靠近存储的位置时收到通知