2011-12-06 121 views
-1

我都。我正在研究Android Google地圖應用程序。該應用程序使用Mapview顯示Google地圖。主要活動在啓動時顯示Google地圖和覆蓋圖。在長時間按下事件後,我會調用另一個表單作爲上下文菜單,因爲我無法在Google地圖上獲取上下文菜單。點擊此按鈕後,此表單再次調用另一個表單。請在下面找到我使用的代碼。在調用顯示菜單的Item活動時,代碼在StartActivity()函數中崩潰。請幫忙。Android谷歌地圖應用程序崩潰時更改活動

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

請不要只是張貼所有的代碼,在未來。從logcat和相關代碼發佈異常堆棧跟蹤 – Craigy

+0

您的代碼在我的eclipse中正常工作..沒有任何異常。 – user370305

回答

0

試試這個,讓我知道發生什麼事,

類MapOverlay

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

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

而且在Item類

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

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

我認爲這會解決您的問題,但是如果您不能始終在類MapsActivity中使用onTouchEvent方法... – Jovan

+0

@Jovan - 是的,你可以在MapsActivity中使用onTouchEvent,但是在這裏我建議他只修改他現有的代碼。 – user370305

+0

感謝您的回覆。我試過了,但沒有奏效。現在,即使是AVD也沒有推出。 – user1082868

相關問題