2012-05-08 15 views
6

नहीं बढ़ रहा है मैं xml के माध्यम से अपना कस्टम व्यू बनाने की कोशिश कर रहा हूं, लेकिन स्क्रीन मेरे दृश्य को नहीं दिखाती है, यह फुला नहीं रही है। के रूप मेंकस्टम व्यू

<?xml version="1.0" encoding="utf-8"?> 
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/profileSwitcher" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#FFFFFF" > 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <ImageView 
      android:id="@+id/imgAdvertise" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:src="@android:drawable/btn_minus" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" > 

     <TextView 
      android:id="@+id/txtAdvertise" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:text="asdas" /> 
    </LinearLayout> 

</ViewSwitcher> 

मैं अपने कस्टम View वर्ग है:: मेरे कस्टम दृश्य एक्सएमएल के रूप में है

public class MyCustomView extends View{ 

    TextView textAdvertise; 
    ImageView imageAdvertise; 
    View view; 
    ViewSwitcher switcher ; 

    public MyCustomView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     // TODO Auto-generated constructor stub 

     LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     if (layoutInflater != null) { 
      view = layoutInflater.inflate(R.layout.main, null); 

     } 

     initialize(); 
    } 

    public void initialize() { 
     // TODO Auto-generated method stub 

     textAdvertise = (TextView) view.findViewById(R.id.txtAdvertise); 
     imageAdvertise = (ImageView) view.findViewById(R.id.imgAdvertise); 
     switcher = (ViewSwitcher) view.findViewById(R.id.profileSwitcher); 
     startAnimation(); 

    } 

    public void startAnimation() { 
     // TODO Auto-generated method stub 

     new Thread() { 
      public void run() { 

       for (;;) { 
        try { 

         Thread.sleep(2000); 
         hRefresh.sendEmptyMessage(5); 
        } catch (Exception e) { 
        } 
       } 
      } 
     }.start(); 
    } 

    Handler hRefresh = new Handler() { 
     public void handleMessage(Message msg) { 
      switch (msg.what) { 
      case 5: 
       switcher.showNext(); 
       // To go back to the first view, use switcher.showPrevious() 
       break; 
      default: 
       break; 
      } 
     } 
    }; 

} 

और मेरे आधार XML लेआउट में मैं View दिखाने के लिए जहां, एक बटन और और मेरे View है के रूप में:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<Button 
    android:id="@+id/button1" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:text="Button" 
    android:layout_weight="1"/> 

<com.example.viewswitcher.MyCustomView 
    android:id="@+id/MyView" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 

    /> 

किसी को भी कर सकते हैं कृपया मुझे बताओ कि यह क्या गलत हो रहा है?

उत्तर

9

आप कुछ भी नहीं देखेंगे क्योंकि MyCustomView आपके द्वारा लिखी गई कोई सामग्री नहीं है। सबसे पहले मैं के बजाय ViewViewGroups बच्चों में से एक (जैसे LinearLayout, RelativeLayout आदि) का प्रयोग करेंगे:

public class MyCustomView extends LinearLayout { 

    TextView textAdvertise; 
    ImageView imageAdvertise; 
    View view; 
    ViewSwitcher switcher ; 

    public MyCustomView(Context context, AttributeSet attrs) { 
     super(context); 
    } 

    public MyCustomView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     if (layoutInflater != null) { 
      view = layoutInflater.inflate(R.layout.main, this, true); 
     } 
     initialize(); 
    } 

    public MyCustomView(Context context, AttributeSet attrs, int theme) { 
     super(context, attrs, theme); 
    } 
// ... 
+1

हे मदद के लिए धन्यवाद .. यह काम किया .. असल में मेरे मेरे लेआउट inflator मैंने लिखा है = layoutInflater.inflate (आर .layout.main, शून्य); यह शून्य समस्या का कारण बनता है .. इसके बजाय मैंने "यह" रखा यह काम किया .. धन्यवाद दोस्त !! – NullPointerException

+0

आपने जो प्रश्न संपादित किया है ... और एक और बात यह काम करती है क्योंकि मैंने देखने के बजाए लीनियरलाउट बढ़ाया है .. ऐसा क्यों ?? इस विवरण के बारे में कोई भी लिंक या पोस्ट .. धन्यवाद – NullPointerException

+2

@ रश्मी मैंने आपके प्रश्न को कोड के कोड को बेहतर प्रारूपित करने के लिए संपादित किया है। 'व्यू' क्लास एक 'व्यू' ऑब्जेक्ट का प्रतिनिधित्व करती है, यह बच्चों को 'फुटेज' स्वीकार नहीं करती है जैसे आप इसे 'inflate' विधि' में जोड़ना चाहते हैं (वास्तव में यदि आप 'inflate' विधि को देखते हैं दस्तावेज़ आप देखेंगे कि दूसरा पैरामीटर 'व्यू ग्रुप' है)। इसके बजाय 'व्यू ग्रुप' ('लीनियरलाउट' 'व्यू ग्रुप' का उप-वर्ग है)' व्यू 'का प्रतिनिधित्व करता है (वास्तव में यह सुपर क्लास' व्यू' का उप-वर्ग है) जो बच्चों के दृश्यों को स्वीकार करता है। मुझे कोई लिंक नहीं पता लेकिन आप 'व्यू' और 'व्यू ग्रुप' के लिए दस्तावेज़ देख सकते हैं। – Luksprog

0

मुझे लगता है कि तुम क्या जरूरत है एक कस्टम दृश्य नहीं है। शायद आपको दृश्य के इंस्टेंट फ्रैगमेंट क्लास से उत्तराधिकारी होना चाहिए।

अपने Fragment को कार्यान्वित करने के लिए here देखें।

+0

क्षमा करें टुकड़े मेरे मामले में उपयोगी नहीं हैं .. क्योंकि मुझे एंड्रॉइड 2.1 – NullPointerException

+0

से अपने ऐप को संगतता देना है, आप एंड्रॉइड सपोर्ट पैकेज का उपयोग कर सकते हैं। [लिंक] (http://developer.android.com/sdk/compatibility-library.html)। न्यूनतम संस्करण 4 है (1.6, मुझे लगता है)। – gutiory

3

अपना लेआउट बढ़ाने के लिए इसका उपयोग करें। संपादित

View headerView = View.inflate(this, R.layout.layout_name, null); 

यह अपने xml फ़ाइल की मूल लेआउट वापस आ जाएगी तो आप सीधे उपयोग कर सकते हैं या आप

संपादित

headerView.findViewById(R.id.view_id); 

का उपयोग करके किसी अन्य दृश्य प्राप्त कर सकते हैं उम्मीद है कि यह

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