简体   繁体   中英

Errors in MainActivity.java with Google Maps API sample project

I'm trying to use the Google Play services's Google Maps demo but I'm returned errors on two lines of the sample code. The code that returns errors are these:

new DemoDetails(R.string.circle_demo, R.string.circle_description,
                CircleDemoActivity.class),

The error for the first line is "The constructor MainActivity.DemoDetails(int, int, Class) is undefined." The second error is "CircleDemoActivity cannot be resolved to a type."

This could be a totally trivial thing, I'm new to Android Development, so please pardon my lack of know-how.

Here's the rest of the code in MainActivity.java:

/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.mapdemo;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.example.mapdemo.view.FeatureView;

/**
 * The main activity of the API library demo gallery.
 * <p>
 * The main layout lists the demonstrated features, with buttons to launch them.
 */
public final class MainActivity extends ListActivity {

/**
 * A simple POJO that holds the details about the demo that are used by the List Adapter.
 */
private static class DemoDetails {
    /**
     * The resource id of the title of the demo.
     */
    private final int titleId;

    /**
     * The resources id of the description of the demo.
     */
    private final int descriptionId;

    /**
     * The demo activity's class.
     */
    private final Class<? extends FragmentActivity> activityClass;

    public DemoDetails(
            int titleId, int descriptionId, Class<? extends FragmentActivity> activityClass) {
        super();
        this.titleId = titleId;
        this.descriptionId = descriptionId;
        this.activityClass = activityClass;
    }
}

/**
 * A custom array adapter that shows a {@link FeatureView} containing details about the demo.
 */
private static class CustomArrayAdapter extends ArrayAdapter<DemoDetails> {

    /**
     * @param demos An array containing the details of the demos to be displayed.
     */
    public CustomArrayAdapter(Context context, DemoDetails[] demos) {
        super(context, R.layout.feature, R.id.title, demos);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        FeatureView featureView;
        if (convertView instanceof FeatureView) {
            featureView = (FeatureView) convertView;
        } else {
            featureView = new FeatureView(getContext());
        }

        DemoDetails demo = getItem(position);

        featureView.setTitleId(demo.titleId);
        featureView.setDescriptionId(demo.descriptionId);

        return featureView;
    }
}

private static final DemoDetails[] demos = {new DemoDetails(
        R.string.basic_map, R.string.basic_description, BasicMapActivity.class),
        new DemoDetails(R.string.camera_demo, R.string.camera_description,
                CameraDemoActivity.class),
        new DemoDetails(R.string.events_demo, R.string.events_description,
                EventsDemoActivity.class),
        new DemoDetails(R.string.layers_demo, R.string.layers_description,
                LayersDemoActivity.class),
        new DemoDetails(
                R.string.locationsource_demo, R.string.locationsource_description,
                LocationSourceDemoActivity.class),
        new DemoDetails(R.string.uisettings_demo, R.string.uisettings_description,
                UiSettingsDemoActivity.class),
        new DemoDetails(R.string.groundoverlay_demo, R.string.groundoverlay_description,
                GroundOverlayDemoActivity.class),
        new DemoDetails(R.string.marker_demo, R.string.marker_description,
                MarkerDemoActivity.class),
        new DemoDetails(R.string.polygon_demo, R.string.polygon_description,
                PolygonDemoActivity.class),
        new DemoDetails(R.string.polyline_demo, R.string.polyline_description,
                PolylineDemoActivity.class),
        new DemoDetails(R.string.circle_demo, R.string.circle_description,
                CircleDemoActivity.class),
        new DemoDetails(R.string.tile_overlay_demo, R.string.tile_overlay_description,
                TileOverlayDemoActivity.class),
        new DemoDetails(R.string.options_demo, R.string.options_description,
                OptionsDemoActivity.class),
        new DemoDetails(R.string.multi_map_demo, R.string.multi_map_description,
                MultiMapDemoActivity.class),
        new DemoDetails(R.string.retain_map, R.string.retain_map_description,
                RetainMapActivity.class),
        new DemoDetails(R.string.raw_mapview_demo, R.string.raw_mapview_description,
                RawMapViewDemoActivity.class),
        new DemoDetails(R.string.programmatic_demo, R.string.programmatic_description,
                ProgrammaticDemoActivity.class)};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ListAdapter adapter = new CustomArrayAdapter(this, demos);

    setListAdapter(adapter);
}

Thanks.

EDIT: App is crashing, I think because Google Maps isn't compatible with the Emulator, but here's the logcat when running it:

04-10 18:40:31.338: W/dalvikvm(2959): Unable to resolve superclass of Lcom/example/mapdemo/BasicMapActivity; (75) 04-10 18:40:31.396: W/dalvikvm(2959): Link of class 'Lcom/example/mapdemo/BasicMapActivity;' failed 04-10 18:40:31.396: E/dalvikvm(2959): Could not find class 'com.example.mapdemo.BasicMapActivity', referenced from method com.example.mapdemo.MainActivity.<clinit> 04-10 18:40:31.396: W/dalvikvm(2959): VFY: unable to resolve const-class 117 (Lcom/example/mapdemo/BasicMapActivity;) in Lcom/example/mapdemo/MainActivity; 04-10 18:40:31.396: D/dalvikvm(2959): VFY: replacing opcode 0x1c at 0x000d 04-10 18:40:31.447: W/dalvikvm(2959): Exception Ljava/lang/NoClassDefFoundError; thrown while initializing Lcom/example/mapdemo/MainActivity; 04-10 18:40:31.477: W/dalvikvm(2959): Class init failed in newInstance call (Lcom/example/mapdemo/MainActivity;) 04-10 18:40:31.477: D/AndroidRuntime(2959): Shutting down VM 04-10 18:40:31.477: W/dalvikvm(2959): threadid=1: thread exiting with uncaught exception (group=0x40a71930) 04-10 18:40:31.536: E/AndroidRuntime(2959): FATAL EXCEPTION: main 04-10 18:40:31.536: E/AndroidRuntime(2959): java.lang.ExceptionInInitializerError 04-10 18:40:31.536: E/AndroidRuntime(2959): at java.lang.Class.newInstanceImpl(Native Method) 04-10 18:40:31.536: E/AndroidRuntime(2959): at java.lang.Class.newInstance(Class.java:1319) 04-10 18:40:31.536: E/AndroidRuntime(2959): at android.app.Instrumentation.newActivity(Instrumentation.java:1054) 04-10 18:40:31.536: E/AndroidRuntime(2959): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097) 04-10 18:40:31.536: E/AndroidRuntime(2959): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 04-10 18:40:31.536: E/AndroidRuntime(2959): at android.app.ActivityThread.access$600(ActivityThread.java:141) 04-10 18:40:31.536: E/AndroidRuntime(2959): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 04-10 18:40:31.536: E/AndroidRuntime(2959): at android.os.Handler.dispatchMessage(Handler.java:99) 04-10 18:40:31.536: E/AndroidRuntime(2959): at android.os.Looper.loop(Looper.java:137) 04-10 18:40:31.536: E/AndroidRuntime(2959): at android.app.ActivityThread.main(ActivityThread.java:5041) 04-10 18:40:31.536: E/AndroidRuntime(2959): at java.lang.reflect.Method.invokeNative(Native Method) 04-10 18:40:31.536: E/AndroidRuntime(2959): at java.lang.reflect.Method.invoke(Method.java:511) 04-10 18:40:31.536: E/AndroidRuntime(2959): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 04-10 18:40:31.536: E/AndroidRuntime(2959): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 04-10 18:40:31.536: E/AndroidRuntime(2959): at dalvik.system.NativeStart.main(Native Method) 04-10 18:40:31.536: E/AndroidRuntime(2959): Caused by: java.lang.NoClassDefFoundError: com.example.mapdemo.BasicMapActivity 04-10 18:40:31.536: E/AndroidRuntime(2959): at com.example.mapdemo.MainActivity.<clinit>(MainActivity.java:98) 04-10 18:40:31.536: E/AndroidRuntime(2959): ... 15 more

The CircleDemoActivity is missing. Add it and that should solve your problem.

Or delete all references to it from your files...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM