0

使用面向 android 地图的示例项目时出现错误。当我运行这个程序时,它在 log-cat 中显示 NoClassDefFoundError。我在这个程序中找不到任何错误,任何人都可以帮我解决这个问题..

PlacesMapActivity.java

public class MainActivity extends Activity {

    // flag for Internet connection status
    Boolean isInternetPresent = false;

    // Connection detector class
    ConnectionDetector cd;

    // Alert Dialog Manager
    AlertDialogManager alert = new AlertDialogManager();

    // Google Places
    GooglePlaces googlePlaces;

    // Places List
    PlacesList nearPlaces;

    // GPS Location
    GPSTracker gps;

    // Button
    Button btnShowOnMap;

    // Progress dialog
    ProgressDialog pDialog;

    // Places Listview
    ListView lv;

    // ListItems data
    ArrayList<HashMap<String, String>> placesListItems = new ArrayList<HashMap<String,String>>();


    // KEY Strings
    public static String KEY_REFERENCE = "reference"; // id of the place
    public static String KEY_NAME = "name"; // name of the place
    public static String KEY_VICINITY = "vicinity"; // Place area name

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        cd = new ConnectionDetector(getApplicationContext());

        // Check if Internet present
        isInternetPresent = cd.isConnectingToInternet();
        if (!isInternetPresent) {
            // Internet Connection is not present
            alert.showAlertDialog(MainActivity.this, "Internet Connection Error",
                    "Please connect to working Internet connection", false);
            // stop executing code by return
            return;
        }

        // creating GPS Class object
        gps = new GPSTracker(this);

        // check if GPS location can get
        if (gps.canGetLocation()) {
            Log.d("Your Location", "latitude:" + gps.getLatitude() + ", longitude: " + gps.getLongitude());
        } else {
            // Can't get user's current location
            alert.showAlertDialog(MainActivity.this, "GPS Status",
                    "Couldn't get location information. Please enable GPS",
                    false);
            // stop executing code by return
            return;
        }

        // Getting listview
        lv = (ListView) findViewById(R.id.list);

        // button show on map
        btnShowOnMap = (Button) findViewById(R.id.btn_show_map);

        // calling background Async task to load Google Places
        // After getting places from Google all the data is shown in listview
        new LoadPlaces().execute();

        /** Button click event for shown on map */
        btnShowOnMap.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent i = new Intent(getApplicationContext(),
                        PlacesMapActivity.class);
                // Sending user current geo location
                i.putExtra("user_latitude", Double.toString(gps.getLatitude()));
                i.putExtra("user_longitude", Double.toString(gps.getLongitude()));

                // passing near places to map activity
                i.putExtra("near_places", nearPlaces);
                // staring activity
                startActivity(i);
            }
        });


        /**
         * ListItem click event
         * On selecting a listitem SinglePlaceActivity is launched
         * */
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // getting values from selected ListItem
                String reference = ((TextView) view.findViewById(R.id.reference)).getText().toString();

                // Starting new intent
                Intent in = new Intent(getApplicationContext(),
                        SinglePlaceActivity.class);

                // Sending place refrence id to single place activity
                // place refrence id used to get "Place full details"
                in.putExtra(KEY_REFERENCE, reference);
                startActivity(in);
            }
        });
    }

    /**
     * Background Async Task to Load Google places
     * */
    class LoadPlaces extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage(Html.fromHtml("<b>Search</b><br/>Loading Places..."));
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        /**
         * getting Places JSON
         * */
        protected String doInBackground(String... args) {
            // creating Places class object
            googlePlaces = new GooglePlaces();

            try {
                // Separeate your place types by PIPE symbol "|"
                // If you want all types places make it as null
                // Check list of types supported by google
                // 
                String types = "cafe|restaurant"; // Listing places only cafes, restaurants

                // Radius in meters - increase this value if you don't find any places
                double radius = 1000; // 1000 meters 

                // get nearest places
                nearPlaces = googlePlaces.search(gps.getLatitude(),
                        gps.getLongitude(), radius, types);


            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * and show the data in UI
         * Always use runOnUiThread(new Runnable()) to update UI from background
         * thread, otherwise you will get error
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all products
            pDialog.dismiss();
            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                    /**
                     * Updating parsed Places into LISTVIEW
                     * */
                    // Get json response status
                    String status = nearPlaces.status;

                    // Check for all possible status
                    if(status.equals("OK")){
                        // Successfully got places details
                        if (nearPlaces.results != null) {
                            // loop through each place
                            for (Place p : nearPlaces.results) {
                                HashMap<String, String> map = new HashMap<String, String>();

                                // Place reference won't display in listview - it will be hidden
                                // Place reference is used to get "place full details"
                                map.put(KEY_REFERENCE, p.reference);

                                // Place name
                                map.put(KEY_NAME, p.name);


                                // adding HashMap to ArrayList
                                placesListItems.add(map);
                            }
                            // list adapter
                            ListAdapter adapter = new SimpleAdapter(MainActivity.this, placesListItems,
                                    R.layout.list_item,
                                    new String[] { KEY_REFERENCE, KEY_NAME}, new int[] {
                                            R.id.reference, R.id.name });

                            // Adding data into listview
                            lv.setAdapter(adapter);
                        }
                    }
                    else if(status.equals("ZERO_RESULTS")){
                        // Zero results found
                        alert.showAlertDialog(MainActivity.this, "Near Places",
                                "Sorry no places found. Try to change the types of places",
                                false);
                    }
                    else if(status.equals("UNKNOWN_ERROR"))
                    {
                        alert.showAlertDialog(MainActivity.this, "Places Error",
                                "Sorry unknown error occured.",
                                false);
                    }
                    else if(status.equals("OVER_QUERY_LIMIT"))
                    {
                        alert.showAlertDialog(MainActivity.this, "Places Error",
                                "Sorry query limit to google places is reached",
                                false);
                    }
                    else if(status.equals("REQUEST_DENIED"))
                    {
                        alert.showAlertDialog(MainActivity.this, "Places Error",
                                "Sorry error occured. Request is denied",
                                false);
                    }
                    else if(status.equals("INVALID_REQUEST"))
                    {
                        alert.showAlertDialog(MainActivity.this, "Places Error",
                                "Sorry error occured. Invalid Request",
                                false);
                    }
                    else
                    {
                        alert.showAlertDialog(MainActivity.this, "Places Error",
                                "Sorry error occured.",
                                false);
                    }
                }
            });

        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }



}

日志猫

02-16 20:05:33.528: E/AndroidRuntime(367): FATAL EXCEPTION: main
02-16 20:05:33.528: E/AndroidRuntime(367): java.lang.NoClassDefFoundError: com.androidhive.googleplacesandmaps.PlacesMapActivity
02-16 20:05:33.528: E/AndroidRuntime(367):  at com.androidhive.googleplacesandmaps.MainActivity$1.onClick(MainActivity.java:110)
02-16 20:05:33.528: E/AndroidRuntime(367):  at android.view.View.performClick(View.java:2408)
02-16 20:05:33.528: E/AndroidRuntime(367):  at android.view.View$PerformClick.run(View.java:8816)
02-16 20:05:33.528: E/AndroidRuntime(367):  at android.os.Handler.handleCallback(Handler.java:587)
02-16 20:05:33.528: E/AndroidRuntime(367):  at android.os.Handler.dispatchMessage(Handler.java:92)
02-16 20:05:33.528: E/AndroidRuntime(367):  at android.os.Looper.loop(Looper.java:123)
02-16 20:05:33.528: E/AndroidRuntime(367):  at android.app.ActivityThread.main(ActivityThread.java:4627)
02-16 20:05:33.528: E/AndroidRuntime(367):  at java.lang.reflect.Method.invokeNative(Native Method)
02-16 20:05:33.528: E/AndroidRuntime(367):  at java.lang.reflect.Method.invoke(Method.java:521)
02-16 20:05:33.528: E/AndroidRuntime(367):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-16 20:05:33.528: E/AndroidRuntime(367):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-16 20:05:33.528: E/AndroidRuntime(367):  at dalvik.system.NativeStart.main(Native Method)
02-16 20:05:33.528: E/AndroidRuntime(367): Caused by: java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation
02-16 20:05:33.528: E/AndroidRuntime(367):  at dalvik.system.DexFile.defineClass(Native Method)
02-16 20:05:33.528: E/AndroidRuntime(367):  at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:209)
02-16 20:05:33.528: E/AndroidRuntime(367):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:203)
02-16 20:05:33.528: E/AndroidRuntime(367):  at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
02-16 20:05:33.528: E/AndroidRuntime(367):  at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
02-16 20:05:33.528: E/AndroidRuntime(367):  ... 12 more
4

2 回答 2

1

确保在 Project > Properties > Android 中选中 Google API。如果不是这种情况,请选择它并单击应用。此更改将修改文件 project.properties 中的目标属性并更改包含的库。(例如,使用最新的 android sdk,您将拥有:target=Google Inc.:Google APIs:14)

转到 Project > Properties > Java Build Path > Libraries 并确保没有直接包含 android.jar 或 maps.jar。您应该只使用至少 maps.jar 和 android.jar 作为叶子的 Google API。

于 2013-02-16T15:47:52.487 回答
0

MainActivity.java 的第 110 行是什么?鉴于您选择性地粘贴了上面的代码,因此不可能知道。如果我冒险猜测/戳穿上述内容,我会说您的目标 API 级别比您运行它的设备更高。

于 2013-02-16T15:40:21.330 回答