0

MySpinner.java

Below is my custom adapter

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) 
{
      inflater1=(LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      rowlayout=inflater1.inflate(R.layout.spinnerlayout, parent, false);
      txt1=(TextView)rowlayout.findViewById(R.id.textView1);
      txt1.setText(s[position]);                
      txt1.setTextSize(TypedValue.COMPLEX_UNIT_DIP,DropDownTextSize);
      txt1.setHeight(RowSize);
      return rowlayout;
 }

}

I made the custom spinner in a different project,which i marked as a library project.

Suppose i have a main.xml file with a linear layout. If i add only custom spinner to it,it works fine without any error even if main.xml is in same project or other.

But if i add any other widget to linear layout along with the custom spinner,it gives me null pointer exception,only if that main.xml is not in the same project.

For main.xml in the same project it works just fine.

1) Why is it that my custom spinner is not visible in the pallete in projects with different target api's?

2)In other project's main.xml if i add only custom spinner,it works properly but if there is any other component like button,textview etc along with it,it gives me a null pointer exception at

txt1.setText(s[position]);

Why does this happen?

Flake
  • 1,386
  • 17
  • 31
  • What is `s`? Its declaration and definition are not in the code you've provided. – Cat Jun 24 '12 at 19:55
  • s is the string array to populate the spinner – Flake Jun 24 '12 at 19:56
  • Yes, but either `txt1` or `s` is null here. Check your `spinnerlayout.xml` file, and ensure that you have a `TextView` with ID `textView1`. If you do, then `s` is null. – Cat Jun 24 '12 at 19:59
  • in the debugger it shows that "txt1" is null not "s" and it only shows "txt1" null when i use custom spinner and some other in built views in the main.xml of other projects.It works fine when only custom spinner is used in the main.xml of other projects. – Flake Jun 24 '12 at 20:04

1 Answers1

0

You're going to have to look at this in the debugger. Either txt1 is null indicating a problem with your layout, or s is null, probably indicating that the adapter hasn't gotten initialized yet.

Break the code into smaller parts. Inspect whatever variables each line uses in the debugger, before you single step through that line. Your problem will turn up.

mjfgates
  • 3,351
  • 1
  • 18
  • 15