5

मुझे stackoverflow पर कस्टम लाइनरलायआउट का यह उदाहरण मिला, लेकिन जब मैं इसे चलाने का प्रयास करता हूं तो यह त्रुटियों को फेंकता है, क्या कोई इसे ढूंढ सकता है?एक्सएमएल को पार्स करने में त्रुटि: कस्टम लाइनरलाइट पर अनबाउंड उपसर्ग

कस्टम LinearLayout:

package com.example.androidapp.widgets; 

import android.content.Context; 
import android.content.res.TypedArray; 
import android.util.AttributeSet; 
import android.view.View; 
import android.view.animation.Animation; 
import android.view.animation.Transformation; 
import android.widget.LinearLayout; 

public class ExpandablePanel extends LinearLayout { 

    private final int mHandleId; 
    private final int mContentId; 

    private View mHandle; 
    private View mContent; 

    private boolean mExpanded = true; 
    private int mCollapsedHeight = 0; 
    private int mContentHeight = 0; 

    public ExpandablePanel(Context context) { 
     this(context, null); 
    } 

    public ExpandablePanel(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     TypedArray a = context.obtainStyledAttributes(attrs, 
      R.styleable.ExpandablePanel, 0, 0); 

     // How high the content should be in "collapsed" state 
     mCollapsedHeight = (int) a.getDimension(
      R.styleable.ExpandablePanel_collapsedHeight, 0.0f); 

     int handleId = a.getResourceId(R.styleable.ExpandablePanel_handle, 0); 
     if (handleId == 0) { 
      throw new IllegalArgumentException(
       "The handle attribute is required and must refer " 
        + "to a valid child."); 
     } 

     int contentId = a.getResourceId(R.styleable.ExpandablePanel_content, 0); 
     if (contentId == 0) { 
      throw new IllegalArgumentException(
       "The content attribute is required and must refer " 
        + "to a valid child."); 
     } 

     mHandleId = handleId; 
     mContentId = contentId; 

     a.recycle(); 
    } 

    @Override 
    protected void onFinishInflate() { 
     super.onFinishInflate(); 

     mHandle = findViewById(mHandleId); 
     if (mHandle == null) { 
      throw new IllegalArgumentException(
       "The handle attribute is must refer to an" 
        + " existing child."); 
     } 

     mContent = findViewById(mContentId); 
     if (mContent == null) { 
      throw new IllegalArgumentException(
       "The content attribute is must refer to an" 
        + " existing child."); 
     } 

     mHandle.setOnClickListener(new PanelToggler()); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     if (mContentHeight == 0) { 
      // First, measure how high content wants to be 
      mContent.measure(widthMeasureSpec, MeasureSpec.UNSPECIFIED); 
      mContentHeight = mContent.getMeasuredHeight(); 
     } 

     // Then let the usual thing happen 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    } 

    private class PanelToggler implements OnClickListener { 
     @Override 
     public void onClick(View v) { 
      Animation a; 
      if (mExpanded) { 
       a = new ExpandAnimation(mContentHeight, mCollapsedHeight); 
      } else { 
       a = new ExpandAnimation(mCollapsedHeight, mContentHeight); 
      } 
      a.setDuration(500); 
      mContent.startAnimation(a); 
      mExpanded = !mExpanded; 
     } 
    } 

    private class ExpandAnimation extends Animation { 
     private final int mStartHeight; 
     private final int mDeltaHeight; 

     public ExpandAnimation(int startHeight, int endHeight) { 
      mStartHeight = startHeight; 
      mDeltaHeight = endHeight - startHeight; 
     } 

     @Override 
     protected void applyTransformation(float interpolatedTime, 
      Transformation t) { 
      android.view.ViewGroup.LayoutParams lp = mContent.getLayoutParams(); 
      lp.height = (int) (mStartHeight + mDeltaHeight * interpolatedTime); 
      mContent.setLayoutParams(lp); 
     } 

     @Override 
     public boolean willChangeBounds() { 
      // TODO Auto-generated method stub 
      return true; 
     } 
    } 
} 

रेस> मान> attrs.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="ExpandablePanel"> 
    <attr name="handle" format="reference" /> 
    <attr name="content" format="reference" /> 
    <attr name="collapsedHeight" format="dimension" /> 
    </declare-styleable> 
</resources> 

main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:cl="http://schemas.android.com/apk/lib/com.example.androidapp.widgets" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

<com.example.androidapp.widgets.ExpandablePanel 
    android:orientation="vertical" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    cl:handle="@+id/expand" 
    cl:content="@+id/value" 
    cl:collapsedHeight="50dip"> 
    <TextView 
     android:id="@id/value" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:maxHeight="50dip" 
     /> 
    <Button 
     android:id="@id/expand" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="More" /> 
</com.example.androidapp.widgets.ExpandablePanel> 


</LinearLayout> 

गतिविधि वर्ग:

package com.example.androidapp.widgets; 

import android.app.Activity; 
import android.os.Bundle; 

public class GoodPanelsActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 
} 

LogCat त्रुटि:

FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidapp.widgets/com.example.androidapp.widgets.GoodPanelsActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class com.example.androidapp.widgets.ExpandablePanel

संपादित करें: LogCat से

java.lang.reflect.InvocationTargetException at java.lang.reflect.Constructor.constructNative(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:415) at android.view.LayoutInflater.createView(LayoutInflater.java:505) Caused by: java.lang.IllegalArgumentException: The handle attribute is required and must refer to a valid child. at com.example.androidapp.widgets.ExpandablePanel.(ExpandablePanel.java:39)

उत्तर

6

अंत में मैं इस मुद्दे को पाया एक्सपेंडेबल पैनेल res के तहत मौजूद है।

चीयर्स।

+0

स्वीकार करें कि आप इसे उत्तर देते हैं, भले ही आपने इसे पोस्ट किया हो, –

+0

धन्यवाद, मुझे इसे स्वीकार करने की अनुमति नहीं देने के बाद, 20 घंटे बाद इसे स्वीकार कर लिया जाएगा –

0

अधिक आप परियोजना है कि इस वर्ग के com.example.androidapp.widgets.ExpandablePanel को शामिल जोड़ने की जरूरत है अपने परियोजना buildpath। यदि यह एक ओपन सोर्स प्रोजेक्ट नहीं है, या स्रोत उपलब्ध नहीं है तो आप ऐसा करने में सक्षम नहीं हैं।

यदि आपके पास यह प्रोजेक्ट है, और यह LibraryProject है तो आपको अपने Project->Properties->Android पर राइट क्लिक करना चाहिए और Library अनुभाग में उस प्रोजेक्ट को जोड़ें।

यदि सामान्य Project है तो बस Properties->Java Build Path->Projects पर जाएं और वहां से उस प्रोजेक्ट को जोड़ें। क्योंकि मैं बाहरी पुस्तकालयों & उपयोग नहीं कर रहा

xmlns:cl="http://schemas.android.com/apk/res/com.example.androidapp.widgets" 

: main.xml में रेखा से नीचे पर

देखो:

xmlns:cl="http://schemas.android.com/apk/lib/com.example.androidapp.widgets" 

यह होना चाहिए

+0

कक्षा विस्तारणीय पैनेला मेरी परियोजना का हिस्सा है, यह बाहरी वर्ग नहीं है। –

+0

ठीक है। शायद आपको कन्स्ट्रक्टर में एक त्रुटि है ... लॉगकैट से अधिक पेस्ट करें। –

+0

मैंने अपना प्रश्न अपडेट किया है, लॉगकैट से अधिक लॉग, कृपया इसे ढूंढें। –

-1

मैं इस स्क्रिप्ट को भी आजमा रहा था, क्योंकि मैं एंड्रॉइड प्रोग्रामिंग के लिए नया हूं और मैं सीखने के लिए कुछ अच्छे उदाहरण ढूंढ रहा था।

जब मैं मुख्य .xml में लेआउट जोड़ता हूं तो मेरा ऐप बल बंद हो जाता है।

जब मैं ExpendablePanel के लिए लेआउट जोड़ने के लिए, चित्रमय लेआउट धूसर हो जाता है और मैं इसे संपादित नहीं कर सकते:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:cl="http://schemas.android.com/apk/lib/com.martin.myapp.widgets" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

<com.martin.myapp.widgets.ExpandablePanel 
    android:orientation="vertical" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    cl:handle="@+id/expand" 
    cl:content="@+id/value" 
    cl:collapsedHeight="50dip"> 
    <TextView 
     android:id="@id/value" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:maxHeight="50dip" 
     /> 
    <Button 
     android:id="@id/expand" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="More" /> 
</com.martin.myapp.widgets.ExpandablePanel> 

तो यह अनिवार्य रूप से कस्टम विजेट के बिना एक खाली एप्लिकेशन है।

मैंने एक विस्तारणीय पैनेल बनाया।जावा और उस लिपि की प्रतिलिपि बनाई जो लाइनरलायआउट को वहां बढ़ाती है, मुझे कोई त्रुटि नहीं देती है। मेरा attrs.xml भी त्रुटि मुक्त है।

एक्लिप्स मुझे चलते समय कोई त्रुटि नहीं देता है। यह चलाता है और इसे मेरे फोन पर भेजता है। यही वह जगह है जहां यह बल बंद हो जाता है। जब मैं main.xml में ExpandablePanel लेआउट को हटा देता हूं तो यह चलता है और एक खाली ऐप है।

मैं वास्तव में इसे समझ नहीं सकता।

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