2
AutoCompleteTextView autoCompView = 
          (AutoCompleteTextView) findViewById(R.id.autocomplete_city);

エラーが表示される

メソッド findViewById は、タイプ CityFragment に対して定義されていません。

と:

public class CityFragment extends Fragment {

    public CityFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.city,
                    container, false);

    AutoCompleteTextView autoCompView = 
                (AutoCompleteTextView) findViewById(R.id.autocomplete_city);

        autoCompView.setAdapter(
                       new PlacesAutoCompleteAdapter(this, R.layout.list_item)
                );

    return rootView;
    }
}

私は基本的にhttps://developers.google.com/places/training/autocomplete-androidからコードをコピーしました

エラーが発生する理由はありますか?

4

4 に答える 4

4

それは確かにそのFragmentようなメソッドを持っていないからfindViewById()です。代わりに、 を使用しrootViewてアクセスする必要があります。

AutoCompleteTextView autoCompView = 
                (AutoCompleteTextView)rootView.findViewById(R.id.autocomplete_city);
于 2013-09-27T17:13:12.510 に答える
2

で変更:

AutoCompleteTextView autoCompView = 
                (AutoCompleteTextView) rootView.findViewById(R.id.autocomplete_city);
于 2013-09-27T17:13:41.663 に答える
2

変化する:

AutoCompleteTextView autoCompView = 
          (AutoCompleteTextView) findViewById(R.id.autocomplete_city);

に:

AutoCompleteTextView autoCompView = 
                (AutoCompleteTextView) rootView.findViewById(R.id.autocomplete_city);
于 2013-09-27T17:14:43.737 に答える
1

ここで rootView は AutoCompleteTextView の親です。したがって、次のように変更します。

AutoCompleteTextView autoCompView = 
                (AutoCompleteTextView) rootView.findViewById(R.id.autocomplete_city);
于 2013-09-27T17:43:43.530 に答える