2013-07-26 15 views
28

ich eine FragmentActivity mit diesem Layout haben:erhalten Eltern Blick von einem Layout

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/menulabel" 
    android:textSize="24sp" /> 

<EditText 
    android:id="@+id/editText1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:ems="10" 
    android:inputType="text" 
    android:layout_below="@+id/textView1" 
    android:hint="@string/search_title_hint" /> 

<TextView 
    android:id="@+id/textView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/spinnersearch" 
    android:layout_below="@+id/editText1" /> 

<LinearLayout 
    android:id="@+id/layout1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/textView2" > 

    <Spinner 
     android:id="@+id/spinner_search" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:prompt="@string/spinnersearch" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/reset_search" /> 

</LinearLayout> 

<Button 
    android:id="@+id/button2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/buttonnewcat" 
    android:layout_below="@+id/layout1" /> 

<Button 
    android:id="@+id/button3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/buttondelcat" 
    android:layout_below="@+id/button2" /> 

<Button 
    android:id="@+id/button4" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/buttoneditcat" 
    android:layout_below="@+id/button3" /> 

<Button 
    android:id="@+id/button5" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/buttonnewtext" 
    android:layout_below="@+id/button4" /> 

<Button 
    android:id="@+id/button6" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/buttondeltext" 
    android:layout_below="@+id/button5" /> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <side.menu.scroll.ScrollerLinearLayout 
     android:id="@+id/menu_content_side_slide_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="45dp" 
      android:background="@drawable/ab_solid_example" 
      android:orientation="horizontal" > 

      <ImageView 
       android:id="@+id/main_tmp_button" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/actionbar_background" 
       android:src="@drawable/download" /> 

     </LinearLayout> 
     <FrameLayout 
      android:id="@+id/fragment_container" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@drawable/bg_groud" /> 

    </side.menu.scroll.ScrollerLinearLayout> 
</RelativeLayout> 

und der ScrollerLinearLayout Klasse ist dies so etwas wie:

public class ScrollerLinearLayout extends LinearLayout { 
    // code of the class 
} 

Wie aus ScrollerLinearLayout zugreifen das übergeordnete Layout, um zum Beispiel den Edit-Text zu bekommen? FragmentActivity und ScrollerLineraLayout sind in verschiedenen Paketen der gleichen Projekte. Ich habe versucht, die getParent() Funktion auf diese Weise innerhalb der ScrollerLinearLayout Funktion zu verwenden, aber es funktioniert nicht.

RelativeLayout r = (RelativeLayout) this.getParent().getParent(); 
int w = r.findViewById(R.id.editText1).getLayoutParams().width; 

Jemand mir helfen? Vielen Dank!

+2

Was genau nicht funktioniert? –

+0

@FD_ es ist eine Nullpointerexception auf 'RelativeLayout r = (RelativLayout) ((ViewGroup) this.getParent()). GetParent();' –

+0

Wo in 'ScrollerLinearLayout' nennen Sie diesen Code? –

Antwort

59

Die Methode getParent gibt eine ViewParent, keine View zurück. Sie müssen den ersten Anruf zu getParent() auch werfen:

RelativeLayout r = (RelativeLayout) ((ViewGroup) this.getParent()).getParent(); 

Wie von der OP in den Kommentaren erwähnt, das ein NPE verursacht. Zum Debuggen, teilen diese in mehrere Teile:

ViewParent parent = this.getParent(); 
RelativeLayout r; 
if (parent == null) { 
    Log.d("TEST", "this.getParent() is null"); 
} 
else { 
    if (parent instanceof ViewGroup) { 
     ViewParent grandparent = ((ViewGroup) parent).getParent(); 
     if (grandparent == null) { 
      Log.d("TEST", "((ViewGroup) this.getParent()).getParent() is null"); 
     } 
     else { 
      if (parent instanceof RelativeLayout) { 
       r = (RelativeLayout) grandparent; 
      } 
      else { 
       Log.d("TEST", "((ViewGroup) this.getParent()).getParent() is not a RelativeLayout"); 
      } 
     } 
    } 
    else { 
     Log.d("TEST", "this.getParent() is not a ViewGroup"); 
    } 
} 

//now r is set to the desired RelativeLayout. 
+0

Ich habe versucht, dies zu tun, aber es funktioniert nicht. der Fehler ist nullpointexception auf 'RelativeLayout r = (RelativeLayout) ((ViewGroup) this.getParent()). getParent();' Ich verstehe nicht, was sein kann ... –

+0

@Rajin, habe ich einen Abschnitt meines Beitrags hinzugefügt um Ihnen bei der Fehlersuche zu helfen. – Phil

+0

Ich habe das Protokoll überprüft und festgestellt, dass 'ViewParent parent = this.getParent();' null zurückgibt. Ich habe diese 'öffentliche Klasse ScrollerLinearLayout erweitert LinearLayout {RelativeLayout r; // irgendein Code public void init() {// dein Code}} 'ist richtig, wo ich die' getParent() 'Methode nenne? –

4

Wenn Sie versuchen, eine View von Ihrem Fragment dann zu finden versuchen, es so zu tun:

int w = ((EditText)getActivity().findViewById(R.id.editText1)).getLayoutParams().width; 
+0

Die Methode 'getActivity()' ist für den Typ ScrollerLineraLayout, der die LinearLayout-Klasse –

0

Die RelativeLayout (dh die ViewParent) sollte eine Ressourcen-ID in der Layoutdatei definiert haben (z. B. android:[email protected]+id/myParentViewId). Wenn Sie dies nicht tun, gibt der Aufruf von getId null zurück. Sehen Sie sich this answer für weitere Informationen an.

-2

Schreiben Sie einfach

this.getRootView(); 
+0

erweitert, nicht definiert. Dies gibt einfach die Parenting-Ansicht nicht das tatsächliche Layout zurück, sondern nur Ebene eins Elternteil – Thecarisma

Verwandte Themen