addView problem

  • 9 Antworten
  • Letztes Antwortdatum
I

iboshido

Neues Mitglied
0
hey leute, ich habe ein problem. Undzwar verstehe ich nicht wieso ich eine Linearlayout layout2 zu einem anderen Linearlayout hinzufügen kann mit "addView".

der Code sieht so aus, app stürtzt aber immer ab:
Code:
final LinearLayout layout1= new LinearLayout(this);
layout1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
layout1.setOrientation(0);//0 is horizantal
layout1.setBackgroundColor(Color.TRANSPARENT);

LinearLayout layout2= (LinearLayout) findViewById(R.id.layout_id);

layout1.addView(layout2);
 
Ich würde sagen das Problem ist, dass LinearLayout layout1 mit final deklariert ist.
 
123thomas schrieb:
Ich würde sagen das Problem ist, dass LinearLayout layout1 mit final deklariert ist.

ne habs schon ohne versucht, liegt nicht am final
 
Schreib mal die Logcat hier rein. Ich tippe mal auf eine NPE, aber ohne Logcat ist es schwer zu sagen.
 
03-23 12:21:51.343 1064-1064/? I/SELinux: Function: selinux_android_load_priority [0], There is no sepolicy file.

03-23 12:21:51.343 1064-1064/? I/SELinux: Function: selinux_android_load_priority [1], There is no sepolicy version file.

03-23 12:21:51.343 1064-1064/? I/SELinux: Function: selinux_android_load_priority , priority version is VE=SEPF_GT-N7100_4.4.2_0016


03-23 12:21:51.343 1064-1064/? I/SELinux: selinux_android_seapp_context_reload: seapp_contexts file is loaded from /seapp_contexts
03-23 12:21:51.343 1064-1064/? D/dalvikvm: Late-enabling CheckJNI
03-23 12:21:51.438 1064-1064/com.example.sautieim.test2 W/dalvikvm: No implementation found for native Ldalvik/system/VMRuntime;.pauseGc:(Ljava/lang/String;)I
03-23 12:21:51.443 1064-1064/com.example.sautieim.test2 E/ActivityThread: Pause GC
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.app.ActivityThread.pauseGC(ActivityThread.java:5525)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2324)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
at android.app.ActivityThread.access$900(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.UnsatisfiedLinkError: Native method not found: dalvik.system.VMRuntime.pauseGc:(Ljava/lang/String;)I
at dalvik.system.VMRuntime.pauseGc(Native Method)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.app.ActivityThread.pauseGC(ActivityThread.java:5525)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2324)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
at android.app.ActivityThread.access$900(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
03-23 12:21:51.453 1064-1064/com.example.sautieim.test2 W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
03-23 12:21:51.453 1064-1064/com.example.sautieim.test2 I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested
03-23 12:21:51.453 1064-1064/com.example.sautieim.test2 W/dalvikvm: VFY: unable to resolve interface method 15025: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
03-23 12:21:51.453 1064-1064/com.example.sautieim.test2 D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
03-23 12:21:51.453 1064-1064/com.example.sautieim.test2 I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode
03-23 12:21:51.453 1064-1064/com.example.sautieim.test2 W/dalvikvm: VFY: unable to resolve interface method 15029: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
03-23 12:21:51.453 1064-1064/com.example.sautieim.test2 D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
03-23 12:21:51.493 1064-1064/com.example.sautieim.test2 I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations
03-23 12:21:51.493 1064-1064/com.example.sautieim.test2 W/dalvikvm: VFY: unable to resolve virtual method 396: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
03-23 12:21:51.493 1064-1064/com.example.sautieim.test2 D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
03-23 12:21:51.493 1064-1064/com.example.sautieim.test2 I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType
03-23 12:21:51.493 1064-1064/com.example.sautieim.test2 W/dalvikvm: VFY: unable to resolve virtual method 418: Landroid/content/res/TypedArray;.getType (I)I
03-23 12:21:51.493 1064-1064/com.example.sautieim.test2 D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
03-23 12:21:51.523 1064-1064/com.example.sautieim.test2 D/AndroidRuntime: Shutting down VM
03-23 12:21:51.523 1064-1064/com.example.sautieim.test2 W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41d20c08)
03-23 12:21:51.523 1064-1064/com.example.sautieim.test2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sautieim.test2, PID: 1064
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sautieim.test2/com.example.sautieim.test2.MainActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
at android.app.ActivityThread.access$900(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:3770)
at android.view.ViewGroup.addView(ViewGroup.java:3623)
at android.view.ViewGroup.addView(ViewGroup.java:3568)
at android.view.ViewGroup.addView(ViewGroup.java:3544)
at com.example.sautieim.test2.MainActivity.onCreate(MainActivity.java:24)
at android.app.Activity.performCreate(Activity.java:5451)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
at android.app.ActivityThread.access$900(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
 
Ich würde sagen, dein Layout2 hat schon einen Parent in einer XML-Datei.

Zitat: Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
 
Kardroid schrieb:
Ich würde sagen, dein Layout2 hat schon einen Parent in einer XML-Datei.

Zitat: Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

Parents sehe ich hier in der xml datei vom layout2 nicht
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.sautieim.test2.MainActivity"
    android:id="@+id/layout_id"
    >


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</LinearLayout>
 
Kenn den Fehler. Bin damals verzweifelt daran.
Du musst deinem Layout2 einen anderen Namen geben!!!! Siehe Bild.
Z.b. test2




final LinearLayout layout1= new LinearLayout(this);
layout1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
layout1.setOrientation(0);//0 is horizantal
layout1.setBackgroundColor(Color.TRANSPARENT);

LinearLayout layout2= (LinearLayout) findViewById(R.id.test2);

layout1.addView(layout2);



http://fs5.directupload.net/images/160602/b274uus6.png
 
Der Fehler ist relativ simple.
Der Fehler tritt bei dieser Zeile auf:
LinearLayout layout2= (LinearLayout) findViewById(R.id.layout_id);

Durch diese Deklaration ist das Layout Teil des RootView. Und dieses fügst mit add nochmal in das Rootview ein. Das mag Android nicht. Sehe Fehlermeldung.
Wenn du nachträglich einen View einfügen möchtest, darf er im RootView nicht enthalten sein.
Stattdessen benutzt man die Klasse LayoutInflater LayoutInflater | Android Developers

Beispiel: Android-er: Add and Remove view dynamically
 
Zurück
Oben Unten