1

这是链接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
4

3 回答 3

3

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

这意味着该活动不在清单中,或者在清单中未正确声明。如果不是这样,您的项目的 Eclipse 设置没有导出支持库。这是有关此问题的链接

于 2013-09-18T12:25:08.937 回答
2
Right click on your project goto Properties->Java Build Path->Order and Export select all checkbox now clean your project and run 
于 2013-09-18T12:35:38.193 回答
0

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

于 2013-09-18T12:24:52.673 回答