11

I have created my own custom spinner which I can access from "Custom and library views" as it implements View. But, the problem is that when drag my custom spinner from palette to use it in my main.xml, it throws "unhandled event loop exception". I don't know where is my mistake.

main.xml

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

<com.myspinner.Myspinner
android:id="@+id/myspinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
pref:MyEntries="@array/testarray" />

MySpinnerattributes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MySpinner">
        <attr name="MyEntries" format="reference"/>
    </declare-styleable>
</resources>

spinnerlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:text="@string/large_text" android:gravity="center"/>

Myspinner.java

    public class Myspinner extends Spinner{


    LayoutInflater inflater;
    CharSequence cs[]={"MySpinner"};
    String s[]={"MySpinner"};
    View row;
    TextView txt;


public Myspinner(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    setAttributes(attrs);    
    this.setAdapter(new MyAdapter(this.getContext(), R.layout.spinnerlayout,s));
    }



public Myspinner(Context context, AttributeSet attrs) {
    super(context, attrs);
    setAttributes(attrs);
    this.setAdapter(new MyAdapter(this.getContext(), R.layout.spinnerlayout,s));
}



public class MyAdapter extends ArrayAdapter<String>{


public MyAdapter(Context context, int textViewResourceId,String[] objects){
    super(context, textViewResourceId, objects);
      // TODO Auto-generated constructor stub
  }



  public View getDropDownView(int position, View convertView,ViewGroup parent){
    return getCustomView(position, convertView, parent);
  }



   public View getView(int position, View convertView, ViewGroup parent) {
    return getCustomView(position, convertView, parent);
   }



    public View getCustomView(int position, View convertView, ViewGroup parent) {

    inflater=(LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      row=inflater.inflate(R.layout.spinnerlayout, parent, false);
      txt=(TextView)row.findViewById(R.id.textView1);
      txt.setText(s[position]);
      return row;
    }
    }

       public void setAttributes(AttributeSet attrs){
       if (attrs != null) 
       {
       TypedArray a=getContext().obtainStyledAttributes(attrs,R.styleable.MySpinner);
       final int N = a.getIndexCount();
       for (int i = 0; i < N; ++i)
       {      
           int attr = a.getIndex(i);
           switch (attr)
           {
                 case R.styleable.MySpinner_MyEntries:
                 cs=a.getTextArray(attr);
                 s=new String[cs.length];
                 for (int j = 0; j < cs.length; ++j)
                 s[j]=(String)cs[j];
                 break;

                 default:break;
            }
        a.recycle();
        } 
            }
            }
            }

It won't give the error if you simply copy paste the main.xml code. It gives the error when I drag my custom spinner from the palette.

HitOdessit
  • 7,198
  • 4
  • 36
  • 59
Flake
  • 1,386
  • 17
  • 31
  • It is not your mistake....probably it is an eclipse bug... take a look at these links... [Here](http://stackoverflow.com/questions/9074189/unhandled-event-loop-exception-in-plugin-org-eclipse-ui) and [here](https://bugs.eclipse.org/bugs/show_bug.cgi?id=319752) :) – Nigel Crasto Oct 21 '12 at 04:19

0 Answers0