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

时间:2011-12-06 05:04:57

标签: android crash

我都是。我正在开发一个Android谷歌地图应用程序。该应用程序使用Mapview显示Google地图。主要活动在启动时显示Google地图和叠加层。在长时间的新闻事件中,我打电话给另一个表单,我将其用作上下文菜单,因为我无法在Google地图上获取上下文菜单。此表单再次单击按钮调用另一个表单。请在下面找到我正在使用的代码。代码在StartActivity()函数崩溃时调用显示菜单的Item活动。请帮忙。

////////////////////////////////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>

////////////////////////////// logcat的//////////////// ////////////////

12-06 17:48:47.457: D/dalvikvm(636): GC_CONCURRENT freed 183K, 3% free 10221K/10503K, paused 7ms+11ms
12-06 17:48:47.457: W/CursorWrapperInner(636): Cursor finalized without prior close()
12-06 17:48:47.468: W/CursorWrapperInner(636): Cursor finalized without prior close()
12-06 17:48:47.689: D/gralloc_goldfish(636): Emulator without GPU emulation detected.
12-06 17:48:48.437: D/dalvikvm(636): GC_CONCURRENT freed 184K, 4% free 10548K/10887K, paused 6ms+5ms
12-06 17:48:49.597: D/dalvikvm(636): GC_CONCURRENT freed 337K, 5% free 10710K/11207K, paused 6ms+8ms
12-06 17:48:50.907: D/dalvikvm(636): GC_CONCURRENT freed 411K, 6% free 10705K/11335K, paused 8ms+6ms
12-06 17:48:52.178: D/dalvikvm(636): GC_CONCURRENT freed 402K, 6% free 10705K/11335K, paused 6ms+13ms
12-06 17:48:53.568: D/dalvikvm(636): GC_CONCURRENT freed 402K, 6% free 10705K/11335K, paused 9ms+15ms
12-06 17:48:54.798: D/dalvikvm(636): GC_CONCURRENT freed 402K, 6% free 10705K/11335K, paused 6ms+6ms
12-06 17:48:55.968: D/dalvikvm(636): GC_CONCURRENT freed 402K, 6% free 10705K/11335K, paused 6ms+13ms
12-06 17:48:57.257: D/dalvikvm(636): GC_CONCURRENT freed 402K, 6% free 10704K/11335K, paused 6ms+8ms
12-06 17:48:58.457: D/dalvikvm(636): GC_CONCURRENT freed 402K, 6% free 10705K/11335K, paused 6ms+10ms
12-06 17:48:59.608: D/dalvikvm(636): GC_CONCURRENT freed 402K, 6% free 10704K/11335K, paused 6ms+12ms
12-06 17:49:02.808: D/dalvikvm(636): GC_CONCURRENT freed 402K, 6% free 10705K/11335K, paused 6ms+6ms
12-06 17:49:04.587: D/dalvikvm(636): GC_CONCURRENT freed 405K, 6% free 10709K/11335K, paused 8ms+6ms
12-06 17:49:06.237: D/dalvikvm(636): GC_CONCURRENT freed 402K, 6% free 10710K/11335K, paused 11ms+6ms
12-06 17:49:07.787: D/dalvikvm(636): GC_CONCURRENT freed 402K, 6% free 10710K/11335K, paused 6ms+7ms
12-06 17:49:09.380: D/dalvikvm(636): GC_CONCURRENT freed 402K, 6% free 10710K/11335K, paused 6ms+6ms
12-06 17:49:10.208: D/AndroidRuntime(636): Shutting down VM
12-06 17:49:10.208: W/dalvikvm(636): threadid=1: thread exiting with uncaught exception (group=0x409951f8)
12-06 17:49:10.248: E/AndroidRuntime(636): FATAL EXCEPTION: main
12-06 17:49:10.248: E/AndroidRuntime(636): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.learn2develop.GoogleMaps/net.learn2develop.GoogleMaps.Item}: java.lang.NullPointerException
12-06 17:49:10.248: E/AndroidRuntime(636):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
12-06 17:49:10.248: E/AndroidRuntime(636):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
12-06 17:49:10.248: E/AndroidRuntime(636):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
12-06 17:49:10.248: E/AndroidRuntime(636):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
12-06 17:49:10.248: E/AndroidRuntime(636):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-06 17:49:10.248: E/AndroidRuntime(636):  at android.os.Looper.loop(Looper.java:137)
12-06 17:49:10.248: E/AndroidRuntime(636):  at android.app.ActivityThread.main(ActivityThread.java:4340)
12-06 17:49:10.248: E/AndroidRuntime(636):  at java.lang.reflect.Method.invokeNative(Native Method)
12-06 17:49:10.248: E/AndroidRuntime(636):  at java.lang.reflect.Method.invoke(Method.java:511)
12-06 17:49:10.248: E/AndroidRuntime(636):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-06 17:49:10.248: E/AndroidRuntime(636):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-06 17:49:10.248: E/AndroidRuntime(636):  at dalvik.system.NativeStart.main(Native Method)
12-06 17:49:10.248: E/AndroidRuntime(636): Caused by: java.lang.NullPointerException
12-06 17:49:10.248: E/AndroidRuntime(636):  at net.learn2develop.GoogleMaps.Item.onCreate(Item.java:17)
12-06 17:49:10.248: E/AndroidRuntime(636):  at android.app.Activity.performCreate(Activity.java:4465)
12-06 17:49:10.248: E/AndroidRuntime(636):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-06 17:49:10.248: E/AndroidRuntime(636):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
12-06 17:49:10.248: E/AndroidRuntime(636):  ... 11 more

1 个答案:

答案 0 :(得分:0)

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

类MapOverlay

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

 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"));
            }