ImageView im Java Code "erstellen"

Dieses Thema im Forum "Android App Entwicklung" wurde erstellt von HeroUser96, 14.10.2010.

  1. HeroUser96, 14.10.2010 #1
    HeroUser96

    HeroUser96 Threadstarter Android-Hilfe.de Mitglied

    Beiträge:
    53
    Erhaltene Danke:
    2
    Registriert seit:
    15.09.2010
    Hallo zusammen,

    ich habe folgendes Problem:

    ich möchte, dass mein Programm, wenn ein bestimmtes Ereigniss eintritt, eine neue ImageView "erstellt", d.h. dass diese ImageView nicht schon vorher im XML-Layout vorhanden sein soll, sondern erst dann erzeugt werden soll.

    Danke schonmal im Vorraus :D
     
  2. DieGoldeneMitte, 14.10.2010 #2
    DieGoldeneMitte

    DieGoldeneMitte Android-Lexikon

    Beiträge:
    1,230
    Erhaltene Danke:
    256
    Registriert seit:
    05.02.2010
    Phone:
    Nexus 5X
    Tablet:
    Nexus 7 (2013)
    Das Ding soll im Layout ja an irgendeine Stelle.
    Packe dort ein passenden (Relative)Layout (mit android:id="@+id/imageviewcontainer" oder so) hin.
    Das kannst Du Dir dann per findViewById(R.id.imageviewcontainer) holen.
    Dort steckst Du mittels addView() deinen erzeugen ImageView reintun.
    Anschließend rufst Du auf dem "Container" eine entsprechende Invalidierung (postInvalidate() oder so) auf.
     
    Zuletzt bearbeitet: 14.10.2010
    HeroUser96 bedankt sich.
  3. HeroUser96, 14.10.2010 #3
    HeroUser96

    HeroUser96 Threadstarter Android-Hilfe.de Mitglied

    Beiträge:
    53
    Erhaltene Danke:
    2
    Registriert seit:
    15.09.2010
    Danke für die schnelle Antwort!

    werde es direkt ausprobieren
     
  4. HeroUser96, 15.10.2010 #4
    HeroUser96

    HeroUser96 Threadstarter Android-Hilfe.de Mitglied

    Beiträge:
    53
    Erhaltene Danke:
    2
    Registriert seit:
    15.09.2010
    Wahrscheinlich bin ich einfach nur zu doof, aber bei mir funktioniert es irgendwie nicht.

    Muss die ImageView schon in einem anderen Layout vorhanden sein?

    Bei mir stürzt er jedes mal bei addView ab.
     
  5. Riffer, 15.10.2010 #5
    Riffer

    Riffer Android-Lexikon

    Beiträge:
    1,261
    Erhaltene Danke:
    220
    Registriert seit:
    06.03.2010
    Phone:
    OnePlus Two
    Tablet:
    Samsung Galaxy NotePRO 12.2
    In so einem Fall wäre ein LogCat nützlich.
     
  6. HeroUser96, 15.10.2010 #6
    HeroUser96

    HeroUser96 Threadstarter Android-Hilfe.de Mitglied

    Beiträge:
    53
    Erhaltene Danke:
    2
    Registriert seit:
    15.09.2010
    Hier der LogCat:

    10-15 12:39:06.856: DEBUG/BatteryService(82): isUsbConnected() = true
    10-15 12:39:06.866: DEBUG/WifiService(82): ACTION_BATTERY_CHANGED pluggedType: 2
    10-15 12:39:06.866: DEBUG/BatteryService(82): mPlugType = 2
    10-15 12:39:06.906: DEBUG/UsbConnectedReceiver(23462): action = psclient.intent.action.usb_status
    10-15 12:39:06.906: DEBUG/UsbConnectedReceiver(23462): ACTION_BATTERY_CHANGED
    10-15 12:39:06.906: DEBUG/UsbConnectedReceiver(23462): usbCurrentType = 2
    10-15 12:39:06.906: DEBUG/UsbConnectedReceiver(23462): Current type is same as previous, return!
    10-15 12:39:44.906: DEBUG/KeyguardViewMediator(82): wakeWhenReadyLocked(6)
    10-15 12:39:44.906: DEBUG/KeyguardViewMediator(82): mWakeAndHandOff.acquire() -- start
    10-15 12:39:44.906: DEBUG/KeyguardViewMediator(82): mWakeAndHandOff.acquire() -- end
    10-15 12:39:44.906: DEBUG/KeyguardViewMediator(82): handleWakeWhenReady(6)
    10-15 12:39:44.916: DEBUG/KeyguardViewMediator(82): pokeWakelock(15000)
    10-15 12:39:44.916: INFO/power(82): *** set_screen_state 1
    10-15 12:39:44.956: DEBUG/PowerManagerService(82): PowerManagerService, no ringing
    10-15 12:39:44.966: INFO/HtcLockScreen(82): HtcLockScreen:onResume
    10-15 12:39:45.026: WARN/dalvikvm(82): disableGcForExternalAlloc: false
    10-15 12:39:45.086: DEBUG/BluetoothA2dpService(82): Received intent with action: android.intent.action.SCREEN_ON
    10-15 12:39:45.236: DEBUG/SurfaceFlinger(82): Screen about to return, flinger = 0x141e28
    10-15 12:39:45.236: DEBUG/SurfaceFlinger(82): screen returned
    10-15 12:39:45.246: DEBUG/SurfaceFlinger(82): wakeup flip
    10-15 12:39:45.266: INFO/BatteryStatsImpl(82): notePhoneSignalStrengthLocked: 4->3
    10-15 12:39:45.306: DEBUG/BT HS/HF(151): gsmAsuToSignal=10
    10-15 12:39:45.506: DEBUG/WifiService(82): ACTION_SCREEN_ON
    10-15 12:39:45.567: DEBUG/WifiService(82): enable and start wifi due to updateWifiState
    10-15 12:39:45.756: INFO/HtcLockScreen(82): touch ACTION_DOWN
    10-15 12:39:45.996: INFO/HtcLockScreen(82): touch ACTION_UP
    10-15 12:39:45.996: WARN/dalvikvm(82): disableGcForExternalAlloc: true
    10-15 12:39:45.996: DEBUG/HtcLockScreen(82): unlock animationn done
    10-15 12:39:46.146: INFO/HtcLockScreen(82): Drag down to unlock screen
    10-15 12:39:46.146: WARN/dalvikvm(82): disableGcForExternalAlloc: false
    10-15 12:39:46.196: INFO/HtcLockScreen(82): HtcLockScreen:onPause
    10-15 12:39:46.337: DEBUG/AndroidRuntime(28683): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
    10-15 12:39:46.337: DEBUG/AndroidRuntime(28683): CheckJNI is OFF
    10-15 12:39:46.346: DEBUG/dalvikvm(28683): Precise GC configured ON
    10-15 12:39:46.686: DEBUG/AndroidRuntime(28683): --- registering native functions ---
    10-15 12:39:47.136: DEBUG/KeyguardViewMediator(82): pokeWakelock(7000)
    10-15 12:39:47.446: DEBUG/KeyguardViewMediator(82): pokeWakelock(7000)
    10-15 12:39:47.576: DEBUG/HtcLockScreen(82): onScreenPause
    10-15 12:39:47.576: DEBUG/KeyguardViewManager(82): hide()
    10-15 12:39:47.586: DEBUG/ViewFlipper(9501): updateRunning() mVisible=true, mStarted=true, mUserPresent=true, mRunning=true
    10-15 12:39:47.666: WARN/InputManagerService(82): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@44f90a08
    10-15 12:39:48.496: DEBUG/JBtlRBtlServicesNative(28683): register_android_server_JBtlRbtlServicesNative: Entered
    10-15 12:39:48.526: DEBUG/JBtlCommonNative(28683): register_android_server_JBtlCommonNative: Entered
    10-15 12:39:48.546: DEBUG/(28683): Entered register_android_server_JBtlBmgNative
    10-15 12:39:48.556: DEBUG/JBtlA2dpNative(28683): <<<< Entered register_android_server_JBtlA2dpNative >>>>
    10-15 12:39:48.556: DEBUG/JBtlAgNative(28683): <<<< Entered register_android_server_JBtlAgNative >>>>
    10-15 12:39:48.556: DEBUG/(28683): Entered register_android_server_JBtlAvrcptgNative
    10-15 12:39:48.566: DEBUG/JBtlPbapsNative(28683): Entered register_android_server_JBtlPbapsNative
    10-15 12:39:48.586: DEBUG/JBthalPbNative(28683): Entered register_android_server_JBthalPbNative
    10-15 12:39:48.596: DEBUG/JBtlSppNative(28683): <<<< Entered register_android_server_JBtlSppNative >>>>
    10-15 12:39:48.606: DEBUG/JBtlOppcNative(28683): <<<< Entered register_android_server_JBtlOppcNative >>>>
    10-15 12:39:48.616: DEBUG/JBtlOppsNative(28683): <<<< Entered register_android_server_JBtlOppsNative >>>>
    10-15 12:39:48.626: DEBUG/JBtlFtpsNative(28683): <<<< Entered register_android_server_JBtlFtpsNative >>>>
    10-15 12:39:48.626: DEBUG/(28683): <<<< Entered register_android_server_JFmRxNative() >>>>
    10-15 12:39:48.936: DEBUG/dalvikvm(28550): GC freed 1233 objects / 190632 bytes in 176ms
    10-15 12:39:49.176: DEBUG/AndroidRuntime(28683): Shutting down VM
    10-15 12:39:49.176: DEBUG/dalvikvm(28683): DestroyJavaVM waiting for non-daemon threads to exit
    10-15 12:39:49.176: DEBUG/dalvikvm(28683): DestroyJavaVM shutting VM down
    10-15 12:39:49.176: ERROR/AndroidRuntime(28683): ERROR: thread attach failed
    10-15 12:39:49.176: DEBUG/dalvikvm(28683): HeapWorker thread shutting down
    10-15 12:39:49.186: DEBUG/dalvikvm(28683): HeapWorker thread has shut down
    10-15 12:39:49.186: DEBUG/jdwp(28683): JDWP shutting down net...
    10-15 12:39:49.186: INFO/dalvikvm(28683): Debugger has detached; object registry had 1 entries
    10-15 12:39:49.186: DEBUG/dalvikvm(28683): VM cleaning up
    10-15 12:39:49.226: DEBUG/dalvikvm(28550): GC freed 480 objects / 186152 bytes in 204ms
    10-15 12:39:49.296: DEBUG/dalvikvm(28683): LinearAlloc 0x0 used 653988 of 5242880 (12%)
    10-15 12:39:49.386: DEBUG/gralloc(82): allocating GPU size=614400, offset=0
    10-15 12:39:49.396: DEBUG/gralloc(82): allocating GPU size=614400, offset=614400
    10-15 12:39:51.356: DEBUG/AndroidRuntime(28697): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
    10-15 12:39:51.376: DEBUG/AndroidRuntime(28697): CheckJNI is OFF
    10-15 12:39:51.396: DEBUG/dalvikvm(28697): Precise GC configured ON
    10-15 12:39:52.366: DEBUG/AndroidRuntime(28697): --- registering native functions ---
    10-15 12:39:53.996: DEBUG/JBtlRBtlServicesNative(28697): register_android_server_JBtlRbtlServicesNative: Entered
    10-15 12:39:54.006: DEBUG/JBtlCommonNative(28697): register_android_server_JBtlCommonNative: Entered
    10-15 12:39:54.006: DEBUG/(28697): Entered register_android_server_JBtlBmgNative
    10-15 12:39:54.036: DEBUG/JBtlA2dpNative(28697): <<<< Entered register_android_server_JBtlA2dpNative >>>>
    10-15 12:39:54.056: DEBUG/JBtlAgNative(28697): <<<< Entered register_android_server_JBtlAgNative >>>>
    10-15 12:39:54.056: DEBUG/(28697): Entered register_android_server_JBtlAvrcptgNative
    10-15 12:39:54.096: DEBUG/JBtlPbapsNative(28697): Entered register_android_server_JBtlPbapsNative
    10-15 12:39:54.106: DEBUG/JBthalPbNative(28697): Entered register_android_server_JBthalPbNative
    10-15 12:39:54.106: DEBUG/JBtlSppNative(28697): <<<< Entered register_android_server_JBtlSppNative >>>>
    10-15 12:39:54.126: DEBUG/JBtlOppcNative(28697): <<<< Entered register_android_server_JBtlOppcNative >>>>
    10-15 12:39:54.136: DEBUG/JBtlOppsNative(28697): <<<< Entered register_android_server_JBtlOppsNative >>>>
    10-15 12:39:54.146: DEBUG/JBtlFtpsNative(28697): <<<< Entered register_android_server_JBtlFtpsNative >>>>
    10-15 12:39:54.176: DEBUG/(28697): <<<< Entered register_android_server_JFmRxNative() >>>>
    10-15 12:39:54.536: DEBUG/dalvikvm(28550): GC freed 48 objects / 1744 bytes in 225ms
    10-15 12:39:55.406: WARN/dalvikvm(82): disableGcForExternalAlloc: true
    10-15 12:39:55.416: INFO/ActivityManager(82): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=de.test/.Test }
    10-15 12:39:55.526: DEBUG/AndroidRuntime(28697): Shutting down VM
    10-15 12:39:55.526: DEBUG/dalvikvm(28697): DestroyJavaVM waiting for non-daemon threads to exit
    10-15 12:39:55.526: DEBUG/dalvikvm(28697): DestroyJavaVM shutting VM down
    10-15 12:39:55.526: DEBUG/dalvikvm(28697): HeapWorker thread shutting down
    10-15 12:39:55.536: DEBUG/dalvikvm(28697): HeapWorker thread has shut down
    10-15 12:39:55.536: DEBUG/jdwp(28697): JDWP shutting down net...
    10-15 12:39:55.536: INFO/dalvikvm(28697): Debugger has detached; object registry had 1 entries
    10-15 12:39:55.536: DEBUG/dalvikvm(28697): VM cleaning up
    10-15 12:39:55.566: ERROR/AndroidRuntime(28697): ERROR: thread attach failed
    10-15 12:39:55.656: DEBUG/gralloc(82): freeing GPU buffer at 0
    10-15 12:39:55.656: DEBUG/gralloc(82): freeing GPU buffer at 614400
    10-15 12:39:55.726: DEBUG/dalvikvm(28697): LinearAlloc 0x0 used 664124 of 5242880 (12%)
    10-15 12:39:55.746: INFO/ActivityManager(82): Start proc de.test for activity de.test/.Test: pid=28704 uid=10087 gids={1015}
    10-15 12:39:55.856: DEBUG/ddm-heap(28704): Got feature list request
    10-15 12:39:55.906: DEBUG/Sensors(82): open_akm, fd=202
    10-15 12:39:55.926: DEBUG/AK8973(62): Compass Start
    10-15 12:39:55.966: WARN/dalvikvm(82): disableGcForExternalAlloc: false
    10-15 12:39:56.196: DEBUG/ViewFlipper(9501): updateRunning() mVisible=false, mStarted=true, mUserPresent=true, mRunning=false
    10-15 12:39:56.346: INFO/ActivityManager(82): Displayed activity de.test/.Test: 639 ms (total 639 ms)
    10-15 12:39:58.237: DEBUG/AndroidRuntime(28704): Shutting down VM
    10-15 12:39:58.237: WARN/dalvikvm(28704): threadid=3: thread exiting with uncaught exception (group=0x4001e390)
    10-15 12:39:58.237: ERROR/AndroidRuntime(28704): Uncaught handler: thread main exiting due to uncaught exception
    10-15 12:39:58.266: ERROR/AndroidRuntime(28704): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
    10-15 12:39:58.266: ERROR/AndroidRuntime(28704): at android.view.ViewGroup.addViewInner(ViewGroup.java:1915)
    10-15 12:39:58.266: ERROR/AndroidRuntime(28704): at android.view.ViewGroup.addView(ViewGroup.java:1810)
    10-15 12:39:58.266: ERROR/AndroidRuntime(28704): at android.view.ViewGroup.addView(ViewGroup.java:1767)
    10-15 12:39:58.266: ERROR/AndroidRuntime(28704): at android.view.ViewGroup.addView(ViewGroup.java:1747)
    10-15 12:39:58.266: ERROR/AndroidRuntime(28704): at de.test.Test.onAnimationEnd(Test.java:92)
    10-15 12:39:58.266: ERROR/AndroidRuntime(28704): at android.view.animation.Animation.getTransformation(Animation.java:749)
    10-15 12:39:58.266: ERROR/AndroidRuntime(28704): at android.view.ViewGroup.drawChild(ViewGroup.java:1450)
    10-15 12:39:58.266: ERROR/AndroidRuntime(28704): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1312)
    10-15 12:39:58.266: ERROR/AndroidRuntime(28704): at android.view.ViewGroup.drawChild(ViewGroup.java:1583)
    10-15 12:39:58.266: ERROR/AndroidRuntime(28704): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1312)
    10-15 12:39:58.266: ERROR/AndroidRuntime(28704): at android.view.ViewGroup.drawChild(ViewGroup.java:1583)
    10-15 12:39:58.266: ERROR/AndroidRuntime(28704): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1312)
    10-15 12:39:58.266: ERROR/AndroidRuntime(28704): at android.view.View.draw(View.java:6538)
    10-15 12:39:58.266: ERROR/AndroidRuntime(28704): at android.widget.FrameLayout.draw(FrameLayout.java:352)
    10-15 12:39:58.266: ERROR/AndroidRuntime(28704): at android.view.ViewGroup.drawChild(ViewGroup.java:1585)
    10-15 12:39:58.266: ERROR/AndroidRuntime(28704): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1312)
    10-15 12:39:58.266: ERROR/AndroidRuntime(28704): at android.view.ViewGroup.drawChild(ViewGroup.java:1583)
    10-15 12:39:58.266: ERROR/AndroidRuntime(28704): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1312)
    10-15 12:39:58.266: ERROR/AndroidRuntime(28704): at android.view.View.draw(View.java:6538)
    10-15 12:39:58.266: ERROR/AndroidRuntime(28704): at android.widget.FrameLayout.draw(FrameLayout.java:352)
    10-15 12:39:58.266: ERROR/AndroidRuntime(28704): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1866)
    10-15 12:39:58.266: ERROR/AndroidRuntime(28704): at android.view.ViewRoot.draw(ViewRoot.java:1364)
    10-15 12:39:58.266: ERROR/AndroidRuntime(28704): at android.view.ViewRoot.performTraversals(ViewRoot.java:1118)
    10-15 12:39:58.266: ERROR/AndroidRuntime(28704): at android.view.ViewRoot.handleMessage(ViewRoot.java:1650)
    10-15 12:39:58.266: ERROR/AndroidRuntime(28704): at android.os.Handler.dispatchMessage(Handler.java:99)
    10-15 12:39:58.266: ERROR/AndroidRuntime(28704): at android.os.Looper.loop(Looper.java:123)
    10-15 12:39:58.266: ERROR/AndroidRuntime(28704): at android.app.ActivityThread.main(ActivityThread.java:4595)
    10-15 12:39:58.266: ERROR/AndroidRuntime(28704): at java.lang.reflect.Method.invokeNative(Native Method)
    10-15 12:39:58.266: ERROR/AndroidRuntime(28704): at java.lang.reflect.Method.invoke(Method.java:521)
    10-15 12:39:58.266: ERROR/AndroidRuntime(28704): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
    10-15 12:39:58.266: ERROR/AndroidRuntime(28704): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
    10-15 12:39:58.266: ERROR/AndroidRuntime(28704): at dalvik.system.NativeStart.main(Native Method)

    10-15 12:39:58.276: INFO/Process(82): Sending signal. PID: 28704 SIG: 3
    10-15 12:39:58.276: INFO/dalvikvm(28704): threadid=7: reacting to signal 3
    10-15 12:39:58.306: INFO/dalvikvm(28704): Wrote stack trace to '/data/anr/traces.txt'
    10-15 12:40:00.076: INFO/BatteryStatsImpl(82): notePhoneSignalStrengthLocked: 3->4
    10-15 12:40:00.136: DEBUG/BT HS/HF(151): gsmAsuToSignal=25
    10-15 12:40:00.707: INFO/Process(28704): Sending signal. PID: 28704 SIG: 9
    10-15 12:40:00.726: INFO/WindowManager(82): WIN DEATH: Window{44df16b8 de.test/de.test.Test paused=false}
    10-15 12:40:00.726: DEBUG/ActivityManager(82): foreground app died, app = de.test
    10-15 12:40:00.726: INFO/ActivityManager(82): Process de.test (pid 28704) has died.
    10-15 12:40:00.736: ERROR/ActivityManager(82): fail to set top app changed!
    10-15 12:40:00.776: DEBUG/ViewFlipper(9501): updateRunning() mVisible=false, mStarted=true, mUserPresent=true, mRunning=false
    10-15 12:40:00.796: DEBUG/Sensors(82): close_akm, fd=202
    10-15 12:40:00.836: DEBUG/ViewFlipper(9501): updateRunning() mVisible=true, mStarted=true, mUserPresent=true, mRunning=true
    10-15 12:40:00.846: INFO/UsageStats(82): Unexpected resume of com.htc.launcher while already resumed in de.test
    10-15 12:40:00.886: WARN/dalvikvm(82): disableGcForExternalAlloc: false
    10-15 12:40:00.976: WARN/InputManagerService(82): Got RemoteException sending setActive(false) notification to pid 28704 uid 10087
    10-15 12:40:01.506: DEBUG/AK8973(62): Compass CLOSE
    10-15 12:40:06.076: INFO/BatteryStatsImpl(82): notePhoneSignalStrengthLocked: 4->3
    10-15 12:40:06.096: DEBUG/BT HS/HF(151): gsmAsuToSignal=12

    hoffe das hilft.
     
  7. DieGoldeneMitte, 15.10.2010 #7
    DieGoldeneMitte

    DieGoldeneMitte Android-Lexikon

    Beiträge:
    1,230
    Erhaltene Danke:
    256
    Registriert seit:
    05.02.2010
    Phone:
    Nexus 5X
    Tablet:
    Nexus 7 (2013)
    10-15 12:39:58.266: ERROR/AndroidRuntime(28704): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

    Da stehts.
    Der neue View hat den falschen Parent.
    Klingt so als hättest Du die Reihenfolge von setContentView und View-Erzeugung verbockt :)
     
  8. HeroUser96, 15.10.2010 #8
    HeroUser96

    HeroUser96 Threadstarter Android-Hilfe.de Mitglied

    Beiträge:
    53
    Erhaltene Danke:
    2
    Registriert seit:
    15.09.2010
    Danke jetzt läufts

    was man nicht alles lernen kann (z.B. dass man nächstes Mal zuerst ins LogCat schauen sollte :D )
     

Diese Seite empfehlen