在自定义Android应用程序中使用Google Maps

时间:2011-11-17 11:41:38

标签: java android google-maps

我遇到以下问题(如LogCat中所示)。我希望any1可以帮助我,因为我是Android应用程序开发的新手。

    11-17 16:50:38.087: E/AndroidRuntime(372): FATAL EXCEPTION: main
    11-17 16:50:38.087: E/AndroidRuntime(372): java.lang.IllegalStateException: Could not     execute method of the activity
    11-17 16:50:38.087: E/AndroidRuntime(372):  at android.view.View$1.onClick(View.java:2072)
    11-17 16:50:38.087: E/AndroidRuntime(372):  at android.view.View.performClick(View.java:2408)
    11-17 16:50:38.087: E/AndroidRuntime(372):  at android.view.View$PerformClick.run(View.java:8816)
    11-17 16:50:38.087: E/AndroidRuntime(372):  at android.os.Handler.handleCallback(Handler.java:587)
    11-17 16:50:38.087: E/AndroidRuntime(372):  at android.os.Handler.dispatchMessagev(Handler.java:92)
    11-17 16:50:38.087: E/AndroidRuntime(372):  at android.os.Looper.loop(Looper.java:123)
    11-17 16:50:38.087: E/AndroidRuntime(372):  at android.app.ActivityThread.main(ActivityThread.java:4627)
    11-17 16:50:38.087: E/AndroidRuntime(372):  at java.lang.reflect.Method.invokeNative(Native Method)
    11-17 16:50:38.087: E/AndroidRuntime(372):  at java.lang.reflect.Method.invoke(Method.java:521)
    11-17 16:50:38.087: E/AndroidRuntime(372):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    11-17 16:50:38.087: E/AndroidRuntime(372):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    11-17 16:50:38.087: E/AndroidRuntime(372):  at dalvik.system.NativeStart.main(Native Method)
    11-17 16:50:38.087: E/AndroidRuntime(372): Caused by: java.lang.reflect.InvocationTargetException
    11-17 16:50:38.087: E/AndroidRuntime(372):  at com.mspl.activities.AlertListActivity.doShow(AlertListActivity.java:144)
    11-17 16:50:38.087: E/AndroidRuntime(372):  at java.lang.reflect.Method.invokeNative(Native Method)
    11-17 16:50:38.087: E/AndroidRuntime(372):  at java.lang.reflect.Method.invoke(Method.java:521)enter code here
    11-17 16:50:38.087: E/AndroidRuntime(372):  at android.view.View$1.onClick(View.java:2067)
    11-17 16:50:38.087: E/AndroidRuntime(372):  ... 11 more
    11-17 16:50:38.087: E/AndroidRuntime(372): Caused by: java.lang.NoClassDefFoundError: com.mspl.activities.AlertLocationActivity
    11-17 16:50:38.087: E/AndroidRuntime(372):  ... 15 more
    11-17 16:50:38.087: E/AndroidRuntime(372): Caused by: java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation
    11-17 16:50:38.087: E/AndroidRuntime(372):  at dalvik.system.DexFile.defineClass(Native Method)
    11-17 16:50:38.087: E/AndroidRuntime(372):  at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:209)
    11-17 16:50:38.087: E/AndroidRuntime(372):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:203)
    11-17 16:50:38.087: E/AndroidRuntime(372):  at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
    11-17 16:50:38.087: E/AndroidRuntime(372):  at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
    11-17 16:50:38.087: E/AndroidRuntime(372):  ... 15 more    

这是我的AlertListActivity.java文件: -

package com.mspl.activities;
    enter code here
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.HashMap;

    import android.app.ListActivity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.LinearLayout;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    import android.widget.TextView;

    public class AlertListActivity extends ListActivity {
    static final ArrayList<HashMap<String, String>> list2 = new ArrayList<HashMap<String, String>>();
    Date date;
    HashMap<String, String> map = new HashMap<String, String>();
    static int flag = 0;
    SimpleAdapter a;
    int selectedIndex = 0;

    // AlertAdapter sa;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alert);
        //addContentView(R.layout.crowview,new ViewGroup.LayoutParams(this));
        SimpleAdapter adapter = new SimpleAdapter(this, list2, R.layout.crowview, new String[] { "alert_type", "date", "identity"}, new int[] { R.id.alarmType, R.id.date, R.id.identity});
        populateList();
        a = adapter;
        setListAdapter(adapter);
    }


    public void populateList()  //Statically filling the ArrayList with HashMap values
    {

        if (flag == 0) // For entry to be entered only once
        {
            flag = 1;
            map.put("alert_type", "Alert_1");
            date = new Date(121, 11, 9, 17, 5, 00);
            map.put("date", date.toString());
            map.put("identity", "9001");
            list2.add(map);

            map = new HashMap<String, String>();
            map.put("alert_type", "Alert_2");
            date = new Date(111, 11, 9, 17, 5, 05);
            map.put("date", date.toString());
            map.put("identity", "9002");
            list2.add(map);

            map = new HashMap<String, String>();
            map.put("alert_type", "Alert_3");
            date = new Date(111, 11, 9, 17, 5, 10);
            map.put("date", date.toString());
            map.put("identity", "9003");
            list2.add(map);

            map = new HashMap<String, String>();
            map.put("alert_type", "Alert_4");
            date = new Date(111, 11, 9, 17, 5, 15);
            map.put("date", date.toString());
            map.put("identity", "9004");
            list2.add(map);

            map = new HashMap<String, String>();
            map.put("alert_type", "Alert_5");
            date = new Date(111, 11, 9, 17, 5, 20);
            map.put("date", date.toString());
            map.put("identity", "9005");
            list2.add(map);
        }

    }

    public void onAck(View view)    //Performs Delete operation on Alert List
    {
        ListView lv1 = getListView();
//      long i2 = lv1.getSelectedItemPosition();
        //lv1.setSelection(i2);
//      int i3 = view.getId();


        //list2.remove(i2);

        LinearLayout llv = (LinearLayout)view.getParent().getParent();
        LinearLayout llv2 = (LinearLayout)llv.getChildAt(0);
        TextView child = (TextView)llv2.getChildAt(0);


        //Toast.makeText(getApplicationContext(),((TextView)child).getText(), Toast.LENGTH_SHORT).show();
        for(int i=0 ; i<list2.size() ; i++)
        {
            HashMap<String, String> hm = list2.get(i);
            String str = hm.get("identity");
            if(str.equals(child.getText()))
            {
                list2.remove(i);
                break;
            }
        }

        lv1.refreshDrawableState();
        a.notifyDataSetChanged();
        a.notifyDataSetInvalidated();

    }



    public void doShow(View view)
    {
        try{
        Intent intent = new Intent(AlertListActivity.this, AlertLocationActivity.class);
        startActivity(intent);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }


    @Override
    protected void onListItemClick(ListView l, View v, int position, long id)   //Listener for ListView Item click
    {
        super.onListItemClick(l, v, position, id);
        selectedIndex = position;
        Object o = this.getListAdapter().getItem(position);

        String str = o.toString();
        // Toast.makeText(this, "You have chosen: " + " " + str,
        // Toast.LENGTH_LONG).show();
        Intent i = new Intent(this, AlertDetailsActivity.class);
        i.putExtra("str", str);
        startActivity(i);

    }

}

这是我的AlertLocationActivity.java文件: -

    package com.mspl.activities;

    import java.util.List;

    import android.graphics.drawable.Drawable;
    import android.os.Bundle;

    import com.google.android.maps.GeoPoint;
    import com.google.android.maps.MapActivity;
    import com.google.android.maps.MapView;
    import com.google.android.maps.Overlay;
    import com.google.android.maps.OverlayItem;

    public class AlertLocationActivity extends MapActivity
{

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

        MapView mapView = (MapView) findViewById(R.id.mapView);
        mapView.setBuiltInZoomControls(true);

        List<Overlay> mapOverlays = mapView.getOverlays();
        Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
        HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable);

        GeoPoint point = new GeoPoint(19240000,-99120000);
        OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");

        itemizedoverlay.addOverlay(overlayitem);
        mapOverlays.add(itemizedoverlay);
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

}

AndroidManifest.xml文件: -

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mspl.activities" android:versionCode="1"
    android:versionName="1.0">

    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".ShowVehicleListActivity"
            android:label="@string/vehicle.list.screen" />

        <activity android:name=".AlertListActivity"
            android:label="@string/alert.list.screen" />

        <activity android:name=".AlertDetailsActivity"
            android:label="@string/alert2.list.screen" />


        <uses-library android:name="com.google.android.maps" />

        <activity android:name=".AlertLocationActivity"
            android:label="@string/alert.loc.screen" />

    </application>
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
    </manifest> 

这是我的mapview.xml文件: -

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    <com.google.android.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:enabled="true"
        android:clickable="true"
        android:apiKey="0WYMWNJ2XW382cucH2luf4o6bRtkU7W6vROnlrg"
    />
    <LinearLayout android:id="@+id/zoom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    />
    </RelativeLayout>

2 个答案:

答案 0 :(得分:3)

我已经解决了。

问题是,即使没有显示Google API文件夹,也会引用两个Google API库。我想这已经发生了,因为我在创建了我正在使用它的应用程序之后安装了Google地图插件。

解决方案是删除“配置构建路径”中对“maps.jar”的引用,然后创建一个新的Android项目,只需将Google API文件夹复制粘贴到我当前的项目中。

Thanx,不管怎么说......

答案 1 :(得分:1)

你有没有添加

<uses-library android:name="com.google.android.maps" />

里面&lt;申请&gt; Manifest文件中的标记?

同时检查Cannot resolve MapActivity class on Android