2010-06-04 8 views
0

J'essaie d'afficher une carte qui tourne en fonction de l'orientation du téléphone. J'ai réécrire une partie de l'échantillon google pour utiliser un SensorEventListner au lieu d'un SensorListener.MapView: crash de l'application sur le lancement

Mon problème est lorsque j'essaie d'utiliser la méthode findViewByID() pour afficher la carte mon application se bloque au démarrage. Si j'utilise MapView(Context, String) pour créer ma vue, cela fonctionne très bien. Est-ce que j'ai manqué quelque chose pour utiliser le fichier de mise en page main.xml? Quel est le problème avec ça?

Merci d'avance.

Voici mon code:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); 
    mRotateView = new RotateView(this); 
    // Seems ok 
    mMapView = new MapView(this, "0t6yfD1f6K1wZe99RmpB2KdpQHl23WZSPRtGLEg"); 

    // Does not work: application crash on launch. 
    //setContentView(R.layout.main); 
    //mMapView = (MapView) findViewById(R.id.mapview); 

    mRotateView.addView(mMapView); 
    setContentView(mRotateView); 

    mMyLocationOverlay = new MyLocationOverlay(this, mMapView); 
    mMyLocationOverlay.runOnFirstFix(new Runnable() { 
     public void run() { 
      mMapView.getController().animateTo(
        mMyLocationOverlay.getMyLocation()); 
     } 
    }); 
    mMapView.getOverlays().add(mMyLocationOverlay); 
    mMapView.getController().setZoom(18); 
    mMapView.setClickable(true); 
    mMapView.setEnabled(true); 
    mMapView.setBuiltInZoomControls(true); 

} 

AndroidManifest:

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

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

    <application android:icon="@drawable/icon" android:label="@string/app_name" 
     android:theme="@android:style/Theme.NoTitleBar"> 

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

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

    </application> 

</manifest> 

res/layout/main.xml:

<?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" 
> 
<com.google.android.maps.MapView 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/mapview" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:clickable="true" 
android:apiKey="0t6yfD1f6K1wZe99RmpB2KdpQHl23WZSPRtGLEg" 
/> 
</LinearLayout> 

06-04 18:07:31.512: INFO/ActivityManager(66): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.hello/.HelloGoogleMapsActivity } 
06-04 18:07:31.842: INFO/ActivityManager(66): Start proc com.example.hello for activity com.example.hello/.HelloGoogleMapsActivity: pid=207 uid=10025 gids={3003, 1015} 
06-04 18:07:33.053: ERROR/ActivityThread(207): Failed to find provider info for com.google.settings 
06-04 18:07:33.063: ERROR/ActivityThread(207): Failed to find provider info for com.google.settings 
06-04 18:07:33.093: ERROR/ActivityThread(207): Failed to find provider info for com.google.settings 
06-04 18:07:33.282: DEBUG/qemud(34): fdhandler_accept_event: accepting on fd 10 
06-04 18:07:33.282: DEBUG/qemud(34): created client 0x150b8 listening on fd 15 
06-04 18:07:33.282: DEBUG/qemud(34): client_fd_receive: attempting registration for service 'sensors' 
06-04 18:07:33.282: DEBUG/qemud(34): client_fd_receive: -> received channel id 8 
06-04 18:07:33.292: DEBUG/qemud(34): client_registration: registration succeeded for client 8 
06-04 18:07:33.302: DEBUG/qemud(34): fdhandler_event: disconnect on fd 15 
06-04 18:07:33.312: DEBUG/SensorManager(207): found sensor: Goldfish 3-axis Accelerometer, handle=0 
06-04 18:07:33.725: DEBUG/AndroidRuntime(207): Shutting down VM 
06-04 18:07:33.725: WARN/dalvikvm(207): threadid=3: thread exiting with uncaught exception (group=0x4001aa28) 
06-04 18:07:33.732: ERROR/AndroidRuntime(207): Uncaught handler: thread main exiting due to uncaught exception 
06-04 18:07:33.772: ERROR/AndroidRuntime(207): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hello/com.example.hello.HelloGoogleMapsActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 
06-04 18:07:33.772: ERROR/AndroidRuntime(207):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401) 
06-04 18:07:33.772: ERROR/AndroidRuntime(207):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417) 
06-04 18:07:33.772: ERROR/AndroidRuntime(207):  at android.app.ActivityThread.access$2100(ActivityThread.java:116) 
06-04 18:07:33.772: ERROR/AndroidRuntime(207):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
06-04 18:07:33.772: ERROR/AndroidRuntime(207):  at android.os.Handler.dispatchMessage(Handler.java:99) 
06-04 18:07:33.772: ERROR/AndroidRuntime(207):  at android.os.Looper.loop(Looper.java:123) 
06-04 18:07:33.772: ERROR/AndroidRuntime(207):  at android.app.ActivityThread.main(ActivityThread.java:4203) 
06-04 18:07:33.772: ERROR/AndroidRuntime(207):  at java.lang.reflect.Method.invokeNative(Native Method) 
06-04 18:07:33.772: ERROR/AndroidRuntime(207):  at java.lang.reflect.Method.invoke(Method.java:521) 
06-04 18:07:33.772: ERROR/AndroidRuntime(207):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) 
06-04 18:07:33.772: ERROR/AndroidRuntime(207):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549) 
06-04 18:07:33.772: ERROR/AndroidRuntime(207):  at dalvik.system.NativeStart.main(Native Method) 
06-04 18:07:33.772: ERROR/AndroidRuntime(207): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 
06-04 18:07:33.772: ERROR/AndroidRuntime(207):  at android.view.ViewGroup.addViewInner(ViewGroup.java:1856) 
06-04 18:07:33.772: ERROR/AndroidRuntime(207):  at android.view.ViewGroup.addView(ViewGroup.java:1751) 
06-04 18:07:33.772: ERROR/AndroidRuntime(207):  at android.view.ViewGroup.addView(ViewGroup.java:1708) 
06-04 18:07:33.772: ERROR/AndroidRuntime(207):  at android.view.ViewGroup.addView(ViewGroup.java:1688) 
06-04 18:07:33.772: ERROR/AndroidRuntime(207):  at com.example.hello.HelloGoogleMapsActivity.onCreate(HelloGoogleMapsActivity.java:156) 
06-04 18:07:33.772: ERROR/AndroidRuntime(207):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123) 
06-04 18:07:33.772: ERROR/AndroidRuntime(207):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364) 
06-04 18:07:33.772: ERROR/AndroidRuntime(207):  ... 11 more 
06-04 18:07:33.812: INFO/Process(66): Sending signal. PID: 207 SIG: 3 
06-04 18:07:33.822: INFO/dalvikvm(207): threadid=7: reacting to signal 3 
06-04 18:07:33.962: INFO/dalvikvm(207): Wrote stack trace to '/data/anr/traces.txt' 
06-04 18:07:34.412: INFO/ARMAssembler(66): generated scanline__00000077:03515104_00000000_00000000 [ 27 ipp] (41 ins) at [0x2aa9e8:0x2aaa8c] in 1786681 ns 
06-04 18:07:34.442: INFO/ARMAssembler(66): generated scanline__00000077:03515104_00001001_00000000 [ 64 ipp] (84 ins) at [0x2ac4f8:0x2ac648] in 2802312 ns 
06-04 18:07:41.580: WARN/ActivityManager(66): Launch timeout has expired, giving up wake lock! 
06-04 18:07:42.550: WARN/ActivityManager(66): Activity idle timeout for HistoryRecord{4379c288 com.example.hello/.HelloGoogleMapsActivity} 

Je pense que le problème est que j'appelle setContentView() 2 fois:

setContentView(R.layout.main); 
mMapView = (MapView) findViewById(R.id.mapview); 

mRotateView.addView(mMapView); 
setContentView(mRotateView); 

J'ai essayé de commenter la première setContentView (R.layout.main); mais l'application plante toujours au moment du lancement. Il résulte d'une NullPointerException:

06-05 10:30:10.272: WARN/dalvikvm(211): threadid=3: thread exiting with uncaught exception (group=0x4001aa28) 
06-05 10:30:10.282: ERROR/AndroidRuntime(211): Uncaught handler: thread main exiting due to uncaught exception 
06-05 10:30:10.312: ERROR/AndroidRuntime(211): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hello/com.example.hello.HelloGoogleMapsActivity}: java.lang.NullPointerException 
06-05 10:30:10.312: ERROR/AndroidRuntime(211):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401) 
06-05 10:30:10.312: ERROR/AndroidRuntime(211):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417) 
06-05 10:30:10.312: ERROR/AndroidRuntime(211):  at android.app.ActivityThread.access$2100(ActivityThread.java:116) 
06-05 10:30:10.312: ERROR/AndroidRuntime(211):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
06-05 10:30:10.312: ERROR/AndroidRuntime(211):  at android.os.Handler.dispatchMessage(Handler.java:99) 
06-05 10:30:10.312: ERROR/AndroidRuntime(211):  at android.os.Looper.loop(Looper.java:123) 
06-05 10:30:10.312: ERROR/AndroidRuntime(211):  at android.app.ActivityThread.main(ActivityThread.java:4203) 
06-05 10:30:10.312: ERROR/AndroidRuntime(211):  at java.lang.reflect.Method.invokeNative(Native Method) 
06-05 10:30:10.312: ERROR/AndroidRuntime(211):  at java.lang.reflect.Method.invoke(Method.java:521) 
06-05 10:30:10.312: ERROR/AndroidRuntime(211):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) 
06-05 10:30:10.312: ERROR/AndroidRuntime(211):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549) 
06-05 10:30:10.312: ERROR/AndroidRuntime(211):  at dalvik.system.NativeStart.main(Native Method) 
06-05 10:30:10.312: ERROR/AndroidRuntime(211): Caused by: java.lang.NullPointerException 
06-05 10:30:10.312: ERROR/AndroidRuntime(211):  at android.view.ViewGroup.addView(ViewGroup.java:1701) 
06-05 10:30:10.312: ERROR/AndroidRuntime(211):  at android.view.ViewGroup.addView(ViewGroup.java:1688) 
06-05 10:30:10.312: ERROR/AndroidRuntime(211):  at com.example.hello.HelloGoogleMapsActivity.onCreate(HelloGoogleMapsActivity.java:156) 
06-05 10:30:10.312: ERROR/AndroidRuntime(211):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123) 
06-05 10:30:10.312: ERROR/AndroidRuntime(211):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364) 
06-05 10:30:10.312: ERROR/AndroidRuntime(211):  ... 11 more 

Maintenant, je sais où le problème de Commes; mais je ne sais pas comment créer ma MapView en utilisant le fichier main.xml. J'utilise à la place MapView() à la place; cela fonctionne, mais est-ce la bonne façon de le faire?

Répondre

0

Si vous définissez une classe pour une vue, le nom de la classe doit être écrit dans l'élément du fichier xml. Dans ce cas, il semble que la fonction onCreate ne soit pas référencée dans votre fichier layout.xml. Essayez layout.findViewById (R.id.mapview); au lieu de simplement findViewById (R.id.mapview) ;. Si cela ne fonctionne pas, vous pouvez également essayer d'ajouter ceci avant l'appel à la carte:

setContentView(R.layout.main); 
Questions connexes