Android Google地图应用程序在更改活动时崩溃

Android Google地图应用程序在更改活动时崩溃,android,crash,Android,Crash,我都喜欢。我正在开发一个android谷歌地图应用程序。应用程序使用Mapview显示Google地图。主要活动是在启动时显示谷歌地图和覆盖图。在一个长时间的新闻发布会上,我调用了另一个表单,我使用它作为上下文菜单,因为我无法在谷歌地图上获得上下文菜单。此窗体在单击按钮时再次调用另一个窗体。请在下面找到我正在使用的代码。调用显示菜单的项活动时,代码在StartActivity()函数处崩溃。请帮忙 ////////////////////////////////MainActivity.java

我都喜欢。我正在开发一个android谷歌地图应用程序。应用程序使用Mapview显示Google地图。主要活动是在启动时显示谷歌地图和覆盖图。在一个长时间的新闻发布会上,我调用了另一个表单,我使用它作为上下文菜单,因为我无法在谷歌地图上获得上下文菜单。此窗体在单击按钮时再次调用另一个窗体。请在下面找到我正在使用的代码。调用显示菜单的项活动时,代码在StartActivity()函数处崩溃。请帮忙

////////////////////////////////MainActivity.java code/////////////////////////////

package net.learn2develop.GoogleMaps;

import java.util.List;
import java.lang.Exception;

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

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;

public class MapsActivity extends MapActivity
{
    MapView mapView;
    MapController mc;
    GeoPoint p;
    public Intent myIntent;

    class MapOverlay extends com.google.android.maps.Overlay
    {
        long startTime;
        long endTime;
        float startX, startY, endX, endY;

        @Override
        public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)
        {
            super.draw(canvas, mapView, shadow);                  

            //---translate the GeoPoint to screen pixels---
            Point screenPts = new Point();
            mapView.getProjection().toPixels(p, screenPts);

            //---add the marker---
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pushpin);           
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y-35, null);



            return true;
        }

        @Override
        public boolean onTouchEvent(MotionEvent event, MapView mapView)
        {  

            if(event.getAction() == MotionEvent.ACTION_DOWN)
            {
                //record the start time
                startTime = event.getEventTime();
                startX = event.getX();
                startY = event.getY();

            }
            else if(event.getAction() == MotionEvent.ACTION_UP)
            {
                //record the end time
                endTime = event.getEventTime();
                endX = event.getX();
                endY = event.getY();
            }

             //verify
            if(((endTime - startTime) > 1000) && (startX == endX) && (startY == endY))
            {
                try
                {
                    //Toast.makeText(getBaseContext(), "Opening menu!!!", Toast.LENGTH_SHORT).show();
                    myIntent = new Intent(mapView.getContext(), Item.class);
                    startActivity(myIntent);

                    //Intent myIntent = new Intent("net.learn2develop.GoogleMaps.Item");
                    //myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    //myIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    //startActivity(myIntent);

                    //Context context = net.learn2develop.GoogleMaps.getBaseContext();
                    //context.startActivity(new Intent(context, Item.class));

                }
                catch(Exception e)
                {
                    Log.d("Events", e.getMessage());
                }
                return true; //notify that you handled this event (do not propagate)
            }
            else
                return false;
        }
    }

    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        MapController mc = mapView.getController();
        switch (keyCode)
        {
            case KeyEvent.KEYCODE_3:
                mc.zoomIn();
                break;
            case KeyEvent.KEYCODE_1:
                mc.zoomOut();
                break;
        }
        return super.onKeyDown(keyCode, event);
    }   

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

        mapView = (MapView) findViewById(R.id.mapview);

        mapView.displayZoomControls(true);

        mc = mapView.getController();

        String coordinates[] = {"12.966667", "77.566667"};
        double lat = Double.parseDouble(coordinates[0]);
        double lng = Double.parseDouble(coordinates[1]);

        p = new GeoPoint(
            (int) (lat * 1E6),
            (int) (lng * 1E6));

        mc.animateTo(p);
        mc.setZoom(17);

      //---Add a location marker---
        MapOverlay mapOverlay = new MapOverlay();
        List<Overlay> listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);
        mapView.invalidate();
    }

    @Override
    protected boolean isRouteDisplayed()
    {
        // TODO Auto-generated method stub
        return false;
    }
}

///////////////////////////////////Item.java code/////////////////////////////

package net.learn2develop.GoogleMaps;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Item extends Activity {
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.item_menu);

        Button pavement = (Button) findViewById(R.id.pavement);
        pavement.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View view)
            {
                Intent myIntent = new Intent(view.getContext(), Pavement.class);
                startActivity(myIntent);

                //startActivity(new Intent("net.learn2develop.GoogleMaps.Pavement"));
            }
        });
    }

}


///////////////////////////////////Pavement.java code////////////////////

package net.learn2develop.GoogleMaps;

import android.app.Activity;
import android.os.Bundle;

public class Pavement extends Activity
{
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.pavement_menu);       
    }
}

/////////////////////////////../layout/item_menu.xml code////////////////////////

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
    <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
        <Button android:id="@+id/pavment"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Pavment"
        />
    </LinearLayout>
    <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
        <Button android:id="@+id/sign"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Sign"
        />
    </LinearLayout>
</LinearLayout>

////////////////////////////../layout/main.xml code////////////////////////

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <TableRow >
    <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="0eXLh5uOHzxYyO-UUL3iRgxeYZoGeY7yusL32zA"
        />
    </TableRow>
</TableLayout>

/////////////////////////////../layout/menu.xml code://///////////////////////


<menu xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/menu"
    >
    <item android:id="@+id/pavement"
        android:title="Pavement" />
    <item android:id="@+id/sign"
        android:title="Sign" />
</menu>

////////////////////////../layout/pavement_menu.xml code//////////////////////

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
    <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
        <Button android:id="@+id/viewInfo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="View info"
        />
    </LinearLayout>
    <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
        <Button android:id="@+id/enterSurvey"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Enter survey"
        />
    </LinearLayout>
</LinearLayout>

/////////////////////////////////AndroidManifest.xml///////////////////////

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

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

    <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" >
        <uses-library android:name="com.google.android.maps"/>

        <activity
            android:label="@string/app_name"
            android:name=".MapsActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="net.learn2develop.GoogleMaps.Item"
            android:label="Item"
            android:theme="@android:style/Theme.Dialog">

        </activity>
        <activity android:name="net.learn2develop.GoogleMaps.Pavement"
            android:label="Pavement"
            android:theme="@android:style/Theme.Dialog">

        </activity>
    </application>
    <uses-permission android:name="android.permission.INTERNET" />"


</manifest>

试试这个让我知道会发生什么

在类映射覆盖中

 myIntent = new Intent(MapsActivity.this, Item.class);
      startActivity(myIntent);

项目类中

 public void onClick(View view)
            {
                Intent myIntent = new Intent(Item.this, Pavement.class);
                startActivity(myIntent);

                //startActivity(new Intent("net.learn2develop.GoogleMaps.Pavement"));
            }

请不要只是张贴所有的代码,在未来。从logcat和相关代码发布异常堆栈跟踪您的代码在我的eclipse中运行良好。。毫无例外……我认为这会解决你的问题,但如果不行,你可以在你的类MapsActivity中始终使用onTouchEvent方法…@Jovan-是的,你可以在MapsActivity中使用onTouchEvent,但在这里我建议他只对现有代码进行更改。谢谢你的回复。我试过了,但没用。现在,甚至AVD也没有启动。@user370305-是的,我看到了你所做的,但我想告诉他,他有和其他可能性。你能把你的日志放在Item.java类的第17行吗?
 myIntent = new Intent(getBaseContext(), Item.class);
  startActivity(myIntent);
 public void onClick(View view)
            {
                Intent myIntent = new Intent(Item.this, Pavement.class);
                startActivity(myIntent);

                //startActivity(new Intent("net.learn2develop.GoogleMaps.Pavement"));
            }