使用NULL对象引用的应用程序崩溃



我正在使用fragment中的autoCompletextView ...但是,当我使用一种刷新AutoCompleteTextView建议的方法时,该应用程序会使用Null Object Reference崩溃这是方法 -

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;
}

可能是您的 getView()是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;
}
 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;
}

最新更新