1

I am using a AutoCompleteTextView in a Fragment... but when I use a method which refreshes the Suggestions for AutoCompleteTextView, the App crashes with Null Object Reference here's the method -

private ArrayList<String> sugs = new ArrayList<String>();

public void refreshSugs() 
{
    ((AutoCompleteTextView) getView().findViewById(R.id.editText1)).setAdapter(new ArrayAdapter(getActivity(), 17367050, this.sugs));
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
{
    //Inflating the Layout 
    View rootView = inflater.inflate(R.layout.home_fragment, container, false); 

    Button buttongo = (Button) rootView.findViewById(R.id.button1);
    EditText et1 = (EditText) rootView.findViewById(R.id.editText1);
    Button buttonsave = (Button) rootView.findViewById(R.id.button2);


    bar = (ProgressWheel) rootView.findViewById(R.id.progressBar1);
    i = (ImageView) rootView.findViewById(R.id.imageView1);
    background2 = (TextView) rootView.findViewById(R.id.textView1);
    background3 = (TextView) rootView.findViewById(R.id.textView2);
    readSugs();
    refreshSugs();

    return rootView;
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Darshan
  • 4,020
  • 2
  • 18
  • 49

2 Answers2

2

Might be your getView() is null.

 private ArrayList<String> sugs = new ArrayList<String>();
private View rootView;
public void refreshSugs() {
         ((AutoCompleteTextView) rootView.findViewById(R.id.editText1)).setAdapter(new ArrayAdapter(getActivity(), 17367050, this.sugs));

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
    {
        //Inflating the Layout 
      rootView = inflater.inflate(R.layout.home_fragment, container, false); 

         ..... your code

        return rootView;
}
Mavya Soni
  • 932
  • 7
  • 15
1
 private ArrayList<String> sugs = new ArrayList<String>();
private View rootView;
public void refreshSugs(View rootView) {
         ((AutoCompleteTextView) rootView.findViewById(R.id.editText1)).setAdapter(new ArrayAdapter(getActivity(), 17367050, this.sugs));

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
    {
        //Inflating the Layout 
      rootView = inflater.inflate(R.layout.home_fragment, container, false); 

         ..... your code...........
      refreshSugs0(rootView);
        return rootView;
}
vasupujy
  • 442
  • 2
  • 11