2013-07-26 6 views
28

से माता पिता के दृश्य प्राप्त मैं इस लेआउट के साथ एक FragmentActivity है:एक लेआउट

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

कैसे करें ScrollerLinearLayout से उपयोग करने के लिए:

<?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> 

और ScrollerLinearLayout वर्ग कुछ इस तरह है प्राप्त करने के लिए पैरेंट लेआउट - उदाहरण के लिए - editetxt? FragmentActivity और ScrollerLineraLayout एक ही परियोजना के विभिन्न पैकेजों में हैं। मैंने फ़ंक्शन को ScrollerLinearLayout के भीतर इस तरह से उपयोग करने का प्रयास किया लेकिन यह काम नहीं करता है।

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

कोई मेरी मदद करता है? धन्यवाद!

+2

वास्तव में क्या काम नहीं करता है लिखना? –

+0

@FD_ यह 'RelativeLayout r = (RelativeLayout) ((ViewGroup) पर एक nullpointerexception है। (getGarent())। GetParent();' –

+0

जहां 'स्क्रॉलरलाइनरआउट' में आप इस कोड को कॉल करते हैं? –

उत्तर

59

getParent विधि ViewParent देता है, View नहीं।

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

के रूप में ओ पी द्वारा टिप्पणी में कहा गया है, यह एक NPE खड़ी कर रहा है: आप getParent() भी करने के लिए पहली कॉल कास्ट करने के लिए की जरूरत है। डिबग करने के लिए, यह कई भागों में विभाजित:

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

मैंने ऐसा करने की कोशिश की लेकिन यह काम नहीं करता है। त्रुटि 'RelativeLayout r = (RelativeLayout) ((ViewGroup) पर इस nullpointexception है। (getGarent())। getParent(); 'मुझे समझ में नहीं आता कि क्या हो सकता है ... –

+0

@Rajin, मैंने अपनी पोस्ट का एक अनुभाग जोड़ा इस मुद्दे को डीबग करने में आपकी सहायता के लिए। – Phil

+0

मैंने लॉग की जांच की और मुझे पता चला कि 'व्यूपेरेंट पैरेंट = this.getParent();' शून्य लौटाता है। मेरे पास यह 'पब्लिक क्लास स्क्रॉलरलाइनरआउट है जो लाइनरलाइट {रिलेवेटिवआउट आर; // कुछ कोड सार्वजनिक शून्य init() {// आपका कोड}} 'सही है जहां मैं' getParent() 'विधि को कॉल करता हूं? –

4

आप तो इस तरह यह कर की कोशिश अपने Fragment से एक View खोजने की कोशिश कर रहे हैं, तो:

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

'getActivity()' विधि को स्क्रॉलरलिनेरालाइट प्रकार के लिए अपरिभाषित किया गया है जो लाइनरलायआउट क्लास –

0

RelativeLayout (यानी ViewParent) होना चाहिए लेआउट फ़ाइल में परिभाषित संसाधन आईडी है (उदाहरण के लिए, android:[email protected]+id/myParentViewId)। यदि आप ऐसा नहीं करते हैं, तो आईडी प्राप्त करने के लिए कॉल शून्य हो जाएगा। अधिक जानकारी के लिए this answer पर देखें।

-2

बस

this.getRootView(); 
+0

तक फैलाता है, यह केवल पेरेंटिंग व्यू को वापस लेता है जैसे वास्तविक लेआउट नहीं, केवल स्तर एक माता-पिता – Thecarisma

संबंधित मुद्दे